Spring Cloud Config cloud storage configuration information
Spring Cloud Config has the features of centrality, versioning, support for dynamic updates, platform independence, and language independence.
Our examples are:
1, real data exists in Git and other repository,
2, scconfigserver from Git to get the corresponding information,
3. Scconfigclient obtained from Scconfigserver
Communication between each other is based on protocols such as HTTP,TCP,UDP.
First, create and run a scconfigserver application
1.pom.xml
<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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>
<spring.version>4.3.0.RELEASE</spring.version>
</properties>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.SR4</version>
<relativePath/>
</parent>
<groupId>org.wanma.example</groupId>
<artifactId>org.wanma.example.ScConfigServer</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>ScConfigServer</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
2. Add @enableconfigserver annotations on the application main class, as follows
Package org.wanma.example.sconfigserver;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class Application
{
public static void Main (string[] args)
{
Springapplication.run (Application.class, args);
}
}
3. Create a repository named Scconfigdata on GitHub and create a mmb-config-client.yml file in it that reads as follows
---
Lucky-word:lexiaofei-v99
4. Develop a application.yml that reads as follows:
Server
port:8001
Spring
Cloud
Config
Server
Git:
Uri:https://github.com/lexiaofei/scconfigdata
Searchpaths:data
5. Start the Scconfigserver application, open the address Http://localhost:8001/ScConfigClient/default, shown below
{
"Name": "Scconfigclient",
"Profiles": ["Default"],
"Label": "Master",
"Version": "091149689CDC758927FB1781E8F89445D921B15C",
"Propertysources":
[
{
"Name": "Https://github.com/lexiaofei/ScConfigData/ScConfigClient.yml",
"Source": {"Lucky-word": "Lexiaofei-v99"}
}
]
}
Second, create and run a scconfigclient application
1.pom.xml
<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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>
<spring.version>4.3.0.RELEASE</spring.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.wanma.example</groupId>
<artifactId>org.wanma.example.ScConfigClient</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>ScConfigClient</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2. Create bootstrap.yml under Resource, and set the Spring.application.name,spring.cloud.config.uri,server.port information as follows
Spring
Application:
Name:scconfigclient
Cloud
Config
uri:http://localhost:8001
---
Server
port:8002
Note that this is bootstrap.yml, not appliction.yml, because BOOTSTRAP.YML will read before the app starts, and Spring.cloud.config.uri will affect the app startup
3. Create a Controller
Package org.wanma.example.scconfigclient.web;
Import Org.springframework.beans.factory.annotation.Value;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;
/**
*
*/
@RestController
public class Luckywordcontroller {
@Value ("${lucky-word}")
String Luckyword;
@RequestMapping ("/lucky-word")
Public String Showluckyword () {
Return "The Lucky Word is:" + Luckyword;
}
}
4. Start Application, open Http://localhost:8002/lucky-word
Package org.wanma.example.scconfigclient;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application
{
public static void Main (string[] args)
{
Springapplication.run (Application.class, args);
}
}
Final Result:
The lucky word is:lexiaofei-v99
Springcloud (3-3) Spring cloud Config cloud storage configuration information