SpringCloud之集成配置中心及自动刷新

环境:SpingBoot2.0 ,SpringCloud Finchley.RELEASEgit

 

 登陆github建立一个仓库 myspringcloudconfig 而后再建立一个config-client1文件夹github

编写application.ymlweb

name: zhangsan

application-dev.ymlspring

name: zhangsan-dev

application-test.ymlbootstrap

name: zhangsan-test

 

3个文件并上传到config-client1目录下服务器

 

搭建rabbitmq服务器app

省略...测试

 

搭建Config Server项目 取名 zns-configurl

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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-bus-amqp</artifactId>
        </dependency>

启动类spa

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

bootstrap.yml

server: port: 10005 servlet: context-path: / eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: application: name: zns-config cloud: config: server: git: uri: https://github.com/zns/myspringcloudconfig.git #Git仓库的地址 search-paths: config-client1 #仓库中配置文件所在文件夹路径 默认 / username: a@qq.com password: 123456 label: master #仓库的分支 bus: trace: enabled: true rabbitmq: #本地环境能够不须要配置mq,可是须要启动mq,Springboot会自动链接本地的mq服务器 host: localhost port: 5672 username: guest password: guest management: #打开bus/refresh刷新开关 endpoints: web: exposure: include: "*"

 搭建Config Client项目 取名 zns-config-client

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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-bus-amqp</artifactId>
        </dependency>

启动类

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

bootstrap.yml

server: port: 10006 servlet: context-path: / eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: application: name: zns-config-client profiles: active: dev #使用哪一个环境配置文件 cloud: config: fail-fast: true discovery: enabled: true service-id: zns-config #配置中心项目服务的应用名称 rabbitmq: #本地环境能够不须要配置mq,可是须要启动mq,Springboot会自动链接本地的mq服务器 host: localhost port: 5672 username: guest password: guest

 

@RefreshScope加在须要读取配置的类上
@RestController @RefreshScope public class MyController { @Value("${name}") String name; @RequestMapping("/getName") public String getName(){ return name; } }

 

测试

1.启动rabbitmq

2.启动注册中心eureka

3.启动配置中心项目

4.启动配置客户端项目

 

访问 getName 控制器url,能够看到已经读取到了git上dev环境的name属性值

 

git上修改name的值保存提交,再调用配置中心服务刷新配置url  

http://localhost:10005/actuator/bus-refresh

 

客户端再次访问getName,能够看到name的最新值已经同步更新显示

相关文章
相关标签/搜索