从零开始搭建spring-cloud(5) ----config
Spring Boot profile
在Spring Boot应用中,我们可以将配置内容写入application.yml,设置多个profile,也可以用多个application-{profile}.properties文件配置,并在启动时指定–spring.profiles.active={profile}来加载不同环境下的配置。
在Spring Cloud微服务架构中,这种方式未必适用,微服务架构对配置管理有着更高的要求,如:
- 集中管理:成百上千(可能没这么多)个微服务需要集中管理配置,否则维护困难、容易出错;
- 运行期动态调整:某些参数需要在应用运行时动态调整(如连接池大小、熔断阈值等),并且调整时不停止服务;
- 自动更新配置:微服务能够在配置发生变化是自动更新配置。
以上这些要求,传统方式是无法实现的,所以有必要借助一个通用的配置管理机制,通常使用配置服务器来管理配置。
Spring Cloud Config
Spring Cloud Config 为分布式系统的外部配置提供了服务端和客户端的支持方案。在配置的服务端您可以在所有环境中为应用程序管理外部属性的中心位置。客户端和服务端概念上的Spring Environment 和 PropertySource 抽象保持同步, 它们非常适合Spring应用程序,但是可以与任何语言中运行的应用程序一起使用。当应用程序在部署管道中从一个开发到测试直至进入生产时,您可以管理这些环境之间的配置,并确保应用程序在迁移时具有它们需要运行的所有内容。服务器存储后端的默认实现使用git,因此它很容易支持标记版本的配置环境,并且能够被管理内容的各种工具访问。很容易添加替代的实现,并用Spring配置将它们插入。
Spring Cloud Config 包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git,我们先以git为例做一套示例。
Spring Cloud Config同样分为server端和client端,一般将配置都放在Git上,这样版本管理比较方便。
Spring Cloud Config Server
新建项目config-server
pom.xml文件内容如下:
<?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"><modelVersion>4.0.0</modelVersion><groupId>cn.ac.iie</groupId><artifactId>config</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.1.4.RELEASE</version><scope>import</scope><type>pom</type></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Greenwich.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.1.4.RELEASE</version><configuration><mainClass>cn.ac.iie.App</mainClass></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skip>true</skip></configuration></plugin></plugins></build>
</project>
添加App.java,内容如下:
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}
修改application.properties
,内容如下:
spring.application.name=config
eureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/
spring.cloud.config.server.git.uri=http://swarm-worker1/duandingyang/config-repo.git
spring.cloud.config.server.git.username=duandingyang
spring.cloud.config.server.git.password=12345678
server.port=8085
在gitlab上新建一个项目config-repo
,然后在新建一个文件elasticsearch.properties
,文件内容跟之前的elasticsearch项目中的配置文件内容一样,如下所示:
management.endpoints.web.exposure.include=health,info,env,metrics
spring.elasticsearch.rest.uris=swarm-manager:9200
management.endpoint.health.show-details=always
logging.level.root=INFO
logging.file=user.log
es.ips[0]=swarm-manager
es.port=9300
es.clusterName=my-applicationeureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/spring.application.name=es-service
server.port=8082
启动项目,在Eureka中可以看到Config服务已经成功启动
然后我们访问http://localhost:8085/elasticsearch-a.properties
:
如果访问:http://localhost:8085/elasticsearch-b.properties
也可以访问:
可以看到正确输出了我们在gitlab上的配置文件。
为什么要在文件名后加一个a
?,如果不写就会报错
如果在浏览器中输入:http://localhost:8085/elasticsearch-a.yml
:
自动会对文件格式进行转换。此外还可以转换json:
浏览器地址栏中的命名格式写法
- /{name}-{profiles}.yml:
- /{label}/{name}-{profiles}.yml
name表示文件名(或者服务名),profiles表示环境,label表示分支(如果不写,默认是master分支)
/{name}-{profiles}.yml方式
在gitlab上面新建一个文件elasticsearch-test.properties
,内容如下:
management.endpoints.web.exposure.include=health,info,env,metrics
spring.elasticsearch.rest.uris=swarm-manager:9200
management.endpoint.health.show-details=always
logging.level.root=INFO
logging.file=user.log
es.ips[0]=swarm-manager
es.port=9300
es.clusterName=my-application
eureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/
spring.application.name=es-serviceenv=test
跟之前的内容是一样的,加了一行env=test
,然后在浏览器中输入:http://localhost:8085/elasticsearch-test.json
,同样可以访问配置文件:
/{label}/{name}-{profiles}.yml
新建分支release
release分支会从master中拷贝一份,修改elasticsearch-test.json,添加内容:label=release
然后通过访问http://localhost:8085/release/elasticsearch-test.json
,可以得到release分支下的配置文件
可以看到多了一个label=release
spring-cloud config会从远端的git拉取到本地的git
在控制台可以查看本地的git路径:
可以修改配置:spring.cloud.config.server.git.basedir=E:/git/neimeng/config/basedir
来修改git的路径.
Spring-config client
上面介绍了config server的使用,现在介绍client的使用。首先修改elasticsearch服务的依赖,添加spring cloud config依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-client</artifactId></dependency>
替换application.properties文件:
server.port=8082spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config
spring.cloud.config.profile=test
spring.application.name=elasticsearch
如果仅仅是这样启动的话,会报错。理由是不知道这个配置文件中的配饰项的先后顺序。因此需要先加载这个配置文件中的内容。
因此需要修改application.properties为bootstrap.properties,此外如果eureka的端口不是默认的8761,程序启动时默认从localhost:8761获取eureka中的service-id,当修改端口后,获取不到service-id,因此会从默认的localhost:8888获取,便会报错找不到service-id。因此需要将git上面的配置eureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/
拿出来,写到bootstrap.properties中。这样就会首先读取eureka,然后在eureka中找服务。因为我们的配置文件中有方括号,程序无法识别,需要将这个配置放在bootstrap.properties中。
bootstrap.properties内容如下:
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config
spring.cloud.config.profile=test
eureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/
es.ips[0]=swarm-manager
application.properties内容如下:
server.port=8082
spring.application.name=elasticsearch
远端git的elasticsearch-test.properties配置文件内容如下:
management.endpoints.web.exposure.include=health,info,env,metrics
spring.elasticsearch.rest.uris=swarm-manager:9200
management.endpoint.health.show-details=always
logging.level.root=INFO
logging.file=user.log
es.port=9300
es.clusterName=my-applicationeureka.client.service-url.defaultZone=http://192.168.171.45:8090/eureka/spring.application.name=es-service
server.port=8082
这样就配置成功了。
动态刷新配置SpringCloud Config Bus
当我们修改git上的配置时,我们的项目中的配置并没有修改。
我们需要在config server端添加SpringCloud Config Bus依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency>
然后在application.properties中添加:
spring.rabbitmq.host=swarm-manager
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
在RabbitMQ中就会出现下面的队列:
在config client中引入config bus依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency>
然后在application.properties中添加:
spring.rabbitmq.host=swarm-manager
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
启动client,查看RabbitMQ管理页面:
可以看到多了一个队列,说明注册成功。
然后需要在config server中暴露api,修改application.properties:
management.endpoints.web.exposure.include=*
启动服务,然后发送请求:
查看RabbitMQ:
应用
在git上面elasticsearch-test.properties中添加内容:
girl.name=Jenny
girl.age=18
在elasticsearch项目中新建GirlConfig.java,内容如下:
package cn.ac.iie.bean;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties("girl")
@RefreshScope
public class GirlConfig {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
然后在新建GirlController.java,内容如下:
@RestController
public class GirlController {@Autowiredprivate GirlConfig girlConfig;@GetMapping("/girl/print")public String print() {return "" + girlConfig.getName() + "," + girlConfig.getAge();}}
这样访问http://localhost:8082/girl/print
当我们修改配置文件之后:
girl.name=Jenny
girl.age=30
首先发送请求:
再次发送请求:
就可以看到更新后的配置了。
每次我们修改配置之后,都需要手动刷新http://localhost:8085/actuator/bus-refresh
,如何避免这种方式?
webhooks
使用webhooks可以实现此功能。
从零开始搭建spring-cloud(5) ----config相关推荐
- 自己动手,使用Spring Initializr从零开始搭建Spring Cloud项目
新建Project 这里使用的开发工具是IDEA,JDK版本1.8. 打开IDEA开发工具,File -> New -> Project 然后一步步往下设置,然后到这一步,选择Spring ...
- 从零开始带你实战搭建Spring Cloud Alibaba商城项目
这里写目录标题 一:环境搭建 二:项目搭建 三:分布式组件--Spring Cloud Alibaba 1.微服务架构简介 2.为什么使用SpringCloud Alibaba作为组件 3.本次项目技 ...
- 从0到1 手把手搭建spring cloud alibaba 微服务大型应用框架(三) (mini-cloud) 搭建认证服务(认证/资源分离版) oauth2.0 (中)
本文承接上文<从0到1 手把手搭建spring cloud alibaba 微服务大型应用框架(三) (mini-cloud) 搭建认证服务(认证/资源分离版) oauth2.0 (上)> ...
- 从0到1手把手搭建spring cloud alibaba 微服务大型应用框架(十五) swagger篇 : gateway 集成swagger 与 knife4j实现在线api文档并嵌入到自己项目内
背景 我们日常开发中基本都是协同开发的,当然极个别的项目整体前后端都是一个人开发的,当多人协作时,尤其是前后端人员协同开发时 必然会面临着前端需要了解后端api接口的情况,两个选择,提前设计好文档,然 ...
- 告诉老默我想学Spring Cloud了(新手篇):从0到1搭建Spring Cloud项目(实际项目开发的浓缩精华版)
告诉老默我想学Spring Cloud了(新手篇):从0到1搭建Spring Cloud项目 一.前言 二.如何选择版本 2.1 SpringCloud 和 Spring Boot 版本选型 2.1. ...
- Spring Cloud入门-Config分布式配置中心(Hoxton版本)
文章目录 Spring Cloud入门系列汇总 摘要 Spring Cloud Config 简介 在Git仓库中准备配置信息 配置仓库目录结构 master分支下的配置信息 dev分支下的配置信息 ...
- maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目
项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...
- 微服务(三) 【手摸手带你搭建Spring Cloud】 Ribbon 什么是负载均衡?spring cloud如何实现负载均衡?ribbon负载均衡有几种策略?Ribbon是什么?
在上一章,我介绍了springcloud的eureka搭建.我们做了服务注册.最后我们还介绍了一些续约,失效剔除等参数配置.已经不需要再通过手动输入ip去访问服务,而是通过中心只需要通过服务名就可以获 ...
- spring cloud(三) config
spring cloud 配置中心 config 搭建过程 1.搭建config-server 服务端 1.1. 新建boot工程 pom引入依赖 <!-- config配置中心 --> ...
- idea 构建java 微服务_使用 IDEA 从 0 开始搭建 Spring Cloud 微服务
以下内容均来源于一个微服务初学者的实践,仅供参考. 微服务架构 首先启动 Spring Cloud Eureka 注册中心,其他部分都作为服务注册到 Eureka ,并通过注册的服务名互相访问.Spr ...
最新文章
- java8压缩,如何使用Java 8压缩多个列表?
- openstack 重启mysql_突然断电导致mariadb数据库无法启动(openstack 命令无法使用)...
- 【搜索引擎Jediael开发笔记】v0.1完整代码
- .NET6之MiniAPI(二十):实体验证FluentValidation
- Visual Studio交叉编译器提供对ARM的支持
- 网络工程属于计算机哪一类,网络工程专业属于什么门类
- ubuntu上安装 ibus Google拼音输入法
- spring加载application.xml异常
- (转)Python3异常-AttributeError: module 'sys' has no attribute 'setdefaultencoding
- 使用JSON数据报错和方法
- 你还在生产环境改代码么?函数计算版本管理(三)使用别名进行灰度发布...
- SPSS异方差检验的实现
- 等效焦距和可视角度换算表
- LTE CQI优化提升方法
- GCC中的编译选项“-I”(大写i),“-L”(大写l),“-l”(小写l)等参数
- Requests库爬虫详解
- 学计算机高中该做些什么准备,准备事项
- linux 内存 参数,linux free命令参数及用法详解(linux查看内存命令)
- CSS:怎么样给背景图加透明度 opacity ?
- 初中教资计算机考试知识点,教资考试初中物理电学知识点总结来了
热门文章
- Flex State
- 从零搭建Prometheus监控报警系统
- python实现redis分布式锁
- PHP的pcntl多进程
- jQuery获取元素
- MySQL覆盖索引:直接从索引查询到了数据
- ERROR: Could not find a version that satisfies the requirement requests (from v ersions: none)
- 中运量71路线路图_双语导乘对标“航空式服务”,71路中运量车队服务进博会出“新招”...
- 基于javaweb的物资配送管理系统_智慧物流之RFID仓库管理系统,为传统的仓库管理带来了希望...
- apache shiro 如何升级_Shiro登录认证