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

引入依赖

<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面试题,以及前沿热门资讯等。

最新文章

  1. 详解微信开发者文档——5 access_token管理
  2. java jar包收集
  3. 偶然的发现(与Code无关)
  4. thinkphp多语言设置
  5. eclips中增加对jar包的引用
  6. ylbtech-Model-Account(通用账户模块设计)
  7. Codevs No.1287 矩阵乘法
  8. Ubuntu 安装之后的配置博文总结
  9. .NET中的线程与异步(笔记)
  10. didLoadFromCCB方法的调用顺序
  11. 2.9 linux学习(1)
  12. z-tree 回显所有选中的id
  13. [Android] Android 常见第三方库汇总地址
  14. Zabbix-3-自定义脚本获取数据
  15. 实验吧 php
  16. SqlServer 获取字符串中小写字母的sql语句
  17. 编程填空:左边i位取反
  18. [Elixir005] 查看指定数据的详细信息 i helper
  19. Hibernate 干货
  20. Python中根据提供的日期,返回是一年中的第几天

热门文章

  1. django 视图与网址
  2. C语言博客作业04
  3. 热修复设计之AOT/JIT&amp;dexopt 与 dex2oat (一)
  4. 再往DjVu鼓吹者的头上敲一棒子
  5. 使用vue配合组件--转载
  6. SQL数据库—&lt;5&gt;视图、索引…简单学习
  7. 中州韵输入法(rime)导入搜狗词库
  8. std::list erase
  9. 【转】Java里如何实现线程间通信
  10. JDBC中如何进行事务处理?