这里写目录标题
原理
搭建config项目
需要两个项目和github仓库
新增github仓库
spring-cloud-config-server
pom.xml
<?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 https://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.2.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cx</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
yml
eureka:
## 表示为Erureka客户端
client:
## 单台server为服务器不需要注册到服务器上
register-with-eureka: true
## 获取注册列表的信息 道理同上 基本两者参数是一样的
fetch-registry: true
service-url: # Eureka服务提供地址
defaultZone: http://node1:10097/eureka/
spring:
cloud:
config:
server: #git仓库的配置
git:
uri: https://github.com/cxjwbj/config-test2 # 指定仓库url
search-paths: config # 当前包下所有文件
username: cxwbj14@163.com # 账号
password: qq136 # 密码
application:
name: config-server
server:
port: 7000
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
spring-cloud-config-client
pom.xml
<?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 https://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.2.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cx</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-config-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
bootStrap.yml
## bootstrap的优先级大于application 我们要抓取配置信息需在应用启动之前 必须的使用bootstrap
eureka:
## 表示为Erureka客户端
client:
## 单台server为服务器不需要注册到服务器上
register-with-eureka: true
## 获取注册列表的信息 道理同上 基本两者参数是一样的
fetch-registry: true
service-url: # Eureka服务提供地址
defaultZone: http://node1:10097/eureka/
spring:
application:
name: myConfig # 此处的名字必须和GitHub仓库的配置文件的名字一样
cloud:
config:
uri: http://localhost:7000 # server的uri
label: master # 不指定也是master分支
server:
port: 8000
编写config获取对应数据
@Configuration
public class ConfigClientConfig {
@Value("${redis.host}")
private String redisHost;
@Value("${mysql.host}")
private String mysqlHost;
@Value("${mongo.host}")
private String mongoHost;
public String getRedisHost() {
return redisHost;
}
public String getMysqlHost() {
return mysqlHost;
}
public String getMongoHost() {
return mongoHost;
}
}
编写controller
@RestController
public class MyController {
@Autowired
private ConfigClientConfig configClientConfig;
@RequestMapping(value = "hosts")
public String getHosts() {
return configClientConfig.getMongoHost() + ", " +
configClientConfig.getMysqlHost() + ", " +
configClientConfig.getRedisHost();
}
}
依次启动 server config-server config-client
//数据缓存到本地git仓库
跟config-server建立连接
此时访问
http://localhost:8000/hosts
获取的配置文件的信息
如何获取的不同环境下的配置文件了?
我们在建个dev下的配置文件
此时在config-client yml文件修改
## bootstrap的优先级大于application 我们要抓取配置信息需在应用启动之前 必须的使用bootstrap
eureka:
## 表示为Erureka客户端
client:
## 单台server为服务器不需要注册到服务器上
register-with-eureka: true
## 获取注册列表的信息 道理同上 基本两者参数是一样的
fetch-registry: true
service-url: # Eureka服务提供地址
defaultZone: http://node1:10097/eureka/
spring:
application:
name: myConfig # 此处的名字必须和GitHub仓库的配置文件的名字一样
cloud:
config:
uri: http://localhost:7000 # server的uri
label: master # 不指定也是master分支
profile: devellop # 此时还会抓取myConfig-develop的配置文件
server:
port: 8000
此时依次重启对应服务
client加载了对应的配置文件
重新访问http://localhost:8000/hosts 返回数据就发生改变
gitHub和config-client客户端yml对应规则
如:myConfig-devellop.properties
- myConfig:客户端名字
- devellop:yml 文件profile: devellop对应的环境必须是最后一个“-”默认就是myConfig.properties
- label: 就是gitHub分支名默认是master