spring cloud中统一配置Config使用
文章目录
一、简介
在微服务中,配置可以进行统一管理并提供配置服务,配置文件可以放在本地,也可放在如git等仓库中,通知更新可以客户端主动更新,也可以借助消息队列rabbitmq通知更新。借助消息队列rabbitmq,可以一次性通知所有客户端更新。
二、步骤
这里直接介绍使用git仓库存储配置信息,使用消息队列rabbitmq通知更新的步骤。使用前先安装并启动消息队列rabbit。
2.1 配置Config服务步骤
-
启动类添加开启配置服务注解@EnableConfigServer;
-
application.yaml文件中添加git仓库及rabbit配置信息:
spring: application: name: spring-cloud-config # profiles: # active: native #本地配置时使用此项 cloud: config: server: git: uri: https://github.com/xx/config-test searchPaths: config #指定配置文件目录 username: xx password: xx rabbitmq: host: 127.0.0.1 port: 5672 username: guest password: guest
-
在git仓库config目录下添加配置文件stu.properties(若采用本地配置,则在项目resources目录下添加配置文件)
-
配置更新时,可调用类似http://localhost:8017/actuator/bus-refresh地址通知客户端配置更新;
3.2 配置config客户端步骤
-
启动类上添加配置发现注解@EnableDiscoveryClient;
-
application.yaml文件添加服务发现相关配置:
spring: application: name: spring-cloud-client-config cloud: bus: enabled: true trace: enabled: true config: failFast: true rabbitmq: host: 127.0.0.1 port: 5672 password: guest username: guest
-
调用类使用@RefreshScope修饰,内使用如注解@Value等注入配置信息,使用方式同配置文件在本地一样;
-
可调用类似http://localhost:8053/actuator/refresh对当前客户端更新,也可使用类似http://localhost:8053/actuator/bus-refresh通知所有客户端配置更新;
三、示例
这里使用注册中心spring_cloud_register_center、配置服务spring_cloud_config、客户端spring_cloud_client_config进行示例。
3.1 注册中心
注册中心为spring_cloud_register_center项目。
3.1.1 maven依赖
<?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.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dragon.study</groupId>
<artifactId>spring_cloud_register_center</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_cloud_register_center</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<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-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>
3.1.2 application.yaml配置
server:
port: 8010
spring:
application:
name: spring-cloud-register-center
eureka:
client:
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/
fetchRegistry: false #是否拉取注册信息,由于注册中心是单个结点,不需要拉取
registerWithEureka: false #是否注册自己,由于当前就是注册中心,帮不需要注册
3.1.3 启动类
package com.dragon.study.spring_cloud_register_center;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudRegisterCenterApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudRegisterCenterApplication.class, args);
}
}
3.2 配置服务
配置服务为spring_cloud_config项目。
3.2.1 maven依赖
<?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.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dragon.study</groupId>
<artifactId>spring_cloud_config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_cloud_config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<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-config-server</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>
<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>
3.2.2 application.yaml配置
server:
port: 8017
spring:
application:
name: spring-cloud-config
# profiles:
# active: native #本地配置时使用此项
cloud:
config:
server:
git:
uri: https://github.com/xx/config-test
searchPaths: config #指定配置文件目录
username: xx
password: xx
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/
management:
endpoints:
web:
exposure:
include: "*"
3.2.3 添加配置文件
在https://github.com/xx/config-test仓库的config目录下添加配置文件stu.properties(若采用本地配置,则在项目resources目录下添加配置文件即可)。
3.3 客户端
客户端项目为spring_cloud_client_config。
3.3.1 maven依赖
<?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.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dragon.study</groupId>
<artifactId>spring_cloud_client_config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_cloud_client_config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-config</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.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
<version>2.2.3.RELEASE</version>
</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>
3.3.2 application.yaml配置
server:
port: 8053
spring:
application:
name: spring-cloud-client-config
cloud:
bus:
enabled: true
trace:
enabled: true
config:
failFast: true
rabbitmq:
host: 127.0.0.1
port: 5672
password: guest
username: guest
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/
management:
endpoints:
web:
exposure:
include: "*"
3.3.3 bootstrap.yaml配置
客户端需要在项目resources目录下添加bootstrap.yaml配置文件,内容如下:
eureka:
client:
service-url:
defaultZone: http://localhost:8010/eureka/ #在bootstrap.yaml文件中此项需要配置,否则不生效
spring:
cloud:
config:
discovery:
enabled: true
serviceId: spring-cloud-config
label: master
name: stu #配置文件名
3.3.4 调用配置服务相关类
定义调用controller:
package com.dragon.study.spring_cloud_client_config.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("stu")
@RefreshScope
public class StuController {
//注入远程配置中心配置,使用方式如本地配置一样
@Value("${stuId}")
private String stuId;
@RequestMapping("stuId")
public String stuId() {
return stuId;
}
}
定义启动类:
package com.dragon.study.spring_cloud_client_config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class SpringCloudClientConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudClientConfigApplication.class, args);
}
}
3.4 测试
3.4.1 常规测试
#可获取配置中心中配置的值
GET http://localhost:8053/stu/stuId
Accept: application/json
3.4.2 更新测试
配置中心中的配置值修改后。
配置服务端查看:
### 在配置中心主动通知所有客户端的配置更新
POST http://localhost:8017/actuator/bus-refresh
Accept: application/json
### 查看git上的配置
GET http://localhost:8017/stu-master.properties
Accept: application/json
客户端查看:
### 在客户端查询配置中心值
GET http://localhost:8053/stu/stuId
Accept: application/json
### 仅更新当前客户端的配置
POST http://localhost:8053/actuator/refresh
Accept: application/json
### 在客户端通知所有客户端配置更新
POST http://localhost:8053/actuator/bus-refresh
Accept: application/json