This article is SpringCloud combat (f) - a highly available distributed configuration center (Spring Cloud Config), to focus on the first article, please click the portal:
SpringCloud combat (five) - Routing Gateway (zuul)
We previously introduced the Zuul routing gateways. With the increasing work in business, demand for increasingly complex, we need to split a lot of micro-business service module division, this time each module has its own configuration file, at this time if there are 10,000 modules, we have 1 ten thousand configuration files, so we need to configure a center for unified configuration file management, SpringCloud provided Spring Cloud Config to configure file management for us, here I do not nonsense, too lazy to write to open a single node of Spring Cloud Config the article (with a single node basically nothing), direct write Spring Cloud Config cluster structures, the organization chart below:
A, Spring Cloud Config Introduction
Provide server and client support Spring Cloud Config configured as an external distributed system. Use Config Server, you can manage its external properties for applications in all environments. It is ideal for spring application, the application can also be used in other languages. With the application through the process from development to testing and production deployment, you can manage the configuration between these environments and to determine the application has everything you need to run the migration. The default storage backend server implementations use git, so it is easy to support label version of the configuration environment, and can access a variety of tools for managing content.
Second, prepare for work
Based on the foregoing be set up in the project, first start Eureka clusters, ports are 8759,8760,8761, because we want to achieve real-time refresh Client-side configuration files, so we need to install the messaging middleware, here I choose rabbitMQ.
rabbitMQ installation tutorial
rabbitMQ Configure Remote Access
After rabbitMQ need to configure remote access port to connect to (my remote access port configuration is 5672) in the program.
Third, create a config-server project
Create a new spring-boot project, named: config-server.
3.1 maven dependence
<?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>com.oal.microservice</groupId>
<artifactId>openAiLab</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.oal.microservice</groupId>
<artifactId>config-server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config-server</name>
<description>config</description>
<dependencies>
<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-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
We need to introduce spring-cloud-config-server, spring-cloud-starter-bus-amqp, where we introduced the Bus message bus, the purpose is to be able to notify the client to MQ pulls the configuration files, so as to achieve the update client object configuration file, in which case the structure is shown below:
3.2 configuration file (application.yml)
eureka:
client:
serviceUrl:
defaultZone: http://eureka-serve-01:8761/eureka/
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/AndyWebJava/SpringcloudConfig
searchPaths: respo
label: master
username:
password:
bus:
enabled: true
trace:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: bus-refresh
Note that we need to expose bus-refresh Interface Bus in, we need to update the configuration file by bus.
spring.cloud.config.server.git.uri is git repository address
spring.cloud.config.server.git.searchPaths is the path configuration file
spring.cloud.config.server.git.label configuration file located branch
spring.cloud.config.server.git.username, spring.cloud.config.server.git.password if it is a total warehouse, you can fill in the blank.
management.endpoints.web.exposure.include = bus-refresh is to expose Bus config-server configuration file of bus-refresh refresh port, so we can go to refresh client configuration file by Postman, and note that I am only here let config-server configuration file to do the work refreshed.
3.3 start classes (ConfigServerApplication)
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
Start config-server, port 8888, and we set up a server to complete, then activates a plurality of high availability config-server instance (different ports) to register the Eureka cluster, as shown:
Fourth, create config-client project
Create a new spring-boot project, named: config-client.
4.1 maven dependence
<?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>com.oal.microservice</groupId>
<artifactId>openAiLab</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.oal.microservice</groupId>
<artifactId>config-client</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config-client</name>
<description>config</description>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.2 configuration file (bootstrap.yml)
spring:
profiles:
active: ${CONFIG_ACTIVE:dev}
---
spring:
profiles: dev
application:
name: config-client
cloud:
config:
label: master
profile: dev
discovery:
enabled: true
service-id: config-server
bus:
enabled: true
trace:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
server:
port: 8881
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
注意这里配置文件必须是以bootstrap.*开头,不然无法从服务端获取到配置信息。
spirng.config.config.label 是资源文件所在分支,它默认会去git上找文件名为config-client-dev.*的文件进行读取,这里我们可以通过判断不同的运行环境来读取到不同环境的配置文件。
spring.cloud.config.profile 是运行环境,用来区分开发、测试、生产环境。
spring.cloud.config.discovery.service-id 是配置中心服务端实例名。
spring.profiles.active= ${CONFIG_ACTIVE:dev} ,这种写法会从环境变量中读取key为CONFIG_ACTIVE的值,如果环境变量中找不到就直接去默认值,这里默认值是dev。
4.3 启动类(ConfigClientApplication)
@SpringBootApplication
@EnableDiscoveryClient
@RefreshScope
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${foo}")
String foo;
@RequestMapping(value = "/hi")
public String hi() {
return foo;
}
}
我们需要在客户端的启动类上添加@RefreshScope注解,表示当配置改变的时候进行刷新。
启动config-client工程,访问 http://localhost:8881/hi,浏览器返回结果:
foo version 1
然后我们改变git仓库中config-client-dev.properties文件中的foo值为50,访问 http://localhost:8888/actuator查看config-server端暴露出来的对外端口,如图所示:
我们可以看到全局配置文件的刷新地址是
http://localhost:8888/actuator/bus-refresh
我们可以通过指定{destination}来进行局部配置文件刷新,例如 http://localhost:8888/actuator/bus-refresh/config-client*
http://localhost:8888/actuator/bus-refresh/{destination}
Then we open access Postman Post by the way HTTP: // localhost: 8888 / Actuator / Bus-Refresh / config-Client * , as shown:
After the success of a web browser. HTTP: // localhost: 8881 / hi , return results
foo version 50
It explained that it had successfully broken the Client-side configuration file.
Highly available distributed configuration center (Spring Cloud Config) completed structures.