参考:

http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html

http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

https://www.cnblogs.com/xiaoliu66007/p/8963934.html

关系图

版本控制

差异文件内容

wly-config-dev.properties

local.ip=192.168.1.0
local.port=8080

wly-config-test.properties

local.ip=192.168.1.1
local.port=8081

 wly-config-product.properties

local.ip=192.168.1.2
local.port=8082

spring boot版本:2.0.3.RELEASE

spring cloud config server配置

pom.xml

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

application.yml

server:
port: 8080
spring:
application:
name: spring-cloud-config-server-wly
cloud:
config:
server:
# git配置:项目/${search-paths}/${application}-${profile}.properties
# cloud-config-wly/config/config/neo-config-dev.properties
git:
# 项目地址,换成自己的项目地址
uri: git@xxx:xxxx/cloud-config-wly.git
# 表示文件路径
search-paths: config
# 项目用户名,换成项目用户名
username: xxx
# 项目密码,换成项目密码
password: xxx

启动类(新增@EnableConfigServer注解)

package com.wang.springcloudconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer
@SpringBootApplication
public class SpringcloudconfigApplication { public static void main(String[] args) {
SpringApplication.run(SpringcloudconfigApplication.class, args);
}
}

启动服务

http://localhost:8080/wly-config/dev

spring cloud config client配置

pom.xml

<!-- 引入spring cloud config 客户端配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 引入web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

application.yml同级目录(src/main/resources)新增配置文件:bootstrap.properties

此文件会先于application.yml加载

# 相当于${application}
spring.cloud.config.name=wly-config
# 相当于${profile}
spring.cloud.config.profile=product
# 配置中心server服务url
spring.cloud.config.uri=http://localhost:8080/
# 相当于git分支
spring.cloud.config.label=master

application.yml

spring:
application:
name: spring-cloud-config-client-wly
server:
port: 8081

controller

package com.wang.springcloudconfigclient.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @Author wly
* @Date 2018/7/2 11:35
*/
@RestController
public class HelloController {
@Autowired
private Environment env; @RequestMapping("/test")
public String test() {
return env.getProperty("local.ip") + "," + env.getProperty("local.port");
}
}

 访问

http://localhost:8081/test

页面输出

192.168.1.2,8082

基于注册中心版

在spring cloud config client端写死spring cloud config server的ip、port不适用于复杂多变的场景,比如集群的配置,ip变化等。所以我们可以基于注册中心配置

启动一个注册中心的服务spring-cloud-eureka,端口号8765

pom.xml

spring cloud config server、spring cloud config client均引入eureka客户端

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

启动类

spring cloud config server、spring cloud config client启动类均添加注册注解(@EnableDiscoveryClient)

package com.wang.springcloudconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudconfigApplication { public static void main(String[] args) {
SpringApplication.run(SpringcloudconfigApplication.class, args);
}
}

spring cloud config server application.yml

添加注册中心配置

server:
port: 8080
spring:
application:
name: spring-cloud-config-server-wly
cloud:
config:
server:
# git配置:项目/${search-paths}/${application}-${profile}.properties
# cloud-config-wly/config/config/neo-config-dev.properties
git:
# 项目地址
uri: git@xxx:xxx/cloud-config-wly.git
# 表示文件路径
search-paths: config
# 项目用户名
username: xxxx
# 项目密码
password: xxxx
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8765/eureka

spring cloud config client bootstrap.properties

# 相当于${application}
spring.cloud.config.name=wly-config
# 相当于${profile}
spring.cloud.config.profile=dev
# 配置中心server服务url,使用注册中心配置后,此句注释即可
#spring.cloud.config.uri=http://localhost:8080/

# 相当于git分支
spring.cloud.config.label=master
# 启用注册发现
spring.cloud.config.discovery.enabled=true
# 读取服务,即config server的spring.application.name
spring.cloud.config.discovery.serviceId=spring-cloud-config-server-wly

# 注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8765/eureka

顺序启动注册中心、spring cloud config server、spring cloud config client3个服务

注册中心

访问(效果与之前一致~)

http://localhost:8080/wly-config/dev

http://localhost:8081/test

代码

server:https://gitee.com/wlyfree/springcloudconfig

client:https://gitee.com/wlyfree/springcloudconfigclient

spring cloud config配置的更多相关文章

  1. spring cloud config配置记录

    1. spring cloud config配置记录 1.1. pom <!-- 分布式配置中心 --> <dependency> <groupId>org.spr ...

  2. Spring Cloud Config 配置刷新

    客户端进行刷新操作. 1.添加 actuator包,这样 /refresh url才处于可用状态. <dependency> <groupId>org.springframew ...

  3. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  4. 微服务SpringCloud之Spring Cloud Config配置中心Git

    微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...

  5. 微服务SpringCloud之Spring Cloud Config配置中心服务化

    在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...

  6. spring cloud --- config 配置中心 [本地、git获取配置文件]

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...

  7. Spring Cloud Config 配置中心高可用

    详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...

  8. Spring Cloud Config 配置中心 自动加解密功能 jasypt方式

    使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用  jasypt-spring-boot- ...

  9. Spring Cloud Config 配置中心

    请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...

随机推荐

  1. excel 正则表达式用法

    Private Sub RegEx_Replace()        Dim myRegExp As Object      Dim Myrange As Range, C As Range      ...

  2. Mac 10.12安装截图工具Jietu

    说明:这个Jietu是鹅厂出品,名字就叫Jietu!! 下载: http://jietu.qq.com/ 离线版本:(链接: https://pan.baidu.com/s/1eSzt8Ue 密码: ...

  3. (转)一次棘手的rootvg更换硬盘处理过程

    一次棘手的rootvg更换硬盘处理过程 原文:http://www.talkwithtrend.com/Article/160857 事件起因 下午接到现场工程师电话,一台双系统抽屉IBM P570一 ...

  4. 使用redux-devtools工具

    在vue中型项目开发的过程中,一般都是要用到vuex这个状态管理工具的,这样可以方便我们管理全局的状态,同时,为了在开发的过程中,更加方便地实时查看到state状态,我们会使用 vue-devtool ...

  5. github里如何删除一个repository仓库

    高手请绕行,新手往下走. 作为一个刚接触github(https://github.com/)的新手,除了感叹开源的丰富和强大之外,自己肯定也想试用一下,因此申请帐号神马的.今天自己创建一个Repos ...

  6. auguements实参对象的数组化

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Tableau学习系列之Tableau的工作区简介(工作表工作区、 仪表板工作区 和故事工作区 )和基本概念

    不多说,直接上干货! 在首次进入Tableau或打开Tableau但没有指定工作簿时,会显示“开始页面”,其中包含了 近使用的工作簿.已保存的数据连接.示例工作簿和其他一些入门资源,这些内容将帮助初学 ...

  8. 对 BOIS 的一些介绍

    BIOS里不可调节CPU将在64/32位系统下工作 知识点延伸1,一般CPU要想在64位状态下运行必须具备两个条件: 1)安装64位操作系统 2)CPU本身就支持64位指令集 知识点延伸2有必要了解B ...

  9. JavaScript设计模式-20.责任链模式

    <html> <head> <title>javascript高级语法20-责任链模式</title> </head> <body&g ...

  10. C++实现二叉排序树

    1.定义 二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),亦称二叉搜索树. 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子 ...