Spring Cloud构建客户端

在完成了上述验证之后,确定配置服务中心已经正常运作,下面我们尝试如何在微服务应用中获取上述的配置信息。

  • 创建一个Spring Boot应用,命名为config-client,并在pom.xml中引入下述依赖:
     
     
1
2
3
4
5
6
7
8
9
10
     
     
<dependencies>
<dependency>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-web </artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud </groupId>
<artifactId>spring-cloud-starter-config </artifactId>
</dependency>
</dependencies>
  • 创建Spring Boot的应用主类,具体如下:
     
     
1
2
3
4
5
6
7
8
     
     
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web( true).run(args);
}
}
  • 创建bootstrap.yml配置,来指定获取配置文件的config-server-git位置,例如:
     
     
1
2
3
4
5
6
7
8
9
10
11
     
     
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost: 1201/
profile: default
label: master
server:
port: 2001

上述配置参数与Git中存储的配置文件中各个部分的对应关系如下:

  • spring.application.name:对应配置文件规则中的{application}部分
  • spring.cloud.config.profile:对应配置文件规则中的{profile}部分
  • spring.cloud.config.label:对应配置文件规则中的{label}部分
  • spring.cloud.config.uri:配置中心config-server的地址

这里需要格外注意:上面这些属性必须配置在bootstrap.properties中,这样config-server中的配置信息才能被正确加载。

在完成了上面你的代码编写之后,读者可以将config-server-git、config-client都启动起来 我们可以看到该端点将会返回从git仓库中获取的配置信息:

     
     
1
2
3
     
     
{
"profile": "default"
}

另外,我们也可以修改config-client的profile为dev来观察加载配置的变化。

从现在开始,我这边会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,希望可以帮助更多的好学者。大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。源码来源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值