微服务迁移记(三):配置中心SpringCloud Config搭建
springboot推荐使用注解方式,减少了大量的xml配置。系统的基本配置文件我选择用yml格式,相对于properties,代码更简洁(不用重复写属性),结构化更清晰一点,读取速度也应该能略快一点吧。配置文件名bootstrap.yml优先于application.yml。
分布式配置中心,主要是将配置信息保存在配置中心的本地文件或数据库或远程版本控制中心(svn、git)中。研究了一段时间阿波罗,不知道为啥虚拟机能telnet宿主mysql,但阿波罗始终提示数据库连接不上,遂放弃。进一步研究config,并将配置数据成功保存至mysql数据库中。
一、数据库创建
建立一张tb_config_server。为防止字段与关键字重复,前面加了字母a。
akey:配置名称
avalue:配置值
application:对应注册中心注册应用的服务别名
aprofile:dev 开发环境,prd:生产环境
label:标签
DROP TABLE IF EXISTS `tb_config_server`;
CREATE TABLE `tb_config_server` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`akey` varchar(255) DEFAULT NULL,
`avalue` varchar(255) DEFAULT NULL,
`application` varchar(255) DEFAULT NULL,
`aprofile` varchar(255) DEFAULT NULL,
`label` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
增加一条测试数据:
insert into tb_config_server(akey,avalue,application,aprofile,label) values('site_url','http://www.baidu.com','xxproject-api-service-sys','dev');
其中:xxproject-api-service-sys:代表是api项目中系统维护模块服务,这个名字是这个项目注册到注册中心的别名。
二、config配置中心搭建
1. 新建项目:xxproject-config-server
2. 导包,主要是config和数据库,同时需要将config注册到注册中心,所以consul的包也导入
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version> 2.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> <version>2.1.3.RELEASE</version> </dependency> <!-- 使用数据库配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version> 5.1.48</version> </dependency> </dependencies>
3. 编写配置文件
spring.application.name:注册到注册中心的别名,客户端通过这个别名连接配置中心。
spring.cloud.config.server.jdbc:主要是一条SQL语句,提取配置列表。
spring: application: name: config-server cloud: consul: host: 192.168.0.7 port: 8500 discovery: hostname: 192.168.0.6 config: server: jdbc: sql: SELECT akey,avalue FROM tb_config_server WHERE application=? AND aprofile=? AND label=? datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/zyconfig??useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: 123456 profiles: active: jdbc server: port: 9000
4. 编写启动入口
@EnableDiscoveryClient:注册中心
@EnableConfigServer:配置中心
@SpringBootApplication @EnableDiscoveryClient @EnableConfigServer public class AppConfigServer { public static void main(String[] args){ SpringApplication.run(AppConfigServer.class,args); } }
启动程序后,就可以在注册中心看到配置中心的服务,配置中心搭建完毕
三、客户端读取测试
1. config包导入
<!-- config client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> <version> 2.1.4.RELEASE</version> </dependency>
2. 配置文件(使用bootstrap.yml)
spring:
application:
## 系统管理服务,注册中心别名
name: xxproject-api-service-sys
cloud:
config:
discovery:
## 配置中心在注册中心的别名
service-id: config-server
enabled: true
profile: dev
label: dev
consul:
host: 192.168.0.7
discovery:
hostname: 192.168.0.6
port: 8500
server:
port: 9001
3. 启动入口
@SpringBootApplication @EnableDiscoveryClient public class AppService { public static void main(String[] args){ SpringApplication.run(AppService.class,args); } }
4. 编写简单的测试控制器
@RestController public class testController{ @Value("{site_url}") private String site_url; @RequestMapping("/index") public String index(){ return site_rul; } }
访问:http://localhost:9001/index
可以成功打印出:http://www.baidu.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述