建立配置中心module:
依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
yml:
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/surpera/springcloud-config.git #github路径
search-paths:
- springcloud-config
label: main
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
主启动类:
@SpringBootApplication
@EnableConfigServer
public class ConfigCenter3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenter3344.class,args);
}
}
测试可以从github中获取到配置信息,启动eureka7001和配置中心3344:
访问:http://localhost:3344/main/config-dev.yml
访问测试成功:
推荐配置规则:
然后建立3355客户端module来获取配置中心的配置:
依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
bootstap.yml:
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: main #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 name+profile=config-dev.yml的config-dev
uri: http://localhost:3344 #配置中心地址
controller:
@RestController
@RefreshScope //动态刷新
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
主启动类:
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
启动3355并访问github:http://localhost:3355/configInfo
结果:
问题:当github上的配置文件修改时,3344刷新可以获得修改后的配置,
但是3355由于yml中没有连接github,只有重启才能获取到修改后的配置。
解决方法:
首先引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
yml添加:
management:
endpoints:
web:
exposure:
include: "*"
来暴露监控端口
controller类加上@RefreshScope注解:
@RefreshScope
@RestController
public class ConfigClientController {}
主启动:
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
之后如果修改了github的配置文件:
只要在cmd中输入:curl -X POST “http://localhost:3355/actuator/refresh”
就可以让3355不用重启就可以得到更新后的配置。
问题:需要手动刷新,无法大范围定点的刷新。
解决方法,加入springcloud BUS消息总线(支持rabbitqm和kafka):
安装Erlang,再安装RabbitMQ
启动rabbitmq,访问http://localhost:15672/测试启动成功。
然后建立和3355客户端一样项目3366客户端:
集体修改配置中心3344,客户端3355,客户端3366的
pom.xml:
加入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
yml中加入:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
之后修改配置中心的配置文件后:
命令行输入:
curl -X POST "http://localhost:3344/actuator/bus-refresh"
发现3355,3366客户端都刷新了。
定点刷新输入:
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
只刷新3355客户端,不刷新3366客户端.