Spring Cloud Config 分为

  Config Server:

    分布式配置中心,是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息

  Config Client:

    通过指定配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息

Spring boot版本2.1.8.RELEASE

服务中心使用Consu,启动Consu

1.配置中心(服务端)

easy-config

(1)添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

(2)配置

  在resources下

  A. 添加 bootstrap.properties

  spring.profiles.active=native本地存储配置方式

  也可以使用git方式

server.port=
spring.application.name=easy-config
spring.cloud.consul.host=localhost
spring.cloud.consul.port=
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port}
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/config/

  B. 添加config/easy-api-dev.properties

hello-string=我是来自配置中心的

(3)修改启动类

添加注解@EnableConfigServer开启配置服务支持

package com.tydt.easy.config;

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

启动easy-config

浏览器访问 http://localhost:8091/easy-api/dev

返回结果

{
"name": "easy-api",
"profiles": [
"dev"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "classpath:/config/easy-api-dev.properties",
"source": {
"hello-string": "我是来自配置中心的"
}
}
]
}

说明:

  配置中心的配置文件会被转化成相应的web接口

  /{application}/{profile}[/{label}]

  /{application}/{profile}.yml

  /{label}/{application}-{profile}.yml

  /{application}/{profile}.properties

  /{label}/{application}-{profile}.properties

2.客户端

easy-api

(1)添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

(2)配置

  添加配置bootstrap.properties

  通过注册中心的发现服务,去配置中心查找配置

server.port=8083
spring.application.name=easy-api
spring.profiles.active=dev
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500 spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.heartbeat.enabled=true management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=easy-config
#设为true,如果无法连接config server,启动时会抛异常,并停止服务
spring.cloud.config.fail-fast=true

(3)测试方法

package com.tydt.engine.api.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController {
@Value("${hello-string}")
private String helloString;
@RequestMapping("/")
public String Hello(){
return "hello,easy-api,"+helloString;
}
}

3.测试

启动easy-api

测试地址

  http://localhost:8083/

返回结果

  hello,easy-api,我是来自配置中心的

4.更新

修改了配置中心的配置后,如何读取到新的配置呢

(1)修改测试方法

  添加注解 @RefreshScope

package com.tydt.easy.api.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class HelloController {
@Value("${hello-string}")
private String helloString;
@RequestMapping("/")
public String Hello(){
return "hello,easy-api,"+helloString;
}
}

启动easy-config

启动easy-api

测试地址

  http://localhost:8083/

返回结果

  hello,easy-api,我是来自配置中心的

(2)修改配置

easy-config的resources/config/easy-api-dev.properties

hello-string=我是来自配置中心的111

重启easy-config

执行http://localhost:8083/actuator/refresh

输出

[
    "hello-string"
]
http://localhost:8083/
输出
  hello,esay-api,我是来自配置中心的111

说明:

  如果出现Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available

  无论在 Config Server 中配置什么端口,Config Client 启动时,会去访问都默认的 8888 端口

  出现这种情况可以删掉以前的配置文件

  在resources文件夹下,新建 bootstrap.properties 文件( bootstrap.yml)

  为什么会这样呢?application.properties(application.yml)同bootstrap.properties(bootstrap.yml)的区别是什么呢?查看

Spring官方文档中文版

spring boot2X集成spring cloud config的更多相关文章

  1. Spring Boot集成Spring Data Reids和Spring Session实现Session共享

    首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...

  2. SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  3. SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  4. Spring boot2X集成zuul与consul实现负载均衡和反向代理

    zuul 是netflix开源的一个API Gateway 服务器 所有从设备或网站来的请求都会经过Zuul到达后端的Netflix应用程序. 作为一个边界性质的应用程序,Zuul提供了动态路由.监控 ...

  5. Spring Boot 集成spring security4

    项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...

  6. Spring MVC集成Spring Data Reids和Spring Session实现Session共享

    说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入. ...

  7. Spring boot 集成Spring Security

    依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  8. Spring boot集成spring session实现session共享

    最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...

  9. Spring MVC集成Spring Data Reids和Spring Session实现Session共享出现:No bean named 'springSessionRepositoryFilter' available

    出现这个问题:No bean named 'springSessionRepositoryFilter' available的的原因: 1.Spring MVC加载了多个配置文件导致的,并不是版本问题 ...

随机推荐

  1. 聊聊Lambda架构

    定义 在数据分析场景中,我们可能会遇到这样的问题.例如,我们要做一个推荐系统,如果我们用批处理任务去做,一天或者一小时的推荐频次明显延迟太大.如果用流处理任务,虽然延迟的问题解决了,然而只用实时数据而 ...

  2. 使“Cmder Here”菜单在Tab页开新窗口

    Cmder是一个非常好用的的控制台命令行,我们在实际使用的时候,经常通过如下指令将其注册到右键菜单: Cmder.exe /REGISTER ALL 这样就可以在任意文件夹下快速打开Cmder,并且能 ...

  3. c# 合并两个DataTable

    当两个DataTable 结构相同可以用自带方法(Merge)合并 // // 摘要: // 将指定的 System.Data.DataTable 与当前的 DataTable 合并,指示是否在当前的 ...

  4. Ext.form.Label组件动态设置html值

    解决方法: (1)用的是 Ext.getCmp(id).setText('XXXX')可以动态设置label 显示的文本值,但是文本中有个别数字需要改变颜色显示,需要加样式,这种方法会把加样式的标签( ...

  5. Winform中设置ZedGraph的曲线为折线、点折线、散点图

    场景 Winform中设置ZedGraph的曲线为散点图: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102465399 在上 ...

  6. sigprocmask阻塞信号

    有时候不希望在接到信号时就立即停止当前执行,去处理信号,同时也不希望忽略该信号,而是延时一段时间去调用信号处理函数 1. 信号阻塞和忽略信号的区别 操作系统在信号被进程解除阻塞之前不会将信号传递出去, ...

  7. MySQL容器化详细教程

    前言:  上篇文章介绍了Docker工具的安装及常用命令使用.本篇文章我们会介绍如何在Docker中运行MySQL实例,可能有的小伙伴会问:为什么要在Docker里运行MySQL呢?因为在Docker ...

  8. Python基础(三)--流程控制之if、while、for,break与continue

    一.流程控制之if……else…… if语句是指编程语言中用来判定所给定的条件是否满足,根据判定的结果(真或假)决定执行给出的两种操作之一 if 条件1: 缩进的代码块 elif 条件2: 缩进的代码 ...

  9. hadoop hdfs 有内网、公网ip后,本地调试访问不了集群解决

    问题背景: 使用云上的虚拟环境搭建测试集群,导入一些数据,在本地idea做些debug调试,但是发现本地idea连接不上测试环境 集群内部配置hosts映射是内网映射(内网ip与主机名映射),本地只能 ...

  10. Nginx 核心配置-新建一个web站点

    Nginx 核心配置-新建一个web站点 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Nginx基础配置常用参数说明 [root@node101.yinzhengjie.or ...