微服务配置中心是干啥的_Spring Cloud微服务获取配置中心Config Server配置信息

本文介绍如何在Spring Cloud微服务中配置并使用Config Client从Config Server获取配置信息。首先,在pom.xml中引入相关依赖,然后在application.yml和bootstrap.yml中设置配置。启动类ConfigClientController通过@Value注解获取Git仓库配置文件中的属性。测试时,成功从Config Server获取到Git仓库中对应的环境配置。
摘要由CSDN通过智能技术生成

前文《Spring Cloud Config里使用Git作为后端存储来开发ConfigServer》已经介绍了如何配置Config Server配置中心,本文将介绍如何在Spring Cloud微服务获取配置中心Config Server配置信息。

一、创建一个新的微服务,整合Config Client

1.pom文件

deviceservice-config-client

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-config

1

2

3

4

5

6

7

8

9

10

11

12

13

14

deviceservice-config-client

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-config

2.配置文件application.yml

server:

port: 8081

1

2

server:

port:8081

3.配置文件bootstrap.yml

spring:

application:

# 对应config server所获取的配置文件的{application}

name: deviceservice-foo

cloud:

config:

uri: http://localhost:8080/

# profile对应config server所获取的配置文件中的{profile}

profile: dev

# 指定Git仓库的分支,对应config server所获取的配置文件的{label}

label: master

1

2

3

4

5

6

7

8

9

10

11

spring:

application:

# 对应config server所获取的配置文件的{application}

name:deviceservice-foo

cloud:

config:

uri:http://localhost:8080/

# profile对应config server所获取的配置文件中的{profile}

profile:dev

# 指定Git仓库的分支,对应config server所获取的配置文件的{label}

label:master

spring.application.name:对应Config Server 所获取的配置文件中的{application}

spring.cloud.config.uri:指定Config Server 的地址,默认是http://localhost:8888

spring.cloud.config.profile:profile对应Config Server所获取的配置文件中的{profile}

spring.cloud.config.label:指定Git仓库的分支,对应Config Server所获取配置文件的{label}

需要注意的是,以上属性必须配置在bootstrap.yml文件中,而不是application.yml。具体原因参看这篇文章《SpringBoot中配置文件bootstrap和application的区别比较》。

4.ConfigClientController类

@RestController

public class ConfigClientController {

@Value("${profile}")

private String profile;

@GetMapping("/profile")

public String hello() {

return this.profile;

}

}

1

2

3

4

5

6

7

8

9

10

@RestController

publicclassConfigClientController{

@Value("${profile}")

privateStringprofile;

@GetMapping("/profile")

publicStringhello(){

returnthis.profile;

}

}

5.启动类

@SpringBootApplication

public class ClientApplication {

public static void main(String[] args) throws Exception {

SpringApplication.run(ClientApplication.class, args);

}

}

1

2

3

4

5

6

@SpringBootApplication

publicclassClientApplication{

publicstaticvoidmain(String[]args)throwsException{

SpringApplication.run(ClientApplication.class,args);

}

}

在Controller中,通过注解@Value(“${profile}”),绑定Git仓库配置文件中的profile属性。

二、测试

1.启动deviceservice-config-server(前文所编写)

2.启动deviceservice-config-client

3.访问http://localhost:8081/profile,可获得如下结果

说明Config Client能够正常通过Config Server获得Git仓库中对应环境的配置。

浏览量:

1,087

0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值