Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务中心采用Git的方式存储配置文件,因此我们很容易部署修改,有助于对环境配置进行版本管理。

一、配置中心

1.1

新建模块config-server

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.feng</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>config-server</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies> </project>

1.2

application.yml

server:
port: 8030 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/ #eureka服务注册地址 # git管理配置
spring:
cloud:
config:
server:
git:
uri: https://github.com/fengzp/config/ #git仓库地址
searchPaths: demo* #搜索路径
# username: username
# password: password
application:
name: config-server

1.3

ConfigApplication,添加EnableConfigServer标识是一个配置中心服务

/**
* @author fengzp
* @date 17/5/4
* @email fengzp@gzyitop.com
* @company 广州易站通计算机科技有限公司
*/
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigApplication { public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}

1.4

在配置的git仓库下新建一个demo1的文件夹,在里面创建一个叫client-a-dev.properties的配置文件

文件中随便加上两个配置

ip=192.168.30.51
port=9999

启动模块,然后打开 http://localhost:8030/client-a/dev

说明读取配置成功

这里说明一下http请求读取配置的匹配规则:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

二、客户端读取配置

2.1

修改client-a模块

pom文件新增依赖

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

2.2

bootstrap.yml添加相关配置后

server:
port: 8910 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/ spring:
application:
name: client-a
cloud:
config:
discovery:
enabled: true #开启通过服务来访问Config Server的功能
service-id: config-server
profile: dev
label: master

2.3

在TestController添加测试方法

@RestController
public class TestController { @Autowired
RestTemplate restTemplate; @RequestMapping("/hi")
@HystrixCommand(fallbackMethod = "hiFallback")
public String hi(@RequestParam String id){
return restTemplate.getForObject("http://service-a/hi?id="+id, String.class);
} public String hiFallback(String id) {
return "hi, " + id + ", error!";
} @Value("${ip}")
private String ip;
@Value("${port}")
private String port; @RequestMapping("/getProperties")
public String getProperties(){
return ip + " : " + port;
}
}

启动模块后打开 http://localhost:8910/getProperties

说明读取配置成功

spring cloud学习(五) 配置中心的更多相关文章

  1. spring cloud学习(六) 配置中心-自动更新

    上一篇学习了spring cloud config的基本使用,但发现有个问题,就是每次更改配置后,都需要重启服务才能更新配置,这样肯定是不行的.在上网查资料了解后,spring cloud支持通过AM ...

  2. Spring Cloud Config的配置中心获取不到最新配置信息的问题

    Spring Cloud Config的配置中心获取不到最新配置信息的问题 http://blog.didispace.com/spring-cloud-tips-config-tmp-clear/

  3. 跟我学SpringCloud | 第六篇:Spring Cloud Config Github配置中心

    SpringCloud系列教程 | 第六篇:Spring Cloud Config Github配置中心 Springboot: 2.1.6.RELEASE SpringCloud: Greenwic ...

  4. Spring Cloud Config 实现配置中心,看这一篇就够了

    Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...

  5. Spring Cloud Config(配置中心)

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 一.简介 Spring Cloud Config为分布式系统中的外部配置提供服务器和客 ...

  6. Spring Cloud Config 分布式配置中心使用教程

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

  7. Spring Cloud 2-Config 分布式配置中心(七)

    Spring Cloud  Config  1.github配置 2.服务端配置 pom.xml application.xml Application.java 3.配置和命名 1. 配置加载顺序 ...

  8. Spring Cloud Config 分布式配置中心【Finchley 版】

    一. 介绍 1,为什么需要配置中心? 当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错. 因此,需要一个能够动态注册和获取服务信息的 ...

  9. Spring Cloud Consul使用——配置中心

    1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

随机推荐

  1. Controller异步模式

    转载: https://blog.csdn.net/yingxiake/article/details/51193319 因为服务器请求处理线程的总数是有限的,如果类似的请求多了,所有的处理线程处于阻 ...

  2. 在win8 App中,StorageFile比Path更好用

    Skip the path: stick to the StorageFile: http://blogs.msdn.com/b/wsdevsol/archive/2012/12/05/stray-f ...

  3. Linux运维之docker虚拟化部署nginx

    一.Docker的概念 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱 ...

  4. mysql 批量杀进程

    select concat('KILL ',id,';') from information_schema.processlist where user='root';

  5. 2019.01.13 bzoj1146: [CTSC2008]网络管理Network(整体二分+树剖)

    传送门 题意简述:给一棵树,支持单点修改,询问路径上两点间第kkk大值. 思路: 读懂题之后立马可以想到序列上带修区间kkk大数的整体二分做法,就是用一个bitbitbit来支持查值. 那么这个题把树 ...

  6. 2018.12.21 bzoj3238: [Ahoi2013]差异(后缀自动机)

    传送门 后缀自动机好题. 题意: 做法:samsamsam 废话 考虑翻转字串,这样后缀的最长公共前缀等于前缀的最长公共后缀. 然后想到parentparentparent树上面两个串的最长公共后缀跟 ...

  7. SQL2008 2机镜像

    清除设置 //删除端点 declare @sql varchar() declare @mirrName varchar() select @mirrName=name from sys.databa ...

  8. 学以致用一 安装centos7.2虚拟机

    5说来惭愧,也是很久没来博客园了.距离上次写的已经快一年,只能说时间过的真的很快. 而如果这一年一直在坚持认真学习的话,收获肯定很多.然而我确又浪费了很多光阴,不得不恨这人生苦短. 在这一年里,小孩还 ...

  9. 基于udp协议的套接字,socketserver模块,多道技术,进程理论

    进程指的是一个正在进行/运行的程序,进程是用来描述程序执行过程的虚拟概念 进程vs程序 程序:一堆的代码 进程:程序执行的过程 进程的概念起源于操作系统,进程是操作系统最核心的概念,操作系统的其他所有 ...

  10. hihocoder#1631 : Cats and Fish

    Description There are many homeless cats in PKU campus. They are all happy because the students in t ...