1、簡介
本文主要內容是基於jdbc搭建配置中心,使應用從配置中心讀取配置信息並成功注冊到注冊中心,關於配置信息表結構僅供參考,大家可以根據具體需要進行擴展。
2、Config Server 搭建
2.1、Maven 依賴
因為需要從數據庫讀取配置文件,所以需要添加MySQL的依賴
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
2.2、JDBC 數據庫准備
這里的表結構只為測試使用,具體可根據業務需要進行調整,此結構僅供參考
-- ----------------------------
-- Table structure for properties
-- ----------------------------
DROP TABLE IF EXISTS `properties`;
CREATE TABLE `properties` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`key` varchar(50) DEFAULT NULL,
`value` varchar(500) DEFAULT NULL,
`application` varchar(50) DEFAULT NULL,
`profile` varchar(50) DEFAULT NULL,
`label` varchar(50) DEFAULT NULL,
`remark` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of properties
-- ----------------------------
BEGIN;
INSERT INTO `properties` VALUES (2, 'eureka.client.serviceUrl.defaultZone', '${EUREKA_SERVICE_URL:http://localhost:8888}/eureka/', 'eureka-client', 'dev', 'v0.0.1', '配置中心地址');
INSERT INTO `properties` VALUES (3, 'management.endpoint.conditions.enabled', 'true', 'eureka-client', 'dev', 'v0.0.1', '啟用終結點');
INSERT INTO `properties` VALUES (4, 'eureka.instance.prefer-ip-address', 'true', 'eureka-client', 'dev', 'v0.0.1', '使用IP地址注冊到注冊中心');
INSERT INTO `properties` VALUES (5, 'spring.application.name', 'eureka-client', 'eureka-client', 'dev', 'v0.0.1', '應用名稱');
INSERT INTO `properties` VALUES (6, 'eureka.instance.instanceId', '${spring.application.name}@${spring.cloud.client.ip-address}@${server.port}', 'eureka-client', 'dev', 'v0.0.1', '在注冊中心的實例ID');
INSERT INTO `properties` VALUES (7, 'management.endpoints.web.exposure.include', '*', 'eureka-client', 'dev', 'v0.0.1', '開放哪些監控端口');
INSERT INTO `properties` VALUES (8, 'server.port', '8000', 'eureka-client', 'dev', 'v0.0.1', '應用服務端口號');
COMMIT;
2.3、Config Server 配置
server.port=1000
spring.application.name=lkf-cloud-config-jdbc
spring.profiles.active=jdbc
#數據庫配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/lkf_cloud?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#讀取配置文件的SQL語句
spring.cloud.config.server.jdbc.sql=SELECT `key`,`value` FROM properties WHERE application=? AND PROFILE=? AND label=?
# 配置中心api前綴
spring.cloud.config.server.prefix=lkf
啟動配置中心,訪問 http://localhost:1000/lkf/eureka-client/dev/v0.0.1,得到應用為 eureka-client ,開放環境【dev】,版本號為【v0.0.1】 的配置信息
{
"name": "eureka-client",
"profiles": ["dev"],
"label": "v0.0.1",
"version": null,
"state": null,
"propertySources": [{
"name": "eureka-client-dev",
"source": {
"eureka.client.serviceUrl.defaultZone": "${EUREKA_SERVICE_URL:http://localhost:8888}/eureka/",
"management.endpoint.conditions.enabled": "true",
"eureka.instance.prefer-ip-address": "true",
"spring.application.name": "eureka-client",
"eureka.instance.instanceId": "${spring.application.name}@${spring.cloud.client.ip-address}@${server.port}",
"management.endpoints.web.exposure.include": "*",
"server.port": "8000"
}
}]
}
至此,基於jdbc的配置中心已經成功從MySQL數據庫讀取配置信息,配置中心搭建完成
3、Config Client 配置
3.1、Maven 依賴
<!--注冊中心客戶端-->
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--actuator 監控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--配置中心客戶端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3.2、Config Client 配置
#配置環境
spring.cloud.config.profile=dev
#配置中心地址
spring.cloud.config.uri=http://localhost:1000/lkf
#配置文件名稱
spring.cloud.config.name=eureka-client
#配置文件版本號
spring.cloud.config.label=v0.0.1
啟動應用,在瀏覽器訪問注冊中心:http://127.0.0.1:8888,發現應用已成功從注冊中心讀取配置文件並注冊到注冊中心