Config Server 可以从本地读取配置文件,也可以从远程 Git 读取配置文件。
从本地读取配置文件
本例采用 Maven 多模块结构,首先创建父项目 spring-cloud-config,使用的 Spring Boot 版本是 2.0.6.RELEASE,Spring Cloud 的版本是 Finchley.RELEASE,其 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.wuychn</groupId>
<artifactId>spring-cloud-config</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.cloud.version>Finchley.RELEASE</spring.cloud.version>
</properties>
<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>
</project>
在父项目下新建一个子项目,命名为 config-server,其 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">
<parent>
<artifactId>spring-cloud-config</artifactId>
<groupId>com.wuychn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
application.yml:
server:
port: 9007
spring:
cloud:
config:
server:
native:
search-locations: classpath:/config
profiles:
active: native
application:
name: config-server
在 application.yml 中,通过 spring.profiles.active=native 来配置 Config Server 从本地读取配置文件,读取配置的路径为 classpath 下的 config 目录。在 Config Server 工程的 src/main/resource 目录下,新建一个 config 文件夹,用于存放本地配置文件。在 config 目录下,新建一个 config-client-dev.yml 文件,用作 config-client 工程的配置文件,其内容如下:
server:
port: 9002
foo: foo v1
在 config-client-dev.yml 中,指定了 config-client 的端口为 9002,并定义了一个名为 foo 的变量。
config-server 的启动类如下:
package com.wuychn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
在程序启动类上通过 @EnableConfigServer 注解开启 Config Server 的功能。
在父项目下新建子项目 config-client,它将从 config-server 读取配置文件。config-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">
<parent>
<artifactId>spring-cloud-config</artifactId>
<groupId>com.wuychn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-client</artifactId>
<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>
</dependencies>
</project>
配置文件 bootstrap.yml:
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:9007
fail-fast: true
profiles:
active: dev
注意 config-client 的配置文件名是 bootstrap.yml,经过测试,如果写成 application.yml,那么会读取不到 config-server 中的配置文件,bootstrap.yml 相比 application.yml 有更高的优先级。在 config-client 的配置文件中,通过 spring.cloud.config.uri 指定 Config Serer 的地址,读取的配置文件名为 ${spring.application.name}-${spring.profiles.active}.yml,在本例中,为 config-client-dev.yml,如果没有读取成功,则执行快速失败(fail-fast)。
config-client 的启动类如下:
package com.wuychn;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ConfigClientApplication {
@Value("${foo}")
private String foo;
@GetMapping("/foo")
public String foo() {
return foo;
}
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
在 config-client 的启动类中,提供了一方法读取配置文件中的 foo 的值,如果读取成功,说明 config-client 从 config-server 读取配置文件成功。
启动 config-server、config-client,仔细观察 config-client 的启动日志,可以知道它向 config-server 读取了配置文件,最终启动的端口是 9002。
在浏览器中访问 http://localhost:9002/foo,结果如下:
foo v1
从远程 Git 仓库读取配置文件
Spring Cloud Config 可以从远程 Git 仓库读取配置文件,这样可以通过 Spring Cloud Bus 在不重启项目的情况下对 Config Client 的配置进刷新。
首先,修改 Config Server 的 application.yml 配置文件:
server:
port: 9007
spring:
cloud:
config:
server:
git:
uri: https://github.com/xxx/xxx
searchPaths: respo
username: xx
password: xx
label: master
application:
name: config-server
其中,uri 为 Git 仓库的地址,searchPaths 为搜索远程仓库的文件夹地址,username 和 passowrd 是 Git 仓库的登录名和密码,label 为 Git 仓库的分支名。
之后,将 config-client-dev.yml 上传到远程 Git 仓库中,启动 config-server、config-client,访问 http://localhost:9002/foo,结果如下:
foo v2
由此可见,config-server 从远程仓库读取了配置文件。
构建高可用的 Config-server
当服务实例很多时,所有的服务实例需要同时从配置中心读取配置文件,这时可以考虑将配置中心 config-server 做成一个微服务,并且将其集群化,从而达到高可用。
构建 Eureka Server
在 spring-cloud-config 下再新建一个子项目,命名为 eureka-server,作为服务注册中,其 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>
<artifactId>eureka-server</artifactId>
<parent>
<artifactId>spring-cloud-config</artifactId>
<groupId>com.wuychn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml:
server:
port: 9001
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
fetch-registry: false
register-with-eureka: false
serviceUrl:
defaultZone: http://localhost:9001/eureka/
程序启动类:
package com.wuychn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
改造 Config Server
Config-Server 作为 Eureka Client,首先需要在 pom 文件中添加 Eureka 的依赖:修改后的 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">
<parent>
<artifactId>spring-cloud-config</artifactId>
<groupId>com.wuychn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<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>
</dependencies>
</project>
然后,需要在启动类添加 @EnableEurekaClient 注解:
package com.wuychn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
最后,需要在 application.yml 配置文件中配置服务注册中心的地址:
server:
port: 9007
spring:
cloud:
config:
server:
git:
uri: https://github.com/xx/xx
searchPaths: respo
username: xx
password:
label: master
application:
name: config-server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9001/eureka/
改造 Config Client
Config Client 也是 Eureka Client,需要在 pom 文件中添加 Eureka 的依赖:
<?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">
<parent>
<artifactId>spring-cloud-config</artifactId>
<groupId>com.wuychn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-client</artifactId>
<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>
</dependencies>
</project>
启动类需要加上@EnableEurekaClient 注解:
package com.wuychn;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableEurekaClient
public class ConfigClientApplication {
@Value("${foo}")
private String foo;
@GetMapping("/foo")
public String foo() {
return foo;
}
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
最后,修改 application.yml,配置服务注册中心的地址,并指定向 serviceId 为 config-server 的配置中心服务读取配置文件:
spring:
application:
name: config-client
cloud:
config:
fail-fast: true
discovery:
enabled: true
serviceId: config-server
profiles:
active: dev
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9001/eureka/
依次启动 eureka-server、config-server 和 config-client,在浏览器中访问 http://localhost:9001/,可见 config-server 和 config-client 向服务注册中心注册成功:
随后访问 http://localhost:9002/foo,结果如下:
foo v3
可见,config-server 从远程 Git 读取了配置文件,config-client 从 config-server 读取了配置文件。
那么,如何实现高可用的 Config Server 呢?只需要将 Config Server 多实例部署即可。在 IDEA 中,可以在不同的端口启动多个 config-server 实例,之后多次启动 Config Client,可以看到 Config Client 会轮流从不同的 Config Server 实例读取配置文件,并且做了负载均衡。
使用 Spring Cloud Bus 刷新配置
为什么需要使用 Spring Cloud Bus 去刷新配置文件呢?如果有几十个甚至是几百个微服务,每个服务又有多个实例,当更改配置文件时,需要重新启动很多微服务实例,会非常麻烦。Spring Cloud Bus 的一个功能就是让这个过程变得简单,当远程 Git 仓库的配置更改后,只需要向某一个微服务实例发送一个 POST 请求,通过消息组件通知其他微服务实例重新拉取配置文件。
本例使用 RabbitMQ 作为 Spring Cloud 的消息组件去刷新微服务的配置文件,并基于前面的例子进行改造,只需要改造 config-client 工程。注意,我使用的是 Spring Boot 2.x 的版本,下面的配置和 Spring Boot 1.x 的配置差别很大。
首先,在 pom 文件中引入 RabbitMQ 实现的 Spring Cloud Bus 的起步依赖和 Actuator 的起步依赖:
<?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">
<parent>
<artifactId>spring-cloud-config</artifactId>
<groupId>com.wuychn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-client</artifactId>
<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.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-test</artifactId>
</dependency>
</dependencies>
</project>
然后,在 application.yml 配置文件中添加 RabbitMQ 的相关配置:
spring:
application:
name: config-client
cloud:
config:
fail-fast: true
discovery:
enabled: true
serviceId: config-server
profiles:
active: dev
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: bus-refresh
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9001/eureka/
其中,host 为 RabbitMQ 服务器的 IP 地址,port 为 RabbitMQ 服务器的端口,username 和 password 为 RabbitMQ 服务器的用户名和密码。这里要强调说明的一点,由于 Spring Boot 2.0 的 Actuator 只暴露了 /health、/info 两个端口(为了安全考虑),所以要配置 management.endpoints.web.exposure.include 的属性,可以指定为 *,暴露所有的端点。
最后,需要在启动类加上 @RefreshScope 注解,只有加上该注解,才会在不重启服务的情况下更新配置:
package com.wuychn;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableEurekaClient
@RefreshScope
public class ConfigClientApplication {
@Value("${foo}")
private String foo;
@GetMapping("/foo")
public String foo() {
return foo;
}
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
依次启动 eureka-server、config-server 和 config-client,在浏览器中访问 http://localhost:9002/foo,结果如下:
foo v3
将远程 Git 仓库中配置文件的 foo 值改为 foo v4,通过 Postman 发送一个 POST 请求 http://localhost:9002/actuator/bus-refresh,然后再访问 http://localhost:9002/foo ,结果如下:
foo v4
可见,通过 Spring Cloud Bus 刷新配置成功。由于使用了 Spring Cloud Bus,如果有多个服务,那么其他服务实例也会接收到刷新配置的消息,从而刷新配置。