注意!后续application.yml中的spring.application.name需使用驼峰命名或其他,名称中不可存在符号,如provider-user,需改成providerUser,不然后面讲到的Config组件中,git中的配置文件命名也会带-,然后就会出现无法自动加载配置的bug。
5、Config配置中心
5.1 配置Config Server
依赖如下
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-bus-amqp</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-config-server</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-config-monitor</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-netflix-eureka-client</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
</ dependencies>
bootstrap.yml
解释下这里为什么要改名为bootstrap,这里涉及到一个先后顺序,Spring Boot会先加载bootstrap.yml然后再加载application.yml。
server :
port : 6001
eureka :
client :
service-url :
defaultZone : http: //eureka7001.com: 7001/eureka, http: //eureka7002.com: 7002/eureka, http: //eureka7003.com: 7003/eureka
spring :
cloud :
config :
server :
git :
uri : https: //gitee.com/BigYoungZhao/spring- cloud- config.git
username : 你的码云账号
password : 你的码云密码
rabbitmq :
host : localhost
port : 5672
username : guest
password : guest
application :
name : configServer6001
management :
endpoints :
web :
exposure :
include : "*"
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServer6001Application {
public static void main ( String[ ] args) {
SpringApplication. run ( ConfigServer6001Application. class , args) ;
}
}
检查是否从git获取到配置文件
启动Config Server项目,访问http://localhost:6001/user-dev.yml
检查monitor 接口是否可用,访问http://localhost:6001/monitor
为什么是访问这个接口呢?来看看源码
在spring-cloud-config-monitor
包下,该包定义了目前支持通知提醒的方式。常用的支持gitee 、github 、gitlab这三种git的配置 。notifyByPath
,顾名思义,该接口表示根据路径通知服务的方法
@RequestMapping ( method = RequestMethod. POST)
public Set< String> notifyByPath ( @RequestHeader HttpHeaders headers,
@RequestBody Map< String, Object> request) {
PropertyPathNotification notification = this . extractor. extract ( headers, request) ;
if ( notification != null) {
Set< String> services = new LinkedHashSet < > ( ) ;
for ( String path : notification. getPaths ( ) ) {
services. addAll ( guessServiceName ( path) ) ;
}
if ( this . applicationEventPublisher != null) {
for ( String service : services) {
log. info ( "Refresh for: " + service) ;
this . applicationEventPublisher
. publishEvent ( new RefreshRemoteApplicationEvent ( this ,
this . busId, service) ) ;
}
return services;
}
}
return Collections. emptySet ( ) ;
}
5.2 配置micro-service(需要获取配置的对象服务)
改造上一篇的一个微服务
依赖如下
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-bus-amqp</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-config-client</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-netflix-eureka-client</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
</ dependencies>
application.yml
server :
port : 8001
bootstrap.yml
spring :
cloud :
config :
name : user
profile : dev
discovery :
service-id : configServer6001
uri : http: //localhost: 6001
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderUser8001Application {
public static void main ( String[ ] args) {
SpringApplication. run ( ProviderUser8001Application. class , args) ;
}
}
写个controller测试下
@RestController
@RefreshScope
public class UserController {
@Value ( "${spring.datasource.url}" )
private String datasourceUrl;
@GetMapping ( "/getDatasourceUrl" )
public String getDatasourceUrl ( ) {
return this . datasourceUrl;
}
}
按顺序启动Eureka、Config Server、微服务,访问http://localhost:8001/getDatasourceUrl
当我把user-dev.yml
里的数据库改为test_prod
时,再次去访问http://localhost:8001/getDatasourceUrl
,还是上图的结果。
一旦修改,配置中心的数据是刷新了,而微服务的配置数据只是在请求时获取到,之后不会主动去重新拉取新的配置数据。只能靠 POST请求 http://localhost:6001/actuator/bus-refresh
进行手动刷新。这个主要是通过我们配置的RabbitMQ,将刷新数据的消息放到MQ,并被微服务消费,从而刷新。
那么该如何自动刷新呢?
webhook机制,相当于一个回调,在我们push时,调用这个接口http://公网ip或域名/monitor
。
注意:由于我们的工程在本机,外网无法访问到你的服务,所以搞个内网穿透的工具(natapp )。然后修改本地yml文件再push上去,再调用上面微服务的接口即可看到自动刷新的效果。