前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud配置中心客户端读取配置

SpringCloud配置中心客户端读取配置

作者头像
Java技术栈
发布2018-03-30 15:02:10
1.8K0
发布2018-03-30 15:02:10
举报
文章被收录于专栏:Java技术栈Java技术栈

微服务连接配置中心来实现外部配置的读取。

引入依赖

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>
</dependencies>

spring-cloud-starter-config:配置中心客户端的依赖。

spring-boot-starter-aop, spring-retry:这两个是连接配置中心快速失败和重试需要用到的依赖。

增加启动类

代码语言:javascript
复制
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }

}

添加配置

bootstrap.yml中添加如下配置,必须是bootstrap,application中不行。

代码语言:javascript
复制
spring: 
  application: 
    name: config-client
  cloud:
    config:
      #username: 
      #password: 
      name: ${git.application}
      profile: ${git.profile}
      label: ${git.label}
      fail-fast: true
      retry:
        initial-interval: 2000
        max-attempts: 5
      discovery: 
        enabled: true
        service-id: config-center 

eureka:
  client: 
    serviceUrl:
      defaultZone: ${register-center.urls}

可以看出配置比较简单,下面也不再详述。

application.yml配置文件参考如下:

代码语言:javascript
复制
spring: 
  profiles: 
    active: config-client1

eureka:
  instance:
    prefer-ip-address: true  
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    lease-expiration-duration-in-seconds: ${lease-expiration-duration-in-seconds}
    lease-renewal-interval-in-seconds: ${lease-renewal-interval-in-seconds}

---
spring: 
  profiles: config-client1

server: 
  port: ${config-client1.server.port}

---
spring: 
  profiles: config-client2

server: 
  port: ${config-client2.server.port}

Maven filter配置

代码语言:javascript
复制
... 

#git
git.application=application
git.profile=dev
git.label=master

...

读取配置

代码语言:javascript
复制
@RestController
public class TestController {

    @Value("${username}")
    private String username;

...

使用Value就能读取配置中心的配置,当然也可以通过其他方式获取SpringCloud中的配置,参考之前SpringBoot系列文章。

启动服务

通过指定Profile启动两台微服务,它们可以读取配置中心的内容。

代码语言:javascript
复制
spring-boot:run -Drun.profiles=config-client1 -P dev
spring-boot:run -Drun.profiles=config-client2 -P dev
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java技术栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引入依赖
  • 增加启动类
  • 添加配置
  • Maven filter配置
  • 读取配置
  • 启动服务
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档