前言
前面咱们搭建好了几个服务(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...