spring config 本地配置
spring cloud config 简介
- Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。
- Spring Cloud Config 分为两个部分 ,server端和client端。
server端配置服务器,管理配置信息
client端获取配置信息
创建并运行一个Spring Cloud Config Server
- 创建一个名为config-server的应用,并添加spring-cloud-starter-parent,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"> <modelVersion>4.0.0</modelVersion> <groupId>com.zcr.test</groupId> <artifactId>config-server</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>config-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- 创建application主类,并添加@EnableConfigServer注解,代码如下
package com.zcr.spring; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class,args); } }
- 在本地创建一个配置文件db-service-dev.properties,格式如下
sck.type=sck sck.url=jdbc:oracle:thin:@10.1.50.205:1521:orcl sck.class=oracle.jdbc.driver.OracleDriver sck.user=sddb sck.password=sddb eom.type=eom eom.url=jdbc:oracle:thin:@10.1.50.205:1521:orcl eom.class=oracle.jdbc.driver.OracleDriver eom.user=eom eom.password=eom
- 在resources文件夹下创建application.properties,内容如下
#tomcat端口号 server.port=8888 #配置文件在本地 spring.profiles.active=native #配置文件的目录 spring.cloud.config.server.native.search-locations=D:/workspace/GitHub/spring-config/config-file
目录结构:
- 运行应用并打开网址http://localhost:8888/db-service/dev,出现如下页面,证明配置文件发布成功
创建并运行一个Spring Cloud Config Client
- 创建一个名为config-client的应用,并添加spring-cloud-starter-parent,spring-cloud-starter-config,spring-boot-starter-web依赖,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"> <modelVersion>4.0.0</modelVersion> <groupId>com.zcr.spring</groupId> <artifactId>config-client</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.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-test</artifactId> <scope>test</scope> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
- 在resources中创建bootstrap.properties(bootstrap.yml会在应用启动之前读取),格式如下
#文件名 spring.application.name=db-service #文件模式,默认为default spring.cloud.config.profile=dev #server端ip地址 spring.cloud.config.uri= http://localhost:8888/ #client端ip端口 server.port=8881
- 创建一个controller
package com.zcr.spring; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${sck.user}") String sckUser; @Value("${sck.password}") String sckPassword; @RequestMapping("/hello") public String showUserAndPassword(){ String sb = "username is " + sckUser + "," + "password is " +sckPassword; return sb; } }
- 启动Application,并访问http://localhost:8881/hello,出现如下界面,成功
遗留问题:1.初始化时,注入在static代码块之后,故静态代码块无法获取远程配置信息。
2.对spring了解浅。不懂内部原理,bug调试过程中比较费劲。
3.对spring中的注解不了解。