1配置中心依赖
<dependencies>
<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>
</dependencies>
2在git建一个配置文件仓库(一定要是公开的),并获取git地址
user-dev.yml的内容
server:
port: ${port:9091}
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud
username: root
password: 123456
application:
name: user-service
mybatis:
type-aliases-package: com.itheima.user.pojo
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka
instance:
# 更倾向使用ip地址,而不是host名
prefer-ip-address: true
# ip地址
ip-address: 127.0.0.1
# 续约间隔,默认30秒
lease-renewal-interval-in-seconds: 5
# 服务失效时间,默认90秒
lease-expiration-duration-in-seconds: 5
test:
name: wangzhijun1234
3配置中心的配置文件(注意,要在该配置中心微服务的启动类上添加@EnableConfigServer //开启配置服务 注解)
server:
port: 12000
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/peacezhi/configuration-center-git.git
# 配置rabbitmq信息;如果是都与默认值一致则不需要配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
management:
endpoints:
web:
exposure:
# 暴露触发消息总线的地址
include: bus-refresh
4把user-service的application.yml删除,新增bootstrap.yml
spring:
cloud:
config:
# 要与仓库中的配置文件的application保持一致
name: user
# 要与仓库中的配置文件的profile保持一致
profile: dev
# 要与仓库中的配置文件所属的版本(分支)一样
label: master
discovery:
# 使用配置中心
enabled: true
# 配置中心服务名
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
5启动user-service服务实例
6config-server的依赖添加内容
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
7config-server的配置文件添加内容
server:
port: 12000
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/goheima/heima-config.git
# 配置rabbitmq信息;如果是都与默认值一致则不需要配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
management:
endpoints:
web:
exposure:
# 暴露触发消息总线的地址
include: bus-refresh
8user-service的依赖添加内容
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
9user-service的配置文件(user-dev.yml)添加内容
# 配置rabbitmq信息;如果是都与默认值一致则不需要配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
10UserController的修改
11修改user-dev.yml的配置,比如test.name=“zhangsan”
POST请求地址http://localhost:12000/actuator/bus-refresh
(这个地址为什么这么写,请参考config-server服务实例application.yml的配置文件怎么写的?)
12spring cloud bus总结:http://localhost:12000/actuator/bus-refresh请求配置中心服务读取git 配置文件到MQ里面去,然后user-service通过MQ读取新的配置文件。
链接:https://pan.baidu.com/s/1rb44BgzlkT_EQzUKKUfC2g
提取码:j2q6
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V1的分享