创建一个新项目,这里我用的是IDEA
1: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>com.smart</groupId>
<artifactId>configserver2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config-server2</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>Brixton.RELEASE</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>
注意:这里客户端的cloud依赖为:spring-cloud-starter-config,而服务器端是spring-cloud-config-server,否则会报错,提示类似不能生成bean,不能autowired之类
程序入口不用添加注解,用自动生成的就好
2:resource包下配置 bootstrap.properties 添加配置项
spring.application.name=didispace
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/
server.port=7820
注意:上面的name项必须与仓库里面那一堆配置文件的名称一致
Git仓库里面的配置就是服务器那边的,如下:
didispace.properties
didispace-dev.properties
didispace-prod.properties
didispace-test.properties
3:建立一个controller
package com.smart.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by wanghan on 2018/4/26.
*/
@RefreshScope
@RestController
public class TestController {
@Value("${from}")
private String from;
@RequestMapping("/service")
public String from() {
return this.from;
}
public void setFrom(String from) {
this.from = from;
}
public String getFrom() {
return from;
}
}
4:启动服务访问 http://localhost:7820/service 会显示配置里面的内容
git-dev-1.0
由于bootstrap.properties里面访问的是didispace-dev.properties 所以上面访问结果就是Git仓库 didispace-dev.properties 文件的配置内容