Create a new project, here I am using 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>
Note: The cloud dependency of the client here is: spring-cloud-starter-config, and the server side is spring-cloud-config-server, otherwise an error will be reported, indicating that the bean cannot be generated, and the autowired cannot be used.
There is no need to add annotations to the program entry, just use the automatically generated ones
Configure bootstrap.properties under the resource package to add configuration items
spring.application.name=didispace
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/
server.port=7820
Note: The name item above must be the same as the name of the bunch of configuration files in the warehouse
The configuration in the Git repository is on the server side, as follows:
didispace.properties
didispace-dev.properties
didispace-prod.properties
didispace-test.properties
create a 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;
}
}
Start the service and visit http://localhost:7820/service to display the contents of the configuration
git-dev-1.0
Since bootstrap.properties accesses didispace-dev.properties, the result of the above access is the configuration content of the Git repository didispace-dev.properties file