SpringCloud之七config(配置服务中心)

简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。

新建config-server

引入相关依赖

1
2
3
4
5
6
7
8
9
10
 <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>

新建bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server:
port: 8899

spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/felixjee/SpringCloudConfig.git
password:
username:
default-label: master
search-paths:


eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.port}
client:
service-url:
defaultZone: http://localhost:8761/eureka/
  • spring.cloud.config.server.git.uri:配置git仓库地址
  • spring.cloud.config.server.git.search-paths:配置仓库路径
  • spring.cloud.config.default-label:配置仓库的分支
  • spring.cloud.config.server.git.username:访问git仓库的用户名
  • spring.cloud.config.server.git.password:访问git仓库的用户密码

启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14

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);
}
}

在github Create a new repository 取名SpringCloudConfig
创建userservice-dev.yml 文件并上传到github SpringCloudConfig项目中

1
2
3
4
5
6
7
8

user: user1
server:
port: 8789

spring:
application:
name: config-client:${server.port}

访问http://localhost:8899/userservice/dev

config-server
可以看到已经从远程获取到配置信息

http请求地址和资源文件映射如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

新建config client

引入相关依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   <dependencies>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
</dependencies>

bootstarp.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spring:
application:
name: config-client
cloud:
config:
label: master #对应git的分支。如果配置中心使用的是本地存储,则该参数无用
profile: dev #对应{profile}部分
name: userservice #对应{application}部分
#单个配置
uri: http://localhost:8888/


eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: ${spring.application.name}:${server.port}
prefer-ip-address: true

新建controller 测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
@Value("${user}")
private String user;
@Value("${server.port}")
private String port;
@GetMapping
public String user(){
return "user: "+user+" port: "+port;
}
}

访问 http://localhost:8789/
显示结果如下:

user: user1 port: 8789

说明成功通过配置中心获取到配置信息

刷新

config-client引入

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

config-client application.yml 添加

1
2
3
4
5
6
7
8
9
10
11
12
management:
#refresh接入点显式暴露出来
endpoints:
web:
exposure:
include: refresh,health,info,bus-refresh #指定监控项。默认开启了health、info。我们这里加入了refresh
base-path: /actuator # 用于指定actuator的URL路径。1.X默认是/,2.X默认是/actuator
endpoint:
health:
show-details: always # 用于展示监控项详细信息,默认不展示
server:
port: 8988 #用于指定actuator端口。如果不指定则与系统服务端口一致,建议修改

修改配置文件
user:user4
访问 http://localhost:8789/
没有发生变化

使用
curl -v -X POST http://localhost:8983/actuator/refresh
或者使用postman

config

再次访问 http://localhost:8789/

结果 user: user4 port: 8789

手动刷新成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值