Spring Cloud Config接入
# Spring Cloud Config接入
微服务配置中心能帮助统一管理多个环境、多个应用程序的外部化配置,不需要在某个配置有改动或新增某个配置项时去多个节点上一个个修改,做到了一次改动,处处使用。这里和Eureka注册中心配合使用。
# Spring Cloud Config
SpringCloud子项目,提供了分布式系统中外部配置管理的能力,分为server
和client
两部分。官网地址 (opens new window)
# 配合git仓库使用
建立一个git仓库,将配置文件放在仓库里,配置文件格式应用名-profile.properties(yml),多服务的情况包名可根据服务前缀命名,在配置中心server端配置中增加search-path配置即可,比如多个包名app-1/app-2,可以配置search-path:app-*
# 服务端
# 依赖
<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>
2
3
4
5
6
7
8
9
10
# 配置文件
server:
port: 8077
spring:
application:
name: spring-cloud-config-center
cloud:
config:
server:
git:
uri: git仓库地址
username: 账号
password: 密码
search-paths: app-* #搜索路径,使用通配符
eureka:
client:
service-url:
defaultZone: http://admin:admin@127.0.0.1:19991/eureka/
fetch-registry-interval-seconds: 5
instance:
prefer-ip-address: true
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 启动类
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication.class, args);
}
}
2
3
4
5
6
7
8
# 服务端
# 依赖
<!--springcloud config客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
2
3
4
5
# 配置文件
spring:
application:
name: app-1
cloud:
config:
profile: @spring.profiles.active@
label: master
discovery:
enabled: true
service-id: spring-cloud-config-center
eureka:
client:
service-url:
defaultZone: http://admin:admin@127.0.0.1:19991/eureka/
2
3
4
5
6
7
8
9
10
11
12
13
14
提示
SpringCloudConfig配合服务发现使用时,必须在bootstrap.yaml(或环境变量)开启服务发现spring.cloud.config.discovery.enabled=true
,并且要配置注册中心的地址eureka.client.serviceUrl.defaultZone
,服务启动时会首先去注册中心找到配置中心server。
# 以下引自官网
# Discovery First Bootstrap (opens new window)
If you use a
DiscoveryClient
implementation, such as Spring Cloud Netflix and Eureka Service Discovery or Spring Cloud Consul, you can have the Config Server register with the Discovery Service. However, in the default “Config First” mode, clients cannot take advantage of the registration.If you prefer to use
DiscoveryClient
to locate the Config Server, you can do so by settingspring.cloud.config.discovery.enabled=true
(the default isfalse
). The net result of doing so is that client applications all need abootstrap.yml
(or an environment variable) with the appropriate discovery configuration. For example, with Spring Cloud Netflix, you need to define the Eureka server address (for example, ineureka.client.serviceUrl.defaultZone
). The price for using this option is an extra network round trip on startup, to locate the service registration. The benefit is that, as long as the Discovery Service is a fixed point, the Config Server can change its coordinates. The default service ID isconfigserver
, but you can change that on the client by settingspring.cloud.config.discovery.serviceId
(and on the server, in the usual way for a service, such as by settingspring.application.name
).