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 ...
随机推荐
- javaIO流(三)--IO深入操作
一.字符编码 在计算机的世界中,本质上只认识0,1的字节数据,如果要想描述一些文字的编码就需要对这些二进制的数据进行组合,就需要对二进制的数据进行组合,所以才有了现在可看见的中文显示,但是在进行编码的 ...
- 组件化框架设计之阿里巴巴开源路由框架——ARouter原理分析(一)
阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 背景 当项目的业务越来越复杂,业务线越来越多的时候,就需要按照业 ...
- 静态链表 C++版
笔记静态链表的实现 #include "stdafx.h" #include<iostream> using namespace std; #define MAXSIZ ...
- 【摘】Linux虚拟地址空间布局以及进程栈和线程栈总结
在CSDN上看到的一篇文章,讲的还是满好的. 原文地址:Linux虚拟地址空间布局以及进程栈和线程栈总结 一:Linux虚拟地址空间布局 (转自:Linux虚拟地址空间布局) 在多任务操作系统中,每个 ...
- Hibernate4教程二:基本配置
可编程的配置方式一: 如果在配置cfg.xml的时候,不想在里面配置hbm.xml怎么办呢?可在程序里使用可编程的配置方式,也就是使用程序来指定在cfg.xml里面的配置信息,不推荐这种方式.如下: ...
- 面试题:实现call、apply、bind
面试题:实现call.apply.bind 实现bind module.exports = function(Tcontext, ...args) { let globalThis = typeof ...
- openface人脸识别框架
openface的githup文档地址:http://cmusatyalab.github.io/openface/ openface的安装: 官方推荐用docker来安装openface,这样方便快 ...
- 个人公众号服务端开发Demo
公众号出来很久了,也可以个人申请.知道公众号的服务端开发其实很简单,接口调用封装,数据存取,不外如是. 人一旦懒了,真的是 “无可救药” 了...现简单描述晚到的公众号HelloWorld 思路 公众 ...
- 五、bootstrap-fileinput
一.bootstrap-fileinput <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- 六、SpringBoot配置@ConfigurationProperties与@Value区别
1.@Value的使用 三种用法 // ${key} 从环境变量.配置文件中取值 @Value("${person.last-name}") private String last ...