環境: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的最新值已經同步更新顯示