Spring Config 为我们处理配置中心问题带来了极大的方便,不过由于spring对config的封装很“过度”,所以如果遇上问题都会很头疼,那么这里把几个我遇到的坑在这里简单的为大家叙述下:
首先项目大体一般需要四个:
config-server
config-client
config-bus
eureka-server
注意:config-server和config-client不能放同一个project中不然无法读取配置值和bus驱动刷新配置值。因为maven包可能有冲突。
1. 关于git:
比如 我的项目https://github.com/cauchy8389/J2EETest
我把配置放根目录config文件夹下,那么我在config-server项目中做如下配置
server:
port: 8899
spring:
application:
name: eureka-config-server
kafka:
bootstrap-servers: zhy.cauchy8389.com:9092
consumer:
group-id: groupA
profiles:
active: git
cloud:
config:
server:
git:
uri: https://github.com/cauchy8389/J2EETest
search-paths: config
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
uri后面J2EETest后面不要跟斜杠(/)
然后就可以访问配置文件夹中的配置,访问方式如下:
http请求地址和资源文件映射如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
举个栗子:
http://localhost:8899/eureka-config-client/dev
2. config client配置:
建boot配置,默认可为bootstrap.yml也可以建任何名字然后启动参数启动:
server:
port: 8081
spring:
application:
name: eureka-config-client
kafka:
bootstrap-servers: zhy.cauchy8389.com:9092
consumer:
group-id: groupA
cloud:
config:
discovery:
enabled: true
service-id: eureka-config-server
profile: dev
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
如果名为config-client-boot 则可以在idea中加启动参数(路径填全):
-Dspring.cloud.bootstrap.location=D:/WorkSpace/J2EETest/src/main/resources/springcloud/config-client-boot.yml
接受配置变换的代码:
@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope
public class ClientApplication {
@Autowired
private Environment env;
@Value("${mybook}")
private String mybook;
@RequestMapping("/")
public String home() {
String name = env.getProperty("test.user.name");
return "Hello " + name + mybook;
}
public static void main(String[] args) {
new SpringApplicationBuilder(ClientApplication.class).
web(WebApplicationType.SERVLET).run(args);
//VM option :
//-Dspring.cloud.bootstrap.location=classpath:/springcloud/config-client-boot.yml
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
c.setIgnoreUnresolvablePlaceholders(true);
return c;
}
}
3.maven依赖:
client:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
server:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
bus:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
4.关于bus:
配置:
server:
port: 10001
spring:
kafka:
bootstrap-servers: zhy.cauchy8389.com:9092
consumer:
group-id: groupA
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: bus-refresh
endpoint:
bus-refresh:
enabled: true
如何使用web接口刷新:
http://localhost:10001/actuator/可以看到里面暴露的接口
因为我们在配置里面配置了暴露接口为bus-refresh 所以我们看到
{"_links":{"self":{"href":"http://localhost:10001/actuator","templated":false},"bus-refresh":{"href":"http://localhost:10001/actuator/bus-refresh","templated":false},"bus-refresh-destination":{"href":"http://localhost:10001/actuator/bus-refresh/{destination}","templated":true}}}
然后 我们启动postman 因为只有post方式调用bus-refresh接口
选择form-data方式 然后uri输入http://localhost:10001/actuator/bus-refresh/
点击send 然后见看到配置接受的都变了。
有任何问题,请留言,我将不定期答复。