前言
前面咱們搭建好了幾個服務(https://segmentfault.com/a/11...)後面將springCloud config相關的放下了,今天抽時間將這一部分補齊,並經過bus消息總線實現動態刷新配置(熱部署,不重啓)。首先咱們來看一下爲何要用springcloud config。(ps:本文適合有必定基礎的童鞋看,而且使用的是springboot2,Springboot1.5.x
和2的使用方式稍有不一樣,望悉知。須要springCloud config 基礎知識的童鞋能夠移步到微笑大佬博客: http://www.ityouknow.com/spri...
)html
在咱們瞭解spring cloud config以前,我能夠想一想一個配置中心提供的核心功能應該有什麼java
Spring Cloud Config能夠完美的支持以上全部的需求。mysql
Spring Cloud Config項目是一個解決分佈式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client經過接口獲取數據、並依據此數據初始化本身的應用。Spring cloud使用git或svn存放配置文件,默認狀況下使用git,咱們先以git爲例作一套示例。git
server端
須要添加額pom:github
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--springCloud config server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--springCloud eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--springCloud bus--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
啓動類:web
@SpringBootApplication @EnableConfigServer @EnableEurekaClient public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); }
} spring
yml:sql
server: port: 7770 spring: application: name: config-server cloud: config: enabled: true server: git: uri: https://github.com/iamcrawler/micro search-paths: config-repo username: iamcrawler password: *** bootstrap: true rabbitmq: host: www.iamcrawler.cn port: 5672 username: liuliang password: liuliang eureka: client: service-url: defaultZone: http://localhost:7777/eureka/ management: endpoints: web: exposure: include: "bus-refresh"
client端
pom:bootstrap
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--springCloud eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--springCloud feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- springboot2.0已經將oauth2.0與security整合在一塊兒 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- 因爲一些註解和API從spring security5.0中移除,因此須要導入下面的依賴包 --> <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> <version>2.0.0.RELEASE</version> </dependency> <!--springCloud config client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--springCloud bus--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> </dependencies>
啓動類不用加什麼segmentfault
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class UserServiceApplication { public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class, args); } }
bootstrap.yml:
eureka: client: service-url: defaultZone: http://localhost:7777/eureka/ server: port: 8080 spring: application: name: user-server cloud: config: profile: dev uri: http://localhost:7770/ bus: trace: enabled: true rabbitmq: host: www.iamcrawler.cn port: 5672 username: liuliang password: liuliang security: oauth2: resource: id: mall-resource-test user-info-uri: http://localhost:5555/auth/user prefer-token-info: false management: endpoints: web: exposure: include: bus-refresh
在須要訪問的地方加上@RefreshScope
@RestController @Slf4j @RefreshScope public class HelloController { @Autowired private OrderClient orderClient; @Value("${crawler.test:}") private String crawler; @GetMapping("/hello") public String hello(@RequestParam("name") String name) { System.out.println("入參:" + name); log.info("current:{}", MicroUserUtil.getCurrentUser()); return "hello " + name + "=====" + orderClient.order(name); } @GetMapping("/crawler") public String crawler(){ return this.crawler; } }
驗證
咱們花費了很大的勁集成springCloud config ,springCloud bus 經過rabbitmq發送即便消息使其動態更新。那麼怎麼測試呢?我如今準備訪問上面的路徑/crawler ,而這個crawler.test 是經過config-server配置鏈接的,config-server鏈接的是https://github.com/iamcrawler... 下的 config-repo ,咱們能夠看到,如今上面是liuliang000 即下面的圖:
那麼如今我將 config-repo 下的user-server-dev.yml配置文件的 crawler.test 改成 liang123 以下圖:
提交,push到指定分支之後,
首先咱們仍是調用/crawler ,發現結果沒有變,仍是liang000
下面模擬webhook發送一個請求:
而後咱們再次調用/crawler
結果變了!我沒有重啓任何服務!由此,config-server配置成功!
對本位有參考價值的文章有:
http://www.ityouknow.com/spri...
https://ask.csdn.net/question...
本文的github地址:
https://github.com/iamcrawler...