全部spring cloud全部框架示例参考:http://www.weiqinxue.cn/blogs/index.php/User/articleview/ArticleID/U2A226
spring cloud config 配置集中管理分发的一种方式,默认使用git作为存放处,也支持SVN和本地文件系统:
服务器端搭建:
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>
<groupId>springCloudAll-configServer</groupId>
<artifactId>springCloudAll-configServer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
</dependencies>
</project>
application.yml:
spring:
application:
name: config
profiles:
active: native #开启本地配置查找,不配置这个会报找不到 GIT URI
cloud:
config:
server:
native: #本地查找配置,默认是去git找
search-locations: classpath:/config #配置加载路径,配置文件命名使用{application}-{profile}.yml格式
server:
port: 8888
ConfigApplication.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;
/**
* Created by damon on 2017/3/2.
*/
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
resources/config/test-dev.yml(测试配置文件):
demo: name: Spring cloud 解析之 config server
可访问:http://localhost:8888/test-dev.yml 查看结果
客户端使用:
bootstrap.yml(这个配置会在项目启动前加载,application.yml在启动后加载):
spring:
cloud:
config:
name: test #注意name和profile配装后的格式
profile: dev
uri: http://localhost:8888 #config server地址
server:
port: 10007
使用:
package controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by damon on 2017/3/2.
*/
@RestController
public class HelloController {
@Value("${demo.name}")
public String name;
@RequestMapping(value="/hello")
public String hello(){
String site = String.format("Hello! %s",name);
return site;
}
}
访问http://localhost:10007/hello结果:
Hello! Spring cloud 解析之 config server
留个问题,配置的安全性怎么保证?
好了就写到这里,本汪要睡了,晚安....
git@OSC源码地址:config-service
支付宝打赏
微信打赏