资讯专栏INFORMATION COLUMN

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

endiat / 2153人阅读

摘要:微服务连接配置中心来实现外部配置的读取。引入依赖配置中心客户端的依赖。增加启动类添加配置在中添加如下配置,必须是,中不行。配置文件参考如下配置读取配置使用就能读取配置中心的配置,当然也可以通过其他方式获取中的配置,参考之前系列文章。

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

引入依赖

    
        org.springframework.cloud
        spring-cloud-starter-eureka
    
    
        org.springframework.cloud
        spring-cloud-starter-config
    
    
        org.springframework.boot
        spring-boot-starter-aop
    
    
        org.springframework.retry
        spring-retry
    

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 最强技术教程

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71282.html

相关文章

  • Spring Cloud Consul 之Greenwich版本全攻略

    摘要:在我们的文档中,我们使用来表明就选举和事务的顺序达成一致。提供成员关系,故障检测和事件广播。这是一个允许请求的请求响应机制。这包括服务发现,还包括丰富的运行状况检查,锁定,键值,多数据中心联合,事件系统和。 转载请标明出处: http://blog.csdn.net/forezp/a...本文出自方志朋的博客 什么是Consul Consul是HashiCorp公司推出的开源软件,使...

    qingshanli1988 评论0 收藏0
  • Spring Cloud 配置中心内容加密

    摘要:从配置获取的配置默认是明文的,有些像数据源这样的配置需要加密的话,需要对配置中心进行加密处理。添加加密配置中心配置文件中加入加密密钥。 从配置获取的配置默认是明文的,有些像数据源这样的配置需要加密的话,需要对配置中心进行加密处理。 下面使用对称性加密来加密配置,需要配置一个密钥,当然也可以使用RSA非对称性加密,但对称加密比较方便也够用了,这里就以对称加密来配置即可。 1、安装JCE ...

    williamwen1986 评论0 收藏0
  • Spring Cloud 升级最新 Finchley 版本,踩了所有的坑!

    摘要:因为默认开启了所有攻击防御,需要禁用的防御。版本变化有点大,本次已成功升级了基础依赖,及注册中心配置中心。其他像代替了及其他组件再慢慢升级,的快速发展令升级变得非常蛋疼,本文记录了升级过程中踩过的所有的坑。。。 Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,现在一起为项目做一次...

    WelliJhon 评论0 收藏0
  • 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Co

    摘要:程序的入口类打开网址访问,网页显示这就说明,从获取了的属性,而是从仓库读取的如图本文源码下载四参考资料优秀文章推荐史上最简单的教程终章史上最简单的教程第一篇服务的注册与发现史上最简单的教程第七篇高可用的分布式配置中心 转载请标明出处: http://blog.csdn.net/forezp/a...本文出自方志朋的博客在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服...

    SQC 评论0 收藏0
  • 架构~微服务

    摘要:接下来继续介绍三种架构模式,分别是查询分离模式微服务模式多级缓存模式。分布式应用程序可以基于实现诸如数据发布订阅负载均衡命名服务分布式协调通知集群管理选举分布式锁和分布式队列等功能。 SpringCloud 分布式配置 SpringCloud 分布式配置 史上最简单的 SpringCloud 教程 | 第九篇: 服务链路追踪 (Spring Cloud Sleuth) 史上最简单的 S...

    xinhaip 评论0 收藏0

发表评论

0条评论

endiat

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<