前言
前面我们搭建好了几个服务( https://segmentfault.com/a/11... )后面将springCloud config相关的放下了,今天抽时间将这一部分补齐,并通过bus消息总线实现动态刷新配置(热部署,不重启)。首先我们来看一下为什么要用springcloud config。(ps:本文适合有一定基础的童鞋看,并且使用的是springboot2,Springboot1.5.x
和2的使用方式稍有不同,望悉知。需要springCloud config 基础知识的童鞋可以移步到微笑大佬博客: http://www.ityouknow.com/spri...
)
在我们了解spring cloud config之前,我可以想想一个配置中心提供的核心功能应该有什么
Spring Cloud Config可以完美的支持以上所有的需求。
Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git,我们先以git为例做一套示例。
server端
需要添加额pom:
<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>
启动类:
@SpringBootApplication @EnableConfigServer @EnableEurekaClient public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); }
}
yml:
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:
<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>
启动类不用加什么
@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...