Spring Cloud 配置中心客户端读取配置

算法听星使
• 阅读 2873

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

引入依赖

<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:这两个是连接配置中心快速失败和重试需要用到的依赖。

增加启动类

@EnableDiscoveryClient
@SpringBootApplication
public class ServiceApplication {

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

}

添加配置

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

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配置文件参考如下:

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配置

... 

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

...

读取配置

@RestController
public class TestController {

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

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

启动服务

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

spring-boot:run -Drun.profiles=config-client1 -P dev
spring-boot:run -Drun.profiles=config-client2 -P dev

推荐:Spring Boot & Cloud 最强技术教程

扫描关注我们的微信公众号,干货每天更新。

Spring Cloud 配置中心客户端读取配置

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
3年前
ELK学习笔记之配置logstash消费kafka多个topic并分别生成索引
0x00 filebeat配置多个topicfilebeat.prospectors:input_type:logencoding:GB2312fields_under_root:truefields:添加字段
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Easter79 Easter79
3年前
SpringCloud配置中心——ConfigServer搭建
_在实际应用中,一个系统的每个微服务都会有相同的配置,如数据库配置信息等等。为了将每个微服务的公共配置可以抽取出来。SpringCloud提供了Config配置中心的配置,作为配置中心(ConfigServer),提供给微服务(ConfigClient)读取并且加载配置。使用git仓库存放配置文件,SpringCloudConfig读取到之后会自
Easter79 Easter79
3年前
SpringCloud配置中心高可用搭建
本文通过configserver连接git仓库来实现配置中心,除了git还可以使用svn或者系统本地目录都行。引入依赖<dependencies<dependency<groupIdorg.springframework.cloud</groupId<artifactIdsp
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Java服务总在半夜挂,背后的真相竟然是... | 京东云技术团队
最近有用户反馈测试环境Java服务总在凌晨00:00左右挂掉,用户反馈Java服务没有定时任务,也没有流量突增的情况,Jvm配置也合理,莫名其妙就挂了
Nacos 配置中心源码 | 京东物流技术团队
客户端入口在引入配置中心maven依赖的jar文件中找到springcloudstarteralibabanacosconfig2.2.5.RELEASE.jar!/METAINF/spring.factories,在该配置文件找到NacosConfigB
美凌格栋栋酱 美凌格栋栋酱
5个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(