准备工作
- 创建一个本地git库
- 创建一个spring cloud server项目
- 创建一个spring cloud client项目
- 基本注意事项
创建本地git仓库
$ cd ~
$ mkdir springCloud
$ cd springCloud/
$ git init .
$ echo info.foo: bar >lesson-dev.properties
$ git add -A .
$ git commit -m “init”
创建一个spring cloud 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.lesson</groupId>
<artifactId>configServer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</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>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 启动类ConfigServerController内容
package init;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.stereotype.Controller;
/**
* Created by lesson on 2017/9/27.
*/
@SpringBootApplication
@Controller
@EnableConfigServer
public class ConfigServerController {
public static void main(String[] args) throws Exception {
SpringApplication.run(ConfigServerController.class, args);
}
}
- 配置文件 application.properties
spring.cloud.config.server.git.uri=file://${user.home}/springCloud
management.security.enabled=false
server.port=8888
启动 config-server
运行ConfigServerController main方法
创建spring cloud client
项目结构图
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.lesson</groupId>
<artifactId>configClient</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath />
</parent>
<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-actuator</artifactId>
</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>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- HelloController内容
package init;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by lesson on 2017/9/27.
*/
@SpringBootApplication
@Controller
@RefreshScope
public class HelloController {
@Value("${foo}")
private String name;
@RequestMapping("getFoo")
@ResponseBody
public String getFoo(){
return name;
}
public static void main(String[] args) throws Exception {
SpringApplication.run(HelloController.class, args);
}
}
- application.properties文件
server.port=4000
server.context-path=/
spring.application.name=lesson
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8888/
spring.cloud.config.failFast=true
management.security.enabled=false
启动HelloController
postman 测试url
修改git仓库配置文件内容
调用config client项目的刷新接口 (config server可以自动感知git仓库变化)
重新调用config client项目测试链接
可以看到响应内容已经改为我们修改后的content了
基本注意事项
git仓库中文件命名
所以 lesson-dev.properties 的命名规则
lesson取自cloud client配置文件中的 spring.application.name
dev 取自 cloud client 配置文件中的 spring.cloud.config.profilecloud client 要求配置文件更新自动刷新类中的响应属性的类上要配上@RefreshScope注解 如下
原因摘自 spring cloud
cloud client项目的pom文件中 要引入一下内容
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>