鉴于每个微服务基本对应着一个配置文件,假如每次更新逐个去修改配置文件,会给开发人员以及后期维护带来极大的不便,同时还容易出错,因此在Spring Cloud中引入了分布式配置中心组件——Spring Cloud Config。
它的实现方式有三种,分别为本地,数据库以及git。本篇介绍的是git的实现方式。
简单来说,就是在git创建一个专门管理配置文件的仓库,里面包含各个微服务的配置文件,在微服务启动的时候自动到git仓库拉取对应的配置文件读取。
(一)创建一个config-server配置中心服务器
创建完之后,在子pom.xml中引入以下依赖,同时还需要依赖spring-cloud-starter-netflix-eureka-client
去支持各个服务从注册中心读取配置服务器的信息:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
然后创建一个application启动类:
package com.ningmeng.configserver;
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;
@SpringBootApplication
@EnableConfigServer //开启配置中心
@EnableDiscoveryClient // 将config-server注册到配置的服务注册中心
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
接着配置application.yml文件(这里用的是SpringCloudConfig的默认端口号:8888):
#工程端口号
server:
port: 8888
#eureka注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#配置rabbitmq信息,git的配置仓库地址、账号以及密码(不设置权限时可不配置账号密码,或者可以对账号密码加密与解密)
spring:
application:
name: config-server #工程名
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
cloud:
config:
server:
git:
uri: https://gitee.com/huijunzeng/configServer
search-paths: /config-rep #文件路径
最后登录码云,根据配置文件的git信息,创建一个configServer仓库,在config-rep文件夹下分别新建config.properties、config-dev.properties、config-prod.properties和config-test.properties四个文件。
内容分别对应from=git-1.0、from=git-dev-1.0、from=git-prod-1.0和from=git-test-1.0
然后依次启动eureka-server、config-server工程,配置文件的内容的访问方式如下:
-
/{application}/{profile}[/{label}]
-
/{application}-{profile}.yml
-
/{label}/{application}-{profile}.yml
-
/{application}-{profile}.properties
-
/{label}/{application}-{profile}.properties
这里举例说明properties文件(yml文件同理),比如config-dev.properties,application为config,profile为dev,假如-后面不接其他后缀时,则profile默认为default,label为分支名称,不写时默认为master
本工程中,在gitee码云的configServer仓库的config-rep文件夹下配置了config.properties以及config-dev.properties,那么可通过
访问http://localhost:8888/config/default获取config.properties的内容,而config-dev.properties可通过http://localhost:8888/config/dev访问,其他访问方式还有http://localhost:8888/config-dev.properties,都能拿到相应的文件内容。
以上操作证明搭建config-server配置服务器成功了!
(二)创建一个config-client配置客户端
所有需要从config-server配置服务器拉取配置信息的微服务,都需要依赖spring-cloud-starter-config
,由于父pom.xml中已经引入了一些公共依赖,所以这里只需要单独添加spring-boot-starter-web
依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
然后创建一个application启动类:
package com.ningmeng.configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient // 将服务注册到配置的服务注册中心
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
接着配置bootstrap.yml文件(注意名字是bootstrap,bootstrap.yml优于application.yml加载,即在项目启动那瞬间就加载,然后拉取配置信息之后初始化到项目中):
#####################以下为不引入eureka的基础版的配置########################
#spring:
# cloud:
# config:
# name: config-client
# uri: http://localhost:8888/
# profile: default
# label: master
##############################################################################
######################以下为引入eureka的高可用版的配置########################
####配置属性的说明
#spring.cloud.config.name:对应配置文件规则中的{application}部分
#spring.cloud.config.profile:对应配置文件规则中的{profile}部分
#spring.cloud.config.label:对应配置文件规则中的{label}部分
#spring.cloud.config.uri:配置中心config-server的地址
spring:
cloud:
config:
name: config-client
profile: default # 用来定位Git中的资源
label: master # 读取的分支
discovery:
service-id: config-server # 参数来指定Config Server注册的服务名
enabled: true # 参数设置为true,开启通过服务来访问Config Server的功能
################################################################################
配置文件包含两部分内容:
一部分是不通过从Eureka注册中心查找config-server服务的service-id的方式去拉取,也就是可以不添加注解@EnableDiscoveryClient
,这部分请自行放开注释测试;
另外一部分就是将config-server服务注册到Eureka注册中心,从而提供给各微服务通过config-server配置服务器的serviceId去拉取配置信息,以达到高可用(大多采用这种方式)。
另外,假如同时存在一个本地application.yml配置文件和一个远程bootstrap.yml文件,默认会优先读取bootstrap.yml(加载顺序决定)
接着我们需要在gitee码云先创建一个config-client工程的配置文件config-client.yml,并编辑一下内容保存:
最后写一个controller接口去测试:
package com.ningmeng.configclient.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${from}")
String from;
@Value("${server.port}")
String port;
@RequestMapping("/getRomteConfigurationFromGit")
public String getRomteConfigurationFromGit(){
return from + "-------" + port;
}
}
分别依次启动eureka-server、config-server以及config-client工程,访问http://localhost:6001/getRomteConfigurationFromGit,可看到成功读取了git仓库中的config-client.yml文件的配置信息的内容:
必须要在eureka-server注册中心启动完之后,接着启动config-server配置服务器,不然启动其他服务获取配置服务器中的信息报错找不到实例config-server:
java.lang.IllegalStateException: No instances found of configserver (config-server)
同理,我们也可以配置其他服务的远程配置文件,这里不再累述!
本篇完结!
github代码地址:https://github.com/huijunzeng/springCloudDemo.git