码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Cloud构建客户端

时间:2017-11-15 10:52:23      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:bsp   ref   客户端   div   配置文件   配置参数   name   default   oid   

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

  • 创建一个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架构的搭建过程及如何运用于企业项目。源码来源

Spring Cloud构建客户端

标签:bsp   ref   客户端   div   配置文件   配置参数   name   default   oid   

原文地址:http://www.cnblogs.com/abbie133/p/7837452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!