spring cloud Learn the config configuration tutorial for getting Started
- 2020-10-23 20:06:52
- OfStack
preface
This article is intended to give you an introduction to spring cloud. It mainly introduces the config configuration. Let's start with the detailed introduction.
Introduction to the
Spring cloud config is divided into two parts server client
config-server
Create config - server
First create the config-server project.
File structure:
├ ─ ─ config-server.iml
├ ─ ─ pom.xml
└ ─ ─ src
├ ─ ─ main
│ ├ ─ ─ java
│ │ └ ─ ─ com
│ │ └ ─ ─ lkl
│ │ └ ─ ─ springcloud
│ │ └ ─ ─ config
│ │ └ ─ ─ server
│ │ └ ─ ─ Application.java
│ └ ─ ─ resources
│ ├ ─ ─ application.properties
│ └ ─ ─ bootstrap.properties
└ ─ ─ test
└ ─ ─ java
pom. xml content:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.lkl.springcloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config</artifactId>
<version>1.0.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- Represented as a web engineering -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Exposure to various indicators -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Create startup class
Application.Java
package com.lkl.springcloud.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by liaokailin on 16/4/28.
*/
@Configuration
@EnableAutoConfiguration
@RestController
@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Where @ES50en is the key annotation
Create application.properties under the resources file
server.port=8888
Configure the project to listen on port 8888. By default client gets configuration information by reading http://localhost:8888
Create bootstrap properties
spring.cloud.config.server.git.uri: https://github.com/liaokailin/config-repo
The configuration information through fork https: / / github com/spring - cloud - samples/config - repo download (local)
through
spring.cloud.config.server.Git.uri
Specifies the git address for the configuration information store
Run config - server
spring boot project is very convenient to start, just run ES91en.java
Access to resource information on git follows the following rules:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
application: Represents the application name, passed in client
spring.config.name
configuration
profile: means to get the configuration in the specified environment, such as default, the default value of development environment, test environment and production environment, and dev, test, demo,
production etc.
label: git tag, default value master
If the application name is foo, you can access it as follows:
http://localhost:8888/foo/default
http://localhost:8888/foo/development
It can be accessed as long as it is configured according to the above rules.
config-client
Create config - client
The directory structure is as follows:
├ ─ ─ pom.xml
├ ─ ─ spring-cloud-config-client.iml
└ ─ ─ src
├ ─ ─ main
│ ├ ─ ─ java
│ │ └ ─ ─ com
│ │ └ ─ ─ lkl
│ │ └ ─ ─ springcloud
│ │ └ ─ ─ config
│ │ └ ─ ─ client
│ │ └ ─ ─ Application.java
│ └ ─ ─ resources
│ └ ─ ─ bootstrap.yml
└ ─ ─ test
└ ─ ─ java
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.lkl.springcloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>1.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Create the startup class Application.java
package com.lkl.springcloud.config.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by liaokailin on 16/4/28.
*/
@SpringBootApplication
@RestController
public class Application {
@Value("${name:World!}")
String bar;
@RequestMapping("/")
String hello() {
return "Hello " + bar + "!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Create the bootstrap.properties file, which reads as follows:
spring.application.name: foo
spring.cloud.config.env:default
spring.cloud.config.label:master
spring.cloud.config.uri:http://localhost:8888
Among them
spring.application.name
For the application name,
spring.cloud.config.uri
Configure the config-server exposed get configuration interface, which defaults to http://localhost:8888
The item 23 configuration was mentioned earlier and is configured by default, therefore
bootstrap.properties
Just configure the application name.
Run config - client
Visit http://localhost to get 'Hello liaokailin' to get configuration information on git
Visit http: / / localhost env get all the configuration information, access to configuration information can be found success.
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.lkl.springcloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config</artifactId>
<version>1.0.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- Represented as a web engineering -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Exposure to various indicators -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
0
ok ~ it's work ! more about is here (local download)
conclusion