在分布式系统中,服务数量会很多,如果要修改服务的配置文件,会很麻烦,这个时候,我们想把配置文件放在一个地方统一管理,实时更新,Spring Cloud 就给我们提供了这样一个组件——Spring Cloud Config。
简介
在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
创建ConfigServer
New-projec-spring initializr , next-next-cloud config-server(client端就选client)
添加依赖
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>
<groupId>com.example</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configserver</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
添加注解
在工程启动类上添加@EnableConfigServer开启配置服务器功能
@EnableConfigServer
配置application.yml
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/shmilyah/cloud-config-samples.git
search-paths: '{application}'
label: master
server:
port: 8888
spring.cloud.config.server.git.uri:配置git仓库地址
spring.cloud.config.server.git.searchPaths:配置仓库路径,这里的配置会根据传入的应用名动态查找
由于我这里配置的 git 仓库为公开的,所以不需要配置用户名和密码,如果你配置的是私有仓库,还需要配置用户名和密码
spring.cloud.config.server.git.username 和spring.cloud.config.server.git.password
启动程序访问 http://localhost:8888/config-client/dev/master
将会显示
{“name”:“config-client”,“profiles”:[“dev”],“label”:“master”,“version”:“62a1d6b3d883e8378ed961a3a1566adfc48239b6”,“state”:null,“propertySources”:[{“name”:“https://github.com/shmilyah/cloud-config-samples.git/config-client/config-client-dev.yaml",“source”:{“hello”:"world”}}]}
http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
创建ConfigClient
按照之前的方法,选择client即可
配置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>
<groupId>com.example</groupId>
<artifactId>configclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configclient</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>Dalston.RC1</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
配置application.yml
server:
port: 8989
spring:
application:
name: con-client
cloud:
config:
uri: http://localhost:8888/
profile: dev
label: master
spring.cloud.config.profile: dev开发环境配置文件 test测试环境 pro正式环境
写一个api接口,返回读取的配置中心的值
@SpringBootApplication
@RestController
public class ConfigclientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigclientApplication.class, args);
}
@Value("${hello}")
private String hello;
@RequestMapping("/hh")
public String hh(){
return hello;
}
}
启动程序访问 http://localhost:8989/hh
将会显示:
world
真的十分十分的简单!照着做即可。希望大家好好的去学习。
参考文章 https://blog.youkuaiyun.com/hubo_88/article/details/80692156
https://blog.youkuaiyun.com/forezp/article/details/81041028