1、pom 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gray</groupId>
<artifactId>server-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<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>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-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、 yml配置
server:
port: 8610
spring:
application: #服务名称
name: config
profiles:
active: dev
cloud:
config:
server:
git:
uri: https://gitee.com/graycat/SpringCloudStudy.git
searchPaths: SpringCloud-dev/baseConfig #配置仓库路径
username: 59147***@qq.com
password: lizh***14ma
label: dev #配置仓库的分支
# 设置本地GIT存储目录
# basedir: /Users/admin/code/myProjects/java/imooc/SpringCloud_Sell/config/basedir 本地GIT存储地址设置
rabbitmq:
host: 192.168.1.16
port: 5672
username: admin
password: admin
virtualHost: /
publisherConfirms: true
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure: # 设置开放访问
include: refresh,health,info,bus-refresh
如果配置文件存在本地(在项目根目录/baseConfig文件夹)
server:
port: 8610
spring:
application:
name: config
profiles:
active: native
cloud:
config:
server:
native:
search-locations: file:./baseConfig
rabbitmq:
host: 192.168.34.168
port: 5672
username: admin
password: admin
virtualHost: /
publisherConfirms: true
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
management:
endpoints:
web:
exposure:
include: refresh,health,info,bus-refresh
3、启动类注解
@EnableConfigServer
@SpringCloudApplication
public class ServerConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ServerConfigApplication.class, args);
}
}
4、自定义了非Post刷新方式
@RestController
public class RefreshController {
@Autowired
private RestTemplate restTemplate;
@Value("${server.port}")
private int port;
@RequestMapping("/refresh")
public String refresh(){
String url = String.format("http://%s:%s", "localhost", port) + "/actuator/bus-refresh";
URI uri = URI.create(url);
//消息头
HttpHeaders httpHeaders = new HttpHeaders();
// 设置contentType
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
String httpBody = "{\"motto\":\"配置刷新\"}";
HttpEntity<String> httpEntity = new HttpEntity<String>(httpBody, httpHeaders);
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, httpEntity, String.class);
return response.getBody();
}
}
@Component
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
一般情况都是 发送post请求 到配置中心,http://localhost:8101/actuator/bus-refresh
在CMD 中可以模拟post刷新:curl -X POST http://localhost:8101/actuator/bus-refresh
5、client端 使用配置中心的配置 例:user服务器(正常客户端pom和启动类)
配置yml 由application.yml 改成 bootstrap.yml
spring:
cloud:
config:
discovery:
enabled: true # 打开使用配置中心开关
service-id: CONFIG #配置中心 服务名称
profile: dev # 使用配置类型
name: clientUser,datasource,rabbitmq # 需要的配置,这里配置了多个:自己配置,数据库,mq
label: master
eureka:
client:
service-url: # 这个不能放在配置中心,不然连配置中心都连不上
defaultZone: http://localhost:8761/eureka/
配置中心,文件存放

localhost:8610/getway-dev.yml
浏览器上能直接查看配置