本文共 6454 字,大约阅读时间需要 21 分钟。
说明:参考
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。
在spring cloud config 组件中,分两个角色,一是config server,二是config client。Spring Cloud Config Server最常见是将配置文件存放方式有3种:
(1)本地 (2)远程Git仓库 (3)局域网数据库本文介绍局域网数据库方式存储配置文件
MariaDB [(none)]> create database config;Query OK, 1 row affected (0.03 sec)MariaDB [(none)]> use config;Database changedMariaDB [config]>
MariaDB [config]> CREATE TABLE `config_properties` ( -> `id` bigint(20) NOT NULL AUTO_INCREMENT, -> `key1` varchar(50) COLLATE utf8_bin NOT NULL, -> `value1` varchar(500) COLLATE utf8_bin DEFAULT NULL, -> `application` varchar(50) COLLATE utf8_bin NOT NULL, -> `profile` varchar(50) COLLATE utf8_bin NOT NULL, -> `label` varchar(50) COLLATE utf8_bin DEFAULT NULL, -> PRIMARY KEY (`id`) -> ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;Query OK, 0 rows affected (0.00 sec)MariaDB [config]>
MariaDB [config]> insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('1','server.port','8083','config-client','dev','master');'2','foo','bar-jdbc','config-client','dev','master');Query OK, 1 row affected (0.00 sec)MariaDB [config]> insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('2','foo','bar-jdbc','config-client','dev','master');Query OK, 1 row affected (0.00 sec)MariaDB [config]>
4.0.0 com.cntaiping.tpa config 1.0-SNAPSHOT pom config Demo project for Spring Boot UTF-8 UTF-8 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE config-server config-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
4.0.0 com.cntaiping.tpa config-server 0.0.1-SNAPSHOT jar config-server Demo project for Spring Boot com.cntaiping.tpa config 1.0-SNAPSHOT org.springframework.cloud spring-cloud-config-server mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc
spring: profiles: active: jdbc application: name: config-server datasource: url: jdbc:mysql://10.17.12.160:3306/config?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver cloud: config: label: master server: jdbc: trueserver: port: 8300spring.cloud.config.server.jdbc.sql: SELECT key1, value1 from config_properties where APPLICATION=? and PROFILE=? and LABEL=?
添加@EnableConfigServer
package com.cntaiping.tpa.configserver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
4.0.0 com.cntaiping.tpa config-client 0.0.1-SNAPSHOT jar config-client Demo project for Spring Boot com.cntaiping.tpa config 1.0-SNAPSHOT org.springframework.cloud spring-cloud-starter-config
新建文件bootstrap.properties
spring.application.name=config-clientspring.cloud.config.uri= http://localhost:8300/spring.cloud.config.fail-fast=truespring.profiles.active=dev
默认端口号8083
package com.cntaiping.tpa.configclient;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }}
package com.cntaiping.tpa.configclient.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ConfigController { @Value("${server.port}") String port; @Value("${foo}") String foo; @RequestMapping(value = "/get") public String get(String key){ if("foo".equals(key)){ return foo; } if("server.port".equals(key)){ return port; } return ""; }}
运行server
运行client转载地址:http://uwtab.baihongyu.com/