4.config 组件
1.configserver 配置
1.写pom
<!--config server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.写配置
server:
port: 8848
spring:
application:
name: CONFIGS
cloud:
consul:
host: localhost
port: 8500
config:
server:
git:
#分支
default-label: master
#uri
uri: https://gitee.com/breezewangsz/config.git
# 私有库要写用户名和密码
# username:
# password:
3.在启动类上写注解
@EnableConfigServer
@EnableDiscoveryClient
2.configclinet 配置
1.写pom
<!--config client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--使用远程配置 bootstrap.yaml 要写pom 没有会保找不到yaml的错误-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
2.写配置
#注册中心要写本地
spring:
cloud:
consul:
port: 8500
host: localhost
config:
discovery:
#配置所在服务Id
service-id: CONFIGS
#开启云配置
enabled: true
#所在分支
label: master
#服务名字
name: configclient
#环境
profile: dev
3.在启动类上写注解
@EnableDiscoveryClient
3.手动配置刷新
1.在controller类上加注解
@RefreshScope
//不要重启微服务的情况下将当前scope域中刷新为最新配置
2.curl -X POST http://localhost:客户端服务器端口/actuator/refresh
必须暴露端点(服务端客户端都要加)
management:
endpoints:
web:
exposure:
include: "*"
4.自动刷新
1.写pom
服务端
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--discovery 服务发现注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!--健康检查依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--config client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
客户端
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--discovery 服务发现注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!--健康检查依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--config server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--bus依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2.写配置
服务端
server:
port: 8848
spring:
application:
name: CONFIGS
cloud:
consul:
host: localhost
port: 8500
config:
server:
git:
default-label: master
uri: https://gitee.com/breezewangsz/config.git
# username:
# password:
rabbitmq:
host: xxxxxxxxx
port: xxxx
username: xxxx
password: xxxx
virtual-host: /
management:
endpoints:
web:
exposure:
include: "*"
客户端
spring:
cloud:
consul:
port: 8500
host: localhost
config:
discovery:
service-id: CONFIGS
enabled: true
label: master
name: configclient
profile: dev
#启动时当远程配置还没有完整时项目启动所以的失败都是运许的(没配见错误1)
fail-fast: true
management:
endpoints:
web:
exposure:
include: "*"
3.注解
服务端
@EnableDiscoveryClient
@SpringBootApplication
客户端
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
![远程仓库](https://img-blog.csdnimg.cn/b15c00bb501a49109c3030f155ab1554.png)
4.刷新
#刷新所有服务(new)
http://localhost:8848/actuator/busrefresh
(old)
http://localhost:8848/actuator/bus-refresh
#刷新指定服务
http://localhost:8848/actuator/busrefresh/服务ID
(old)
http://localhost:8848/actuator/bus-refresh/服务ID
错误1
![错误](https://img-blog.csdnimg.cn/3597bc7b6b1f46ee996f21484aa80e99.png)