本文主要是介绍SpringBoot2.0以上使用Spring Cloud Config配置中心【服务端刷新】(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
SpringBoot2.0以上使用Spring Cloud Config配置中心【服务端刷新】(二)
服务端:Spring Cloud Config Server
- 引用相关依赖(可以直接用idea创建springboot下的spring-cloud-config项目):
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><!-- springcloud-bus依赖实现配置自动更新,rabbitmq --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency>
-
启动类加入@EnableConfigServer注解
-
bootstrap.yml文件中加入配置(其中部分配置可写在application.yml中,但是management相关需要写在bootstrap.yml中):
#服务启动端口配置:
server: port: 8080
#指定配置文件所在目录或地址
spring:cloud:config:server:native:#search-locations: classpath:/config/search-locations: D:/Project/config/config-serve/#本地环境不需要配置mq,但是需要启动mq,Springboot会自动连接本地mqrabbitmq:host: localhostport: 5672username: guestpassword: guest
#修改server端配置文件,将相关管理以及健康管理端口暴露
management:endpoints:web:exposure:include: bus-refreshserver:port: 9001
- D:/Project/config/config-serve/下的配置文件application-dev.properties:
server.address=127.0.0.1
server.port=8081
a.url=dev1233
客户端:Spring Cloud Config Client
- 引入依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency>
- 在resources下新建bootstrap.yml配置文件
#开放手动刷新refresh端口,地址为 /actuator/refresh
management:endpoints:web:exposure:include: bus-refresh
# 指明配置服务中心的网址
spring:application:name: applicationcloud:config:label: masterprofile: devuri: http://127.0.0.1:8080#本地环境不需要配置mq,但是需要启动mq,Springboot会自动连接本地mqrabbitmq:host: localhostport: 5672username: guestpassword: guest
- 编写controller测试
@RestController
@RefreshScope
public class ClientController {@Value("${a.url}")private String datUrl;@GetMapping(value = "/test")public String hi(){return datUrl;}}
-
@RefreshScope注解表示 当前配置、实例可以热加载更新
-
访问: http://localhost:8081/test 可以拿到 dev1233
-
修改D:/Project/config/config-serve/下的配置文件application-dev.properties a.url的值
-
以post方式访问: http://localhost:8080/actuator/bus-refresh 即可刷新成功。
注意几点:
- management.endpoints.web.exposure.include=bus-refresh 需要写在bootstrap 配置文件中,不然不会生效。
- springboot2.0之后原/bus/refresh更新为/actuator/bus-refresh
- 此文在springboo2.0环境下测试通过。
- 如果配置文件仓库使用git可使用git webhooks触发,其他的自行查找。只要确认能以post方式访问服务端/actuator/bus-refresh 即可。
这篇关于SpringBoot2.0以上使用Spring Cloud Config配置中心【服务端刷新】(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!