spring cloud config 配置中心
配置中心分为 server 服务端 client 和客户端,
服务端搭建
pom.xml
<?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> <parent> <!-- spring boot 继承 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.cyy</groupId> <artifactId>my-spring-cloud-config-server</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <!-- 添加 config-server 依赖 --> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by Administrator on 2017/8/16. */ @SpringBootApplication @EnableConfigServer // 配置中心服务 public class ConfigSreviceApplication { public static void main(String [] args){ SpringApplication.run(ConfigSreviceApplication.class); } }
properties 配置
server.port=4555 #git 仓库地址 spring.cloud.config.server.git.uri=https://github.com/cyyinfo/spring-cloud-config-demo.git
#如果配置文件不在仓库的根目录则需要配置此项,为配置文件所在目录 spring.cloud.config.server.git.search-paths=config-repository
到这里一个最简单的配置服务端搭建完成,启动后访问
http://localhost:4555/spring-cloud-config-sevice-demo/master 可以看到结果
http://localhost:4555/[git 仓库项目名称]/[profile 默认master]
client 端搭建
pom.xml
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.cyy</groupId> <artifactId>my-spring-cloud-config-client</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动类
package app; 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; /** * Created by Administrator on 2017/8/16. */ @RestController @SpringBootApplication @RequestMapping("/") public class ConfigClientApplication { @Value("${test.demo.name}") private String testDemoName; @RequestMapping("") public String index(){ return "config clicent testDemoName="+this.testDemoName+"#"; } public static void main(String [] args){ SpringApplication.run(ConfigClientApplication.class); } }
配置文件 注意是 bootstrap.properties 而不是 application.properties
spring.cloud.config.env=spring-cloud-config-sevice-demo // git 仓库名称 spring.cloud.config.profile=prev // 配置氮气环境 决定是加载git仓库中的 application-prev.propertis haishi spring.cloud.config.label=master // git 分支名 spring.cloud.config.uri=http://localhost:4555 // 配置中心项目访问地址 spring.application.name=my-config-client server.port=4556
完整 demo 地址
https://github.com/cyyinfo/spring-cloud-config-demo.git