Spring Cloud 微服务入门教程(五):统一配置中心-ConfigService
上一节《Spring Cloud 微服务入门教程(四):微服务间的调用消费-FeignClient》我们讲了微服务的新建和服务间的调用消费,随着微服务的增多,那么多SpringBoot程序,修改他们的配置文件会是很恐怖的工作量,所以微服务架构中还为我们提供了配置中心,这样可以方便统一的管理我们的服务配置文件,同时线上生产环境的配置是不对开发人员开放的,这样只需要运维人员维护线上的配置中心即可。
新建一个配置中心模块
新建一个名为「config」的模块作为配置中心提供的服务,如何新建模块参见之前的章节文章,代码也上传到Github了,在次不再赘述,只关注新增的东西。「config」要比上一章节前的「demoservice」多一个依赖:spring-cloud-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>cloud</artifactId>
<groupId>net.renfei</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>net.renfei</groupId>
<artifactId>config</artifactId>
<version>1.0.0</version>
<name>config</name>
<description>配置中心</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后新建包名net.renfei.config,和程序启动入库类ConfigServerApplication,代码如下:
package net.renfei.config;
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;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
在resources文件夹中新增配置文件application.yml,内容如下:
server:
port: 8114
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://github.com/NeilRen/SpringCloudDemo.git
search-paths: springcloud-config
# username:
# password:
# basedir:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
application.yml中配置了启动端口、应用名称,eureka注册中心地址,还多了spring.cloud.config.server的配置,这里的意思是从Git仓库拉取配置文件,我这里设置的是Github的项目地址,所以用户名和密码都是不需要的,search-paths是指去哪个文件夹下面搜索配置文件。
在Git上新增配置文件
在Git上面新增服务的配置文件,我这里新建了DemoClient-dev.yml、DemoService-dev.yml,地址在:https://github.com/NeilRen/SpringCloudDemo/tree/master/springcloud-config,我们以DemoService-dev.yml为例,解释一下。
首先是命名规则:{application}-{profile}.yml,前面是应用名称,后面是环境配置,那DemoService服务的Dev环境配置文件就是DemoService-dev.yml的命名。
然后是内容,其实除了application.name和spring.cloud.config配置相关的,其他都能放进去远程加载,例如启动端口号、数据库地址等等。在这个章节中我的DemoService-dev.yml在Git中的内容是:
server:
port: 18080
验证配置中心
我们启动eureka注册中心以后,再启动config配置中心,然后可以看到config注册成功,访问的地址本章节示例的地址是:http://localhost:8114/DemoService-dev.yml,其实不仅仅支持yml格式,如果你访问http://localhost:8114/DemoService-dev.json就会自动转换为json格式,也就是说,配置中心是可以自动转换需要的配置文件格式的。


改造服务从配置中心拉取配置
我们以DemoService服务为例,其他服务也是同样的改造方法:
修改POM文件增加spring-cloud-config-client的依赖,本章节DemoService服务示例:
<?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>cloud</artifactId>
<groupId>net.renfei</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>net.renfei</groupId>
<artifactId>demoservice</artifactId>
<version>1.0.0</version>
<name>demo-service</name>
<description>演示服务</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>net.renfei</groupId>
<artifactId>apicenter</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
重命名application.yml为bootstrap.yml
因为需要在程序启动时拉取配置文件,所以之前的application.yml就不能满足要求了,需要使用bootstrap.yml,这样在程序启动时会去配置中心拉取配置文件,bootstrap.yml内容为:
spring:
application:
name: DemoService
cloud:
config:
discovery:
service-id: config
enabled: true
profile: dev
bootstrap.yml中声明了application的名称和配置中心的名字,service-id填的就是注册中心中配置中心的名称,profile就是要加载哪个环境的配置文件。
按照同样的改造步奏,将其他服务改造完后,就可以尝试使用配置中心了,注意我的案例中启动顺序是eureka注册中心->config配置中->DemoService服务->DemoClient服务。
到这里配置中心就搭建完了,但是每次修改配置文件还是需要重启,所以下一章节会讲Spring Cloud Bus服务总线来通知服务自动拉取配置,需要依赖一个新环境支持,那就是「RabbitMQ」消息队列,无论是在本机还是使用Docker,要先提前安装一下,下一节的Spring Cloud Bus服务总线依赖「RabbitMQ」消息队列,我们下一节见。
商业用途请联系作者获得授权。
版权声明:本文为博主「任霏」原创文章,遵循 CC BY-NC-SA 4.0 版权协议,转载请附上原文出处链接及本声明。
相关推荐
猜你还喜欢这些内容,不妨试试阅读一下评论与留言
以下内容均由网友提交发布,版权与真实性无法查证,请自行辨别。微信订阅号
扫码关注「任霏博客」微信订阅号- 你写得非常清晰明了,让我很容易理解你的观点。
- 感谢分享!拿走了~
- 您是说 UCClient 类接收来自Discuz的UCenter的消息吧,请求是来自 Discuz 的 UCenter吗?code 为 null 说明请求URL地址中没有 code 参数 (?code=xxx) ,确定是 UCenter 发起的请求吗?
- String code = request.getParameter("code"); code一直是null 这是为什么啊
- 你好,我想问一下如果是分析型的数据库要怎么制作docker镜像呢 是修改V008R003C002B0320版本号吗
- 可以的,我也正在开发分享的程序,可以邮件或群联系我都可以,关于页面里有联系方式:https://www.renfei.net/page/about 。
- 有破解软件的需要可以私下联系您吗?
- 您好,手机APP只是个客户端,用于数据呈现展示,数据均保存在服务器上,只留个APP没有任何用处,无能为力哦。
- 老哥 看你弄了这么多软件好厉害啊。 我有个软件 我买过几个小会员 没用几天 然后商家跑路了,软件服务器关闭了,连不上去 用不了。 你能做成一个打补丁版本可以本地用的么? 方便看下么?https://haodezhe.lanzouw.com/iD0f30h9joza 谢谢老哥!
- 您好,由于版权投诉和我国知识产权法的完善,我已经下架所有破解软件的下载链接了。
- 生花妙笔信手来 – 基于 Amazon SageMaker 使用 Grounded-SAM 加速电商广告素材生成 [1]
- github.renfei.net 不再完整代理 Github 页面改为代理指定文件
- 优雅的源代码管理(三):本地优雅的使用 Git Rebase 变基
- 优雅的源代码管理(二):Git 的工作原理
- 优雅的源代码管理(一):版本控制系统 VCS(Version Control System)与软件配置管理 SCM(Software Configuration Management)
- ChatGPT 开发商 OpenAI 买下极品域名 AI.com
- 火爆的 AI 人工智能 ChatGPT 国内注册教程、使用方式和收费标准
- 解决 SpringCloud 中 bootstrap.yml 不识别 @activatedProperties@ 参数
- Cron表达式书写教程搞定Linux、Spring、Quartz的定时任务
- 阿里云香港可用区C发生史诗级故障