1 cloud config jdbc server
- pom 配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- application.properties
server.port=8082
spring.application.name=cloud-config-jdbc-server
#数据库配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/config?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=****
spring.datasource.password=****
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.cloud.config.label=master
spring.cloud.config.server.jdbc=true
spring.cloud.config.server.jdbc.sql=SELECT key1, value1 from config_properties where APPLICATION=? and PROFILE=? and LABEL=?
spring.profiles.active=jdbc
注意:数据库连接和用户名密码都需要自己修改,如果需要变更表命和字段直接修改sql字段就可以
- 分享下我的建表语句
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=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
这里解释下为什么用key1这么low的字段,其实想提醒自己高版本的mysql key是关键字,用会出问题
下面是异常信息
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'KEY, VALUE from PROPERTIES where APPLICATION='spring-boot' and PROFILE='master' ' at line 1
2 cloud config client
- pom配置
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- bootstrap.properties(注意这块不是application.properties)
# 和git里的文件名对应
spring.application.name=config-client
# 远程仓库的分支
spring.cloud.config.label=master
# dev 开发环境配置文件 | test 测试环境 | pro 正式环境
# 和git里的文件名对应
spring.cloud.config.profile=dev
# 指明配置服务中心的网址
spring.cloud.config.uri= http://localhost:8082/
server.port=8083
- 测试类代码
@SpringBootApplication
@RestController
public class BiCloudConfigClientApplication {
@Value("${key1}") // git配置文件里的key
String myww;
@RequestMapping(value = "/hi")
public String hi(){
return myww;
}
public static void main(String[] args) {
SpringApplication.run(BiCloudConfigClientApplication.class, args);
}
}
最后附上git源码地址
服务端代码:https://github.com/fasoal/spring-cloud-config-jdbc-server
客户端代码:https://github.com/fasoal/spring-cloud-config-client
网友评论