Spring Cloud配置中心客户端读取配置
微服务连接配置中心来实现外部配置的读取。
引入依赖
<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
推荐阅读
分享Java干货,高并发编程,热门技术教程,微服务及分布式技术,架构设计,区块链技术,人工智能,大数据,Java面试题,以及前沿热门资讯等。
Spring Cloud配置中心客户端读取配置的更多相关文章
- JAVA Spring Cloud 注册中心 Eureka 相关配置
转载至 https://www.cnblogs.com/fangfuhai/p/7070325.html Eureka客户端配置 1.RegistryFetchIntervalSecon ...
- springcloud费话之配置中心客户端(SVN)
目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...
- Spring Cloud config之一:分布式配置中心入门介绍
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...
- Spring Cloud(八):配置中心(服务化与高可用)【Finchley 版】
Spring Cloud(八):配置中心(服务化与高可用)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-04-26 | 本文接之前的<Spring Clou ...
- Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】
Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-04-24 | Spring Cloud Confi ...
- Spring Cloud第十一篇 | 分布式配置中心高可用
本文是Spring Cloud专栏的第十一篇文章,了解前十篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...
- Spring Cloud(九):配置中心(消息总线)【Finchley 版】
Spring Cloud(九):配置中心(消息总线)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-05-07 | 我们在 Spring Cloud(七):配置中心 ...
- Spring Cloud Config实现集群配置中心
Spring Cloud Config为分布式系统提供了配置服务器和配置客户端,可以管理集群中的配置文件.使用Git.SVN等版本管理系统存放配置文件,配置服务器会到版本管理系统获取配置,集群中的配置 ...
- Spring Cloud 系列之 Alibaba Nacos 配置中心
Nacos 介绍 Nacos 是 Alibaba 公司推出的开源工具,用于实现分布式系统的服务发现与配置管理.英文全称 Dynamic Naming and Configuration Service ...
随机推荐
- LeetCode6 dp
120. Triangle 我的解法用了一个二维数组,这样比较浪费空间.O(n*n) 但是标准答案自底向上,一是不需要进行特别判断,二是可以覆盖数组,则只需要O(n)的空间大小. class Solu ...
- 计算两个日期之间相差的天数(带带负数) 支持格式YYYY-mm-dd和YYYY-mm-dd HH:mm:ss
/** * 计算两个日期之间相差的天数(带带负数) 支持格式YYYY-mm-dd比较 * @param higDate 减数 * @param lowDate 被减数 * @returns 差值天数 ...
- HDFS学习笔记二
文章来源于:https://blog.csdn.net/xuejingfu1/article/details/52554174 文件写入staging(分阶段进行) 一个客户端的创建文件的请求并不直接 ...
- GeneXus笔记本—获取当月的最后一天
首先获取当前日期 然后赋值为当前年月的第一天 然后加一个月 减去一天 就是当月最后一天 多用于筛选数据时的条件或者区间 我们先随便拉个页面 简单点就好 放入两个textblock 然后点击Even ...
- php yield关键字以及协程的实现
php的yield是在php5.5版本就出来了,而在初级php界却很少有人提起,我就说说个人对php yield的理解 Iterator接口 在php中,除了数组,对象可以被foreach遍历之外,还 ...
- Spring Bean 的生命周期,如何被管理的
1. 实例化一个Bean,也就是我们通常说的new 2. 按照Spring上下文对实例化的Bean进行配置,也就是IOC注入 3. 如果这个Bean实现了BeanNameAware接口,会调用它实现的 ...
- 0001.第一个多线程demo--分批处理数据
public class UserEntity { private String userId; private String userName; public String getUserId() ...
- 安装JDK,并检测JDK是否安装成功
方法/步骤 首先,我们需要先安装好我们的JDK软件,安装好之后,我们需要对我们的电脑进行环境变量配置的设置,这样我们安装的JDK才能真正起到作用. 第一步:我们需要将光标放在“计算机”上面,然 ...
- python连接mongodb集群
一 安装模块pymongo pip3 install pymongo 二 创建一个MongoClient conn=MongoClient('mongodb://cbi:pass@ip1:20000, ...
- shell脚本学习(2)查找
1 grep 用法: grep -F fa 找含有fa字符的字符串 yuyuyu@ubuntu:~$ grep -F fa < do.txt grep -i fa 忽略大 ...