config 配置中心
yml 配置文件保存到 git 服务器,例如 github.com 或 gitee.com
微服务启动时,从服务器获取配置文件
github 上存放配置文件
新建 “Project”,命名为 config
将sp02,sp03,sp04,sp11四个项目的yml配置文件,复制到config项目,并改名
- item-service-dev.yml
- user-service-dev.yml
- order-service-dev.yml
- zuul-dev.yml
最后,清空四个项目中的application.yml
文件
禁止配置中心的配置信息覆盖客户端配置
默认配置中心配置优先级高,配置中心配置会覆盖客户端的所有配置,包括命令行参数配置,这样我们在item-service和order-service中配置的端口号启动参数会无效
item-service 启动参数:
--service.port=8001
--service.port=8002
order-service 启动参数
--service.port=8201
--service.port=8202
可以设置禁止配置中心的配置将客户端配置覆盖掉,这样当我们从控制中心拉取配置文件后,本地的配置会覆盖拉去来的配置
在四个配置文件中添加下面的配置
spring:
......
cloud:
config:
override-none: true
准备仓库
- 新建module: config,当做一个文件夹,用来存放配置文件
- 把 2,3,4,11 项目的配置文件,放到 config 文件夹
springcloud1 工程目录创建本地仓库
- VCS - Import into version control - Create git repository
- 选择 springcloud1 工程目录设置成本地仓库
把本地仓库提交推送到gitee远程仓库
- ctrl + k 或 VCS - commit
- 勾选要提交的文件,填写提交信息,点击提交
- ctrl+shift+k 或 VCS - git - push
- 点击左上角 define remote
Config 服务器
config 配置中心从 git 下载所有配置文件。
而其他微服务启动时从 config 配置中心获取配置信息。
新建 sp12-config 项目
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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tedu</groupId>
<artifactId>sp12-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp12-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<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>
<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>
application.yml
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/Mjp3309/springcloud1
search-paths: config
#私有仓库需要配置用户名密码
#username:
#password:
server:
port: 6001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
主程序添加 @EnableConfigServer
和 @EnableDiscoveryClient
package cn.tedu.sp12;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer//启动配置中心服务
@EnableDiscoveryClient
@SpringBootApplication
public class Sp12ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(Sp12ConfigApplication.class, args);
}
}
启动,访问测试
访问 item-service-dev.yml 可以使用以下形式:
http://localhost:6001/item-service-dev.yml
http://localhost:6001/item-service/dev
测试其他文件
http://localhost:6001/user-service/dev
http://localhost:6001/zuul/dev
http://localhost:6001/order-service/dev
config 客户端
修改以下项目,从配置中心获取配置信息
- sp02-itemservice
- sp03-userservice
- sp04-orderservice
- sp11-zuul
pom.xml 添加 config 客户端依赖
右键点击项目,编辑起步依赖,添加 config client 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
在四个项目中添加 bootstrap.yml
bootstrap.yml
,引导配置文件,先于 application.yml 加载
- item-service
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: item-service
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
- user-service
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: user-service
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
- order-service
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: order-service
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
- zuul
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: zuul
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
启动服务,观察从配置中心获取配置信息的日志(先启动配置中心和注册中心)
配置刷新
spring cloud 允许运行时动态刷新配置,可以重新从配置中心获取新的配置信息
以 user-service
为例演示配置刷新
pom.xml
user-service 的 pom.xml 中添加 actuator 依赖
右键点击sp03-user-service
项目,编辑起步依赖,添加 actuator 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。