Spring cloud configServer配置中心简单使用
Spring cloud configServer配置中心简单使用
<dependencies>
<!--eureka client 客户端依赖引⼊-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--config配置中⼼服务端-->
<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>
</dependencies>
在github上新建仓库
https://github.com/ln0491/test-config
新建项目
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
//开启配置中心服务
@EnableConfigServer
//开启服务发现
@EnableDiscoveryClient
@SpringBootApplication
public class Config1Application {
public static void main(String[] args) {
SpringApplication.run(Config1Application.class,args);
}
}
配置文件
server:
port: 9991
# 注册到eureka注册中
eureka:
client:
service-url:
# 使用逗号分隔 多个实例,
defaultZone: http://EduCloudEurekaServerB:8762/eureka,http://EduCloudEurekaServerA:8761/eureka
instance:
prefer-ip-address: true #服务实例中显示ip,而不是显示主机名(兼容老的eureka版本)
# 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以自定义它
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@aaa@qq.com
# 自定义元数据
# metadata-map:
# cluster: cl1
# region: rn1
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
logging:
level:
com.liu.service.feign.ResumeFeignClient: debug
spring:
application:
name: edu-cloud-config
cloud:
config:
server:
git:
uri: https://github.com/ln0491/test-config
username: 这里github用户名
password: 这里是github的密码
search-paths:
- edu-config-repo
label: master
rabbitmq:
host: 127.0.0.1
port: 5672
username: root
password: root123
virtual-host: dev_tests
访问
http://localhost:9991/master/edu-service-resume-dev.yml
构建Client客户端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
- bootstrap.yml
spring:
cloud:
config:
name: edu-service-resume
profile: dev
label: master
uri: http://localhost:9991
management:
endpoints:
web:
exposure:
include: "*"
# 注册到eureka注册中
eureka:
client:
service-url:
# 使用逗号分隔 多个实例,
defaultZone: http://EduCloudEurekaServerB:8762/eureka,http://EduCloudEurekaServerA:8761/eureka
instance:
prefer-ip-address: true #服务实例中显示ip,而不是显示主机名(兼容老的eureka版本)
# 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以自定义它
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@aaa@qq.com
# 自定义元数据
metadata-map:
cluster: cl1
region: rn1
- application.yml
server:
port: 8080
spring:
application:
name: edu-service-resume
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/edu?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=UTC
username: root
password: root
jpa:
database: MySQL
show-sql: true
generate-ddl: false
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
ddl-auto: update
- 启用类
@RefreshScope
//@EnableEurekaClient //Eureka 单独有
@SpringBootApplication
@EnableDiscoveryClient //开启注册中心客户端 通用
public class Resume8080Application {
public static void main(String[] args) {
SpringApplication.run(Resume8080Application.class,args);
}
}
- controller
@RestController
public class ConfigController {
@Value("${mysql.url}")
private String mysqlUrl;
@Value("${edu.message}")
private String eduMessage;
@RequestMapping("/config")
public String viewconfig(){
System.err.println("mysqlUrl "+mysqlUrl);
System.err.println("eduMessage "+eduMessage);
return mysqlUrl+"\n"+eduMessage;
}
}
- 访问
http://localhost:8080/config
在github上修改配置
访问
http://localhost:9991/master/edu-service-resume-dev.yml
发现配置已经修改了
http://localhost:8080/config 并没有更新,重启后,更新成功
在Controller上
@RefreshScope
public class ConfigController {
- 重启应用
修改github的配置
手动刷新
http://localhost:8080/actuator/refresh
http://localhost:8080/config
使⽤⼴播机制,⼀次通知,处处⽣效,⽅便⼤范围配置刷新
结合消息总线(Bus)实现分布式配置的⾃动更新(Spring Cloud
Config+Spring Cloud Bus)
Spring Cloud Bus(基于MQ的,⽀持RabbitMq/Kafka) 是Spring Cloud中的消息总线⽅案, Spring
Cloud Config + Spring Cloud Bus 结合可以实现配置信息的⾃动更新
Spring Cloud Config+Spring Cloud Bus 实现⾃动刷新
使⽤RabbitMQ, ConfigServer和ConfigClient都添加都消息总线的⽀持以及
与RabbitMq的连接信息
- Config Server与客户端添加息总线⽀持
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2) ConfigServer与客户端添加配置
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: root
password: root123
virtual-host: dev_tests
- 修改github上的配置
重启各个服务,更改配置之后,向配置中⼼服务端发送post请求
http://localhost:9991/actuator/bus-refresh
再访问
http://localhost:8080/config
http://localhost:8082/config
不用重启,所有客户端配置已经更新
定向配置刷新
http://localhost:9991/actuator/bus-refresh/edu-service-resume:8080
最后⾯跟上要定向刷新的实例的 服务名:端⼝号即可
推荐阅读
-
利用Spring Cloud Config结合Bus实现分布式配置中心的步骤
-
spring-cloud入门之spring-cloud-config(配置中心)
-
SpringCloud之分布式配置中心Spring Cloud Config高可用配置实例代码
-
详解spring cloud config整合gitlab搭建分布式的配置中心
-
spring cloud config分布式配置中心的高可用问题
-
Spring Cloud微服务架构的构建:分布式配置中心(加密解密功能)
-
spring cloud config 配置中心快速实现过程解析
-
跟我学SpringCloud | 第六篇:Spring Cloud Config Github配置中心
-
Spring Cloud Config实现分布式配置中心
-
SpringCloud之分布式配置中心Spring Cloud Config高可用配置实例代码