博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud Conf 搭建配置中心
阅读量:3726 次
发布时间:2019-05-22

本文共 1649 字,大约阅读时间需要 5 分钟。

pringCloud Conf 搭建配置中心

更多干货

Spring Cloud Config支持在Git, SVN和本地存放配置文件,使用Git或者SVN存储库可以很好地支持版本管理,Spring默认配置是使用Git存储库。在本案例中将使用OSChina提供的Git服务存储配置文件。为了能让Config客户端准确的找到配置文件,需要了解application, profile和label的概念

  • server 服务器的配置
  • client 客户端的配置

Server 端代码

pom 依赖 config-server

org.springframework.cloud
spring-cloud-config-server

应用入口引入SpringBootApplication

@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication {  public static void main(String[] args) {    SpringApplication.run(ConfigServerApplication.class, args);  }}

配置

配置appllication.yml 文件,设置服务器端口号和配置文件Git仓库的链接

spring:  cloud:    config:      server:        git:          uri: https://github.com/csyeva/eductoconfig          username:           password:

访问方式

/{application}/{profile}[/{label}]/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties
  • 根据application-profile.properties 判断是否有同名的如果有则使用其配置文件。如果没有则用application.yml

client 客户端

pom 依赖

org.springframework.cloud
spring-cloud-starter-config

配置 bootstrap.yml

在bootstrap.yml中设置客户端名称spring.application.name和Config服务器的地址

  • label 当configserver的后端存储是Git时,默认就是master
  • 如果本地和远程都配置了 那么以远程为准
spring:  cloud:    config:      uri: http://localhost:8080      profile: dev      label: master  application:    name: provider

动态刷新配置

  • 无需重新启动客户端,即可更新Spring Cloud Config管理的配置
  • 获取配置文件中的profile 的值
@RestControllerpublic class ConfigClientController {  @Value("${profile}")  private String profile;  @GetMapping("/profile")  public String getProfile() {    return this.profile;  }}

转载地址:http://caonn.baihongyu.com/

你可能感兴趣的文章
manjaro gnome shell terminal 无法打开
查看>>
string、char*和char[]的转换
查看>>
VMware manjaro 突然无法连接网络
查看>>
复制网页文字小方法
查看>>
C++课设/大作业/简化版WPS
查看>>
manjaro安装前注意事项(物理机)
查看>>
运用Java基础语法,基础数据类型,流程控制语句和数组实现快递柜功能
查看>>
关于集合与数组的区别,Collection、List、Set接口的作用及相关的子类
查看>>
Java多线程之线程的六种状态
查看>>
Java 多线程之关于Runnable接口 与 Callable接口
查看>>
JAVA面向过程之判断五位回文数
查看>>
JAVA面向过程之图形打印任务
查看>>
JAVA面向过程之打印九九乘法表
查看>>
Java之Json解析总结
查看>>
JavaScript之打印九九乘法表
查看>>
JavaScrip 的对象的定义和使用、测试类型
查看>>
html+css+javascript做一个随机点菜器
查看>>
JavaScript实现京东轮播图效果——自动轮播,左右按钮切换图片,小圆圈跟随图片变化,点击小圆圈可跳转图片,无缝循环播放
查看>>
用Html+Css+JavaScript中的事件和正则表达式做一个“个人信息登记表“
查看>>
出现com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database ‘test‘ 应该怎么办
查看>>