在上一篇工程基础上
创建子工程 Config Server 作为配置服务提供者——》Config Server + Eureka Client
启动类添加注解
@EnableEurekaClient
@EnableConfigServer
配置文件,其中含有Eureka注册中心信息以及github 仓库信息
server:
port: 9999 #服务端口
spring:
application:
name: config-server #指定服务名
cloud:
config:
server:
git: #配置git仓库地址
uri: https://github.com/smalldragon1997/cloud-config.git
# search-paths:
# - myspringcloudconfig #配置文件目录地址
# username: xxx #账号(公有项目不需要设置)
# password: xxx #密码(公有项目不需要设置)
label: master #分支名称
###服务注册到eureka注册中心的地址
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/
###因为该应用为服务提供者,是eureka的一个客户端,需要注册到注册中心
register-with-eureka: true
###是否需要从eureka上检索服务
fetch-registry: true
instance:
# prefer-ip-address: true #将自己的ip地址注册到Eureka服务中
instance-id: ${spring.application.name}###${server.port} #指定实例id
在github上创建新仓库,并写入配置内容
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
jdbc.username=root
jdbc.password=12345611111
启动服务,测试获取配置文件
匹配规则,其中application为服务名、profile为环境名、label为分支默认为master,配置会根据规则自动在git寻找文件
/{application}/{profile}/[label]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
在原有的服务消费者工程中引入config client,向eureka中的config server获取配置文件,同时添加actuator提供刷新配置文件
配置文件类,JDBCConfig,其中@RefreshScope标注此类配置在手动刷新时,需要重新向git获取
@Component
@RefreshScope
public class JDBCConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.driverClassName}")
private String driverClassName;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
@Override
public String toString() {
return "JdbcConfigBean [url=" + url + ", username=" + username
+ ", password=" + password + ", driverClassName="
+ driverClassName + "]";
}
}
添加配置文件 bootstrap.yml,bootstrap优先级比application高,所以提前加载注入配置文件
###服务注册到eureka注册中心的地址
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/
spring:
application:
name: ribbon-consumer
cloud:
config:
name: ribbon-consumer # 应用名
# uri: http://localhost:9999/ #配置中心的地址
profile: test # 环境名
label: master #对应的分支
discovery:
enabled: true #启用发现服务功能
service-id: config-server #指定配置中心工程的名称
#开启所有端点 refresh config
management:
endpoints:
web:
exposure:
include: "*"
修改ribbon-consumer的controller类,获取配置文件信息并返回
@Autowired
private JDBCConfig jdbcConfig;
@GetMapping("/consumer")
public String getMsg() {
return consumerFeignClient.getMsg()+"\n获取到配置\n"+jdbcConfig.toString();
}
测试服务消费者通过config client 向config server获取配置文件,获取成功,此时密码为12345611111
在github上修改jdbc密码信息为123456
如果不手动刷新配置文件,密码还是iwei12345611111
这时向config client端发送刷新请求http://localhost:8090/actuator/refresh,方法为post,提示信息为改变的内容
这时再通过ribbon consumer controller 访问配置文件信息,可以看到配置文件在github修改后,通过手动刷新,两个服务器和客户端不需要重启也可以动态修改生效配置
若是需要动态加载yml文件或者是properties文件信息
需要将对应工程的需要动态变化的内容注释掉
然后在新建配置文件bootstrap.yml 添加config 信息,其中属性根据工程修改
###服务注册到eureka注册中心的地址 eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ spring: application: name: ribbon-consumer cloud: config: name: ribbon-consumer # 应用名 # uri: http://localhost:9999/ #配置中心的地址 profile: test # 环境名 label: master #对应的分支 discovery: enabled: true #启用发现服务功能 service-id: config-server #指定配置中心工程的名称 #开启所有端点 refresh config management: endpoints: web: exposure: include: "*"
引入 actuator 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency
假设需要配置动态网关,因为网关规则可能因为业务发展而需要经常修改,这里可以将网关规则剥离到github上进行管理,之后只需要在启动类中添加以下bean注入,实现动态网关配置
@RefreshScope
@ConfigurationProperties("zuul")
public ZuulProperties zuulProperties(){
ZuulProperties properties = new ZuulProperties();
System.out.println("properties:"+properties);
return properties ;
}