Spring Cloud Bus与RabbitMq实现config-server自动刷新配置及注意问题
程序员文章站
2022-06-13 10:27:03
...
spring boot版本1.3.7
获取配置客户端
@RefreshScope
@RestController
public class TestController {
//必须用brootstrap命名,否则报错
@Value("${from}")
private String from;
@RequestMapping("/from")
public String from() {
return this.from;
}
@Autowired
private Environment env;
@RequestMapping("/from2")
public String from2() {
return env.getProperty("from", "undefined");
}
}
spring.application.name=didispace
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.profile=dev
#spring.cloud.config.label=master
#spring.cloud.config.uri=http://localhost:7001/
server.port=7002
#这个一定要写上,否则请求post到http://localhost:7002/bus/refresh 报验证错
management.security.enabled=false
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.usernarne=guest
spring.rabbitmq.password=guest
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
服务配置端
spring.application.name=config-server
server.port=7001
#配置服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
management.security.enabled=false
#以下是个人配置
spring.cloud.config.server.git.uri=https://gitee.com/XXXX/didispace/
spring.cloud.config.server.git.searchPaths=config
spring.cloud.config.server.git.username=*****
spring.cloud.config.server.git.password=*****
@EnableConfigServer
@EnableAutoConfiguration
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
启动相关
第一次请求
注意问题:更改git仓库配置后需要请求
post请求到 http://localhost:7002/bus/refresh 7002是客户端的端口
若报错需要关闭校验
{"timestamp":1513070580796,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource.","path":"/bus/refresh"}campus-080-239
解决方法:
management.security.enabled=false