参考:

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. Mac下安装tomcat8(Mac 10.12)

    1.到官网下载tomcat8 http://tomcat.apache.org/download-80.cgi 说明:tomcat最好不要下载最新的,选择一个适中的最好. 2.安装 ▲解压并重命名文件 ...

  2. shiro学习笔记_0300_jdbcRealm和认证策略

    使用shiro框架来完成认证工作,默认是iniRealm,如果需要使用其他的realm,需要配置. ini配置文件详解,官方文档的说明如下: [main] section 是你配置应用程序的 Secu ...

  3. Java源码安全审查

    最近业务需要出一份Java Web应用源码安全审查报告, 对比了市面上数种工具及其分析结果, 基于结果总结了一份规则库. 本文目录结构如下: 检测工具 FindSecurityBugs 基于class ...

  4. python-tornado-hello,world

    #!/usr/bin/python import tornado.httpserver import tornado.ioloop import tornado.options import torn ...

  5. django显示SQL语句

    django显示SQL语句 有时候我们使用模型查询数据,但是并不知道具体执行的SQL语句到底对不对.那么可以通过下面的方法打印出具体执行的SQL语句.这样有助于调试: queryset = MyMod ...

  6. 把AspDotNetCoreMvc程序运行在Docker上-part1

    接<基于ASP.Net Core学习Docker技术第一步:在CentOS7安装Docker平台>这个博文,在搭建完成Docker平台之后,可以开始让aspdotnetcore程序运行在d ...

  7. 第9章 scrapy-redis分布式爬虫

    9-1 分布式爬虫要点 1.分布式的优点 充分利用多机器的宽带加速爬取 充分利用多机的IP加速爬取速度 问:为什么scrapy不支持分布式? 答:在scrapy中scheduler是运行在队列的,而队 ...

  8. UEditor图片焦点错位,火狐document.body.scrollTop不管用的问题

    转自 http://liyunpeng.iteye.com/blog/2068751 关于 document.body.scrollTop 在火狐浏览器中不管用的问题 看网上有人写通过判断docume ...

  9. SVG 旋转图形实例

    本实例展示如何在SVG中画出一个正方形并使之旋转.运行结果如下图所示: 在文本框中输入时间间隔,单位是毫秒.点击Start按钮,蓝色方块就会开始转动,每个时间间隔变化一度.变换的角度在下面的Angle ...

  10. C# 条码生成类

    using System.Collections; using System.Text.RegularExpressions; namespace DotNet.Utilities { public ...