社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
jdk 1.8
spring boot:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
spring cloud:
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
参考:http://www.cnblogs.com/ityouknow/p/6931958.html 搭建配置中心和消息总线
并在更新为2.0版本遇到问题
<!--配置中心依赖-->
<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-bus-amqp</artifactId>
<version>2.0.0.RC1</version>
</dependency>
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
server:
port: 8099
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/XXX/config
basedir: E:codejavaconfigcentersrcgit_data
username: 用户名
password: 密码
rabbitmq:
host: 127.0.0.1
port: 5672
username: test
password: 123456
application:
name: configserver
management:#此处与2.0之前配置不同,配置路径改变,暴露接口
endpoints:
web:
exposure:
include: "*"
http://localhost:8099/product-dev.yml
可以看到接口已经暴露,只是请方式不对。
首先,这个接口是spring-cloud-config-monitor 包下,该包定义了目前支持通知提醒的方式。常用的支持gitee 、github 、gitlab这三种git的配置
至于为什么要monitor 接口,我的看法是:PropertyPathEndpoint类的请求注解,只有这种带/monitor才会进到这个方法
@RequestMapping(path = "${spring.cloud.config.monitor.endpoint.path:}/monitor")
然后才能调用
@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();
}
看方法名,是根据路径通知服务的方法。
具体的没有过多了解,如果有错误还请指出
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
该文件优先于application.yml执行,在此配置文件获取git的配置
spring:
cloud:
config:
name: product #对应product-dev.yml中的product
profile: dev #对应product-dev.yml中的dev
discovery:
service-id: configserver
uri: http://localhost:8099 #不写这个直接报错:Cannot determine embedded database driver class for database type NONE
基本配置
server:
port: 8091
/**
* 获取git配置信息
*/
@RestController
@RefreshScope //刷新范围,必要,注意
public class GitConfigController {
@Value("${env}")
private String appEnv;
@GetMapping("/gitInfo")
public String configInfo() {
return this.appEnv;
}
}
可以看到已经获取到git的配置。
目前能做的只是conggit拉取配置信息,但是一旦修改,配置中心的数据是刷新了,而product服务的配置数据只是在请求时获取到,之后不会主动去重新拉取新的配置数据。
前提手动将git数据product-dev.yml中env改为 dev
curl -X POST http://localhost:8099/actuator/bus-refresh
接着可以看到数据已经刷新
这个主要是通过我们配置的rabbitMQ,将刷新数据的消息放到mq,并被product消费,从而刷新。
webhook机制,相当于一个回调,在我们push时,调用这个接口。
注意:由于我们的工程在本机,外网无法访问到你的服务,所以搞个内网穿透的工具(natapp)。url填写monitor 上文提到的那个。至于为神马不是/actuator/bus-refresh 应该是更新为2.0版本的缘故,我试了没成功,报错:Cannot deserialize instance of
java.lang.String
out of START_ARRAY token; 搞不定。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!