spring boot2X集成spring cloud config
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
输出
说明:
如果出现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 boot2X集成spring cloud config的更多相关文章
- Spring Boot集成Spring Data Reids和Spring Session实现Session共享
首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...
- SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...
- SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...
- Spring boot2X集成zuul与consul实现负载均衡和反向代理
zuul 是netflix开源的一个API Gateway 服务器 所有从设备或网站来的请求都会经过Zuul到达后端的Netflix应用程序. 作为一个边界性质的应用程序,Zuul提供了动态路由.监控 ...
- Spring Boot 集成spring security4
项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...
- Spring MVC集成Spring Data Reids和Spring Session实现Session共享
说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入. ...
- Spring boot 集成Spring Security
依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...
- Spring boot集成spring session实现session共享
最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...
- Spring MVC集成Spring Data Reids和Spring Session实现Session共享出现:No bean named 'springSessionRepositoryFilter' available
出现这个问题:No bean named 'springSessionRepositoryFilter' available的的原因: 1.Spring MVC加载了多个配置文件导致的,并不是版本问题 ...
随机推荐
- asp.net core 在centeros 7.x下创建服务
Netcore服务生成说明 如有个项目/opt/wwwroot/dpms.1633.com 启动为/usr/bin/dotnet /opt/wwwroot/dpms.1633.com/DPMS.Web ...
- 消息队列mq总结
一.消息队列概述消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构.目前使用较多的消息队列有ActiveMQ,RabbitM ...
- JavaScript 总结[目录]
ECMAScript 基础 一.JavaScript 基础 1.1 JavaScript 简介 1.2 JavaScript 书写位置 二.JavaScript 数据类型 2.1 JavaScript ...
- 项目中 直接跳后台 不用输入/newadmin
- canvas与svg整理与区别
1.canvas画布(位图) 2.绘制矢量图 1.不要在style中给canvas设置宽高 会有位移差 2. //获取元素 var c=document.getElementById("c& ...
- ta和夏天一起来了
目录 ta和夏天一起来了 上半年,过去的就让去过去,遗憾的也别再遗憾. 下半年,拥有的请好好珍惜,想要的请努力去追. ta和夏天一起来了 转眼结束了2019的上半年,在这个月末, 季度末, 周末, ...
- 解决securecrt连接慢(而xshell秒连)的问题
打开securecrt的跟踪选项,观察输出信息来诊断 解决方法: 1)首先,将全局设置中,GSSAPI属性由自动改为GSSAPI 2)其次,将不支持的多余的密钥交换去掉 3)最后,可以将服务端 ...
- 交叉编译tmux
作者:彭东林 邮箱:pengdonglin137@163.com 参考 https://gist.github.com/tessus/5e118d44261a6ab2f198 环境 Qemu-4.1 ...
- xadmin引入django-stdimage在列表页预览图片
一.安装 pip install django-stdimage 安装django-stdimage库 https://github.com/codingjoe/django-stdimage Git ...
- MATLAB之心形图绘制
一.静态心形图绘制 (1)效果展示 (2)静态心形原始代码 clc; clear all; ; % 均布三位坐标 x=-:; y=-:; z=-:; [x,y,z]=meshgrid(x,y,z); ...