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

引入依赖

<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

推荐阅读

干货:免费领取2TB架构师四阶段视频教程

面经:史上最全Java多线程面试题及答案

工具:推荐一款在线创作流程图、思维导图软件

分享Java干货,高并发编程,热门技术教程,微服务及分布式技术,架构设计,区块链技术,人工智能,大数据,Java面试题,以及前沿热门资讯等。

Spring Cloud配置中心客户端读取配置的更多相关文章

  1. JAVA Spring Cloud 注册中心 Eureka 相关配置

    转载至  https://www.cnblogs.com/fangfuhai/p/7070325.html Eureka客户端配置       1.RegistryFetchIntervalSecon ...

  2. springcloud费话之配置中心客户端(SVN)

    目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...

  3. Spring Cloud config之一:分布式配置中心入门介绍

    Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...

  4. Spring Cloud(八):配置中心(服务化与高可用)【Finchley 版】

    Spring Cloud(八):配置中心(服务化与高可用)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-04-26 |  本文接之前的<Spring Clou ...

  5. Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】

    Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-04-24 |  Spring Cloud Confi ...

  6. Spring Cloud第十一篇 | 分布式配置中心高可用

    ​ 本文是Spring Cloud专栏的第十一篇文章,了解前十篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

  7. Spring Cloud(九):配置中心(消息总线)【Finchley 版】

    Spring Cloud(九):配置中心(消息总线)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-05-07 |  我们在 Spring Cloud(七):配置中心 ...

  8. Spring Cloud Config实现集群配置中心

    Spring Cloud Config为分布式系统提供了配置服务器和配置客户端,可以管理集群中的配置文件.使用Git.SVN等版本管理系统存放配置文件,配置服务器会到版本管理系统获取配置,集群中的配置 ...

  9. Spring Cloud 系列之 Alibaba Nacos 配置中心

    Nacos 介绍 Nacos 是 Alibaba 公司推出的开源工具,用于实现分布式系统的服务发现与配置管理.英文全称 Dynamic Naming and Configuration Service ...

随机推荐

  1. 【Python—windows 下 virtualEnv 使用】

    用pip安装virtualenv pip3 install virtualenv 在相应的文件夹中创建一个独立的Python运行环境,命名为env. 之后会自动创建一个 env 文件夹,有: Incl ...

  2. [模板]网络最大流 & 最小费用最大流

    我的作业部落有学习资料 可学的知识点 Dinic 模板 #define rg register #define _ 10001 #define INF 2147483647 #define min(x ...

  3. 美团2018年CodeM大赛-资格赛

    https://www.nowcoder.com/acm/contest/138#question A.下单 水题…… B.可乐 题意:求期望 代码: #include<iostream> ...

  4. Unrecognized SSL message, plaintext connection--SSLSocket 代理服务器连接

    虽然java代码  URL.openconnect(proxy);已经实现了https客户端通过代理连接服务器 但个人在使用socket https代理http://www.cnblogs.com/h ...

  5. vue 项目中使用阿里巴巴矢量图标库iconfont

    原文:https://www.jianshu.com/p/38262f18eee2 1.打开iconfont阿里巴巴官网https://www.iconfont.cn 2.新建项目(这样方便后期维护图 ...

  6. react-native学习(一)————使用react-native-tab-navigator创建底部导航

    使用react-native-tab-navigator创建底部Tab导航 1.使用npm安装react-native-tab-navigator npm install react-native-t ...

  7. Qt 【widget如何铺满窗口】

    刚接触qt不是很长时间,都是使用ui拖拽控件实现界面,然后发现有些问题就是控件一旦多了起来,拖拽就不好控制了,然后就转而使用纯代码开发. 一下是碰到第一个问题: 创建一个MainWidget; Mai ...

  8. eclipse设置tomcat部署目录地址

    参考: https://blog.csdn.net/lvyuan1234/article/details/53418818 右键,open 操作前提是所有项目移除,并且右键clean掉相关数据! 修改 ...

  9. 关于云计算三大服务模式LAAS,PAAS,SAAS的含义及区别

    根据NIST的权威定义,云计算有SPI,即SAAS,PAAS和LAAS三大服务模式,上层是SAAS,中间层是PAAS,底层是LAAS,一层支撑一层. LAAS(Infrastucture-as-a-S ...

  10. Halo(一)

    @EnableJpaAuditing 审计功能(启动类配置) 在实际的业务系统中,往往需要记录表数据的创建时间.创建人.修改时间.修改人. 每次手动记录这些信息比较繁琐,SpringDataJpa 的 ...