環境:SpingBoot2.0 ,SpringCloud Finchley.RELEASE
登錄github創建一個倉庫 myspringcloudconfig 然后再創建一個config-client1文件夾
編寫application.yml
name: zhangsan
application-dev.yml
name: zhangsan-dev
application-test.yml
name: zhangsan-test
3個文件並上傳到config-client1目錄下
搭建rabbitmq服務器
省略...
搭建Config Server項目 取名 zns-config
<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>
啟動類
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的最新值已經同步更新顯示