我们有许多用于多任务的微服务,其中大多数都有一些常见的功能,如日志配置、度量配置和kafka配置等。
我们计划拥有一个具有通用功能的通用JAR,并在所有微服务中使用。
当我们用创建自定义springboot jar时
application.yml
有了这些公共配置和其他一些@Component和@Service类,并作为依赖项包含在微服务中,就不会从特定于JAR的文件中读取任何属性
application.yml
在JAR中。
定制弹簧靴罐-
src/main/java -
@Configuration
public class KafkaConfiguration {}
@Service
public class CommonProcessor {}
src/main/resources-
application.yml
logging:
level:
org.apache: DEBUG
spring:
cloud:
stream:
kafka:
binder:
autoCreateTopics: false
brokers: localhost:9092
configuration:
sasl:
jaas:
config: org.apache.kafka.common.security.plain.PlainLoginModule required username="" password="";
在里面
KafkaConfiguration.java
我正在阅读上面的属性,并创建kafka连接、kafka模板对象等。如果我在这个jar中编写示例消费者/生产者,并作为springboot项目运行,一切都会很好。
但是,我想在我的微服务中使用这个jar作为依赖项,并使用kafka连接/kafkatemplate等,即当它无法从上面加载任何属性时
application.yml
并且创建bean失败。
微服务结构-
src/main/java
@Component
public class Consumer {
@Autowired
KafkaTemplate kafkaTemplate;
}
src/main/resources
application.yml
test:
property: test
example:
service:
url: https://localhost:8080/service
pom.xml
<dependency>
<groupId>com.test</groupId>
<artifactId>common-jar</artifactId>
<version>1.0</version>
我在这里缺少了什么,因为我的JAR文件无法通过加载属性来创建其中定义的bean?这种拥有2个application.yml的方法不正确吗?在一个地方拥有共同属性的最佳方式是什么?