spring cloud config配置
参考:
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配置的更多相关文章
- spring cloud config配置记录
1. spring cloud config配置记录 1.1. pom <!-- 分布式配置中心 --> <dependency> <groupId>org.spr ...
- Spring Cloud Config 配置刷新
客户端进行刷新操作. 1.添加 actuator包,这样 /refresh url才处于可用状态. <dependency> <groupId>org.springframew ...
- 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...
- 微服务SpringCloud之Spring Cloud Config配置中心Git
微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...
- 微服务SpringCloud之Spring Cloud Config配置中心服务化
在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...
- spring cloud --- config 配置中心 [本地、git获取配置文件]
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...
- Spring Cloud Config 配置中心高可用
详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...
- Spring Cloud Config 配置中心 自动加解密功能 jasypt方式
使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用 jasypt-spring-boot- ...
- Spring Cloud Config 配置中心
请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...
随机推荐
- HUE配置文件hue.ini 的liboozie和oozie模块详解(图文详解)(分HA集群)
不多说,直接上干货! 我的集群机器情况是 bigdatamaster(192.168.80.10).bigdataslave1(192.168.80.11)和bigdataslave2(192.168 ...
- 基础js--调试js
1,逻辑错误 常见错误: 是否由于拼写错误而导致申明了新的变量? 是否在条件判定上出现了疏漏? 方法:使用开发者工具调试代码 2,代码错误 常见错误: 是否拼写错误? 是否使用中文的符号? 扩展: 1 ...
- MySQL数据库以及其Python用法
一 命令行模式下: mysql -u root -p # 进入进入mysql命令行模式 show databases; # 查看所有数据库 create database data; # 创建数据库, ...
- OpenLdap与BerkeleyDB安装过程
前段时间在看LDAP方面的东西,最近重装了Ubuntu之后开始在自己的机器上装一个OpenLDAP. 装的过程中还遇到不少问题,不过通过Google同学的帮忙,全部得到解决.下面装安装过程记录如下: ...
- Ibatis框架之系统架构
如果用最简洁的话来总结 iBATIS 主要完成那些功能时,我想下面几个代码足够概括. Class.forName("oracle.jdbc.driver.OracleDriver" ...
- Java Reflect
Method method=demo.getMethod("sayChina"); method.invoke(demo.newInstance()); ...
- i.mx6 Android5.1.1 系统属性
属性变更的请求时init事件循环处理的另一个事件,在Android平台中,为了让运行中的所有进程共享系统运行时所需要的各种设置值,系统开辟了属性存储区域,并提供了访问该区域的API.属性由键(key) ...
- JQuery ajax-向服务器发送请求的方法
如需将请求发送到服务器,我们使用 XMLHttpRequest 对象的 open() 和 send() 方法: xmlhttp.open("GET","ajax_info ...
- sql 解释顺序
from:全量数据, where:数据过滤,生成新的虚表.个人主观上理解,where中的条件,如果涉及到join中的表,则会移动到相应的on条件中,减少后续生成的虚表大小. join:根据on中的条件 ...
- Docker学习之基本概念
Docker学习之基本概念 作为一个后端noder,不了解docker有点说不过去,这节开始,学习一些docker层面的东西. 什么是docker Docker最初是dotCloud公司创始人Solo ...