0%

SpringCloud Config-client

pom.xml

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

application.yml

1
2
3
4
5
6
server:
port: 7007

spring:
application:
name: config-client

bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
spring:
cloud:
config:
# 对应{application}部分
name: spring-cloud-config
# 对应{profile}部分
profile: pro
# 配置中心的具体地址
uri: http://localhost:7006/
# 对应git的分支。如果配置中心使用的是本地存储,则该参数无用
label: master
discovery:
# 指定配置中心的service-id,便于扩展为高可用配置集群。
service-id: config-server

ConfigClientController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.linjian.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* ConfigClientController
*
* @author jlin
* @date 2019/3/20 14:42
* @Description
*/
@RestController
public class ConfigClientController {
@Value("${environment}")
String environment;

@RequestMapping(value = "/environment")
public String environment() {
return environment;
}
}

测试:

  1. 启动config-server、config-client

  2. 访问 http://localhost:7007/environment