Spring Cloud Getting Started tutorial (b): One of the Configuration Management Spring Cloud service registry entry

Use Config Server, you can manage an external application properties in all environments. The concept mapping on the client and server with Spring  Environmentand PropertySourceabstract the same, so they are a perfect fit with Spring applications, but can be used with any application running in any language. As applications by the developers to test and production deployment process, you can manage the configuration between these environments and to determine the application has everything you need to run the migration. The default storage backend server implementations use git, so it is easy to support label version of the configuration environment, and can access a variety of tools for managing content. Alternatively implemented easily added, and insert configuration using Spring

These are the Spring Cloud's official website describes the configuration services, simple to elaborate my understanding. For example, we want to build a website, you need to configure the database connection, specify the IP address of the database server, database name, user name and password and other information. The usual way, we can define this information in a configuration file, or a page specially configured to develop these things. Only a web server, it is very convenient.

But if the need to build with multiple servers, each server can of course do the same configuration, but the maintenance and synchronization will be very troublesome. I understand the configuration of the service at least two different scenarios:

1) multiple customers use the same configuration: for example, multiple servers in a cluster, if the back-end using the same database, so each server are using the same configuration.

2) Different customers use different configurations: for example, the typical scenario is the development, testing, production and use of the same system, but with a different database

If there is a fundamental unity of the configuration it is not very convenient, a possible solution is to put these configuration files into a shared storage (such as a network shared disk) in. So only you need to modify one or more shared storage configuration files on it. But the way to share files is limited by the specific deployment environment, which is often difficult to achieve multiple Web servers share the same storage hard disk.

Shortcoming shared disk resource locator is more difficult, Spring Cloud's solution is to move these profiles into the version management server which, Spring Cloud default configuration using the GIT. All Web services are acquiring these configuration files from the GIT. Since no shared storage between GIT server to specific Web server, as long as the network up on the line, which can implement Web services decouple the configuration storage location information.

Spring Cloud GIT unified control applications and interactive services, applications only need to configure URL to Spring Cloud GIT according to specifications. After using GIT, Scenario 1 Scenario 2 and the only difference is that Scenario 2 different client use different versions of the configuration file, but the file access application but appears to be the same. Spring Cloud service configuration into the configuration of FIG.

Let's continue on an example of one of Spring Cloud started. Registration service  to continue, so "Hello World" is read out from the configuration file helloworld.properties, content format as follows

hello=Hello World

Where the value of the keyword hello "Hello World", is what we want to output.

A. Create a config Server

 1. Create Config Server, maven project configuration inside spring-cloud-config-server

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
</dependency>

Complete configuration is as follows:

Copy the code
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4   <modelVersion>4.0.0</modelVersion>
 5   <groupId>com.chry</groupId>
 6   <artifactId>springcloud.helloworld.config.server</artifactId>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <packaging>jar</packaging>
 9   <name>helloworld.config.server</name>
10   <description>Demo Config Server</description>
11 
12     <parent>
13         <groupId>org.springframework.boot</groupId>
14         <artifactId>spring-boot-starter-parent</artifactId>
15         <version>1.5.3.RELEASE</version>
16         <relativePath/> <!-- lookup parent from repository -->
17     </parent>
18 
19     <properties>
20         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22         <java.version>1.8</java.version>
23     </properties>
24 
25     <dependencies>
26         <!--eureka server -->
27         <dependency>
28             <groupId>org.springframework.cloud</groupId>
29             <artifactId>spring-cloud-starter-eureka</artifactId>
30         </dependency>
31         <dependency>
32             <groupId>org.springframework.cloud</groupId>
33             <artifactId>spring-cloud-starter-eureka-server</artifactId>
34         </dependency>
35         <dependency>
36             <groupId>org.springframework.cloud</groupId>
37             <artifactId>spring-cloud-config-server</artifactId>
38         </dependency>
39         <!-- spring boot test-->
40         <dependency>
41             <groupId>org.springframework.boot</groupId>
42             <artifactId>spring-boot-starter-test</artifactId>
43             <scope>test</scope>
44         </dependency>
45     </dependencies>
46 
47     <dependencyManagement>
48         <dependencies>
49             <dependency>
50                 <groupId>org.springframework.cloud</groupId>
51                 <artifactId>spring-cloud-dependencies</artifactId>
52                 <version>Dalston.RC1</version>
53                 <type>pom</type>
54                 <scope>import</scope>
55             </dependency>
56         </dependencies>
57     </dependencyManagement>
58 
59     <build>
60         <plugins>
61             <plugin>
62                 <groupId>org.springframework.boot</groupId>
63                 <artifactId>spring-boot-maven-plugin</artifactId>
64             </plugin>
65         </plugins>
66     </build>
67 
68     <repositories>
69         <repository>
70             <id>spring-milestones</id>
71             <name>Spring Milestones</name>
72             <url>https://repo.spring.io/milestone</url>
73             <snapshots>
74                 <enabled>false</enabled>
75             </snapshots>
76         </repository>
77     </repositories>
78 
79 </project>
Copy the code

2. Create Config Server, it is also a Spring Boot application, @ EnableConfigServer narrative as a Config Server. Similarly, we use @EnableEurekaClient to register it to a service center.

Copy the code
 1 package springcloud.helloworld.config.server;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.config.server.EnableConfigServer;
 6 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 7 
 8 @EnableEurekaServer
 9 @EnableConfigServer
10 @SpringBootApplication
11 public class ConfigServerApplication {
12     public static void main(String[] args) {
13         SpringApplication.run(ConfigServerApplication.class, args);
14     }
15 }
Copy the code

3. Config server configuration file appication.yml, pay attention to the configuration file is the url GIT repository server address, file searchPaths configuration file is located in the warehouse folder paths you do not need to specify a specific profile name in the server side, because of the specific What is the application configuration file (ie client) decision.

Copy the code
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git.oschina.net/chrywhy/test
          searchPaths: spring-cloud/helloworldConfig
  application:
    name: config-server
Copy the code

4. Start config server, access the http: // localhost: 8888 / abc / xyz, as visible response. This is the output that did not include the specific contents of the configuration file, the response explained, config server configuration can access our services in application.yml in GIT

The URL is valid and means need to be explained. We can see from the output abc is the application name, xyz is the name of the profile, attention abc here, xyz are casually enter the name, you do not need real, config server just returned from the REST interface application called abc when called xyz, structure GIT configuration environment profile.

REST interface config server provides, Spring Cloud provides several official documents can be several optional URL as follows:

  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml
  3. /{label}/{application}-{profile}.yml
  4. /{application}-{profile}.properties
  5. /{label}/{application}-{profile}.properties

For example, a third format, if we have a profile spring-cloud / helloworldConfig / config-client-dev.properties in the GIT repository then visit http:. // localhost: 8888 / config-client-dev.properties can display configuration file contents. In this example, application name is "config-client" (also below our client will be created), profile name is dev, file suffix .properties

In this example because the configuration of the eureka service center, so this config server as a eureka client registered to a eureka server, you can from http: // localhost: 8761 to see us start the config server, if you do not register to the service center, but also this configuration removed

 

 Two . Create a config client

1. Create a maven project, pom.xml as follows:
Copy the code
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>com.chry</groupId>
 5     <artifactId>Springcloud.helloworld.config.client</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7     <name>Springcloud.helloworld.config.client</name>
 8     <packaging>jar</packaging>
 9     <description>Demo Spring Config Client</description>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>1.5.3.RELEASE</version>
15         <relativePath/> <!-- lookup parent from repository -->
16     </parent>
17 
18     <properties>
19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21         <java.version>1.8</java.version>
22     </properties>
23 
24     <dependencies>
25         <dependency>
26             <groupId>org.springframework.cloud</groupId>
27             <artifactId>spring-cloud-starter-eureka</artifactId>
28         </dependency>
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-web</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.cloud</groupId>
35             <artifactId>spring-cloud-starter-config</artifactId>
36         </dependency>
37         <dependency>
38             <groupId>org.springframework.boot</groupId>
39             <artifactId>spring-boot-starter-test</artifactId>
40             <scope>test</scope>
41         </dependency>
42     </dependencies>
43 
44     <dependencyManagement>
45         <dependencies>
46             <dependency>
47                 <groupId>org.springframework.cloud</groupId>
48                 <artifactId>spring-cloud-dependencies</artifactId>
49                 <version>Dalston.RC1</version>
50                 <type>pom</type>
51                 <scope>import</scope>
52             </dependency>
53         </dependencies>
54     </dependencyManagement>
55 
56     <build>
57         <plugins>
58             <plugin>
59                 <groupId>org.springframework.boot</groupId>
60                 <artifactId>spring-boot-maven-plugin</artifactId>
61             </plugin>
62         </plugins>
63     </build>
64 
65     <repositories>
66         <repository>
67             <id>spring-milestones</id>
68             <name>Spring Milestones</name>
69             <url>https://repo.spring.io/milestone</url>
70             <snapshots>
71                 <enabled>false</enabled>
72             </snapshots>
73         </repository>
74     </repositories>
75 
76 
77 </project>
Copy the code

2. Create a spring boot application as a client

Copy the code
 1 package springcloud.helloworld.config.client;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 @SpringBootApplication
10 @RestController
11 public class ConfigClientApplication {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(ConfigClientApplication.class, args);
15     }
16 
17     @Value("${hello}")
18     String hello;
19     @RequestMapping(value = "/hello")
20     public String hello(){
21         return hello;
22     }
23 }
Copy the code

This application is very simple, is to get the value of the configuration item hello from the Config Server, Client Server submit a request to the REST Config Server later, Config Server to access GIT server, the value of configuration items hello and get back to the client.

3. Config client requires an application configuration file that defines the config Server's URL, as well as specific GIT branch to be accessed. This profile is bootstrap.yml (or bootstrap.properties)

Copy the code
 1 spring:
 2   application:
 3     name: config-client
 4   cloud:
 5     config:
 6       label: master
 7       profile: dev
 8       uri: http://localhost:8888/
 9 server:
10   port: 8881
Copy the code

This configuration defines the name of the application is config-client (which is to be used in front of the assembled Config Server to a question in the application), profile using dev, GIT branch with master. url is the config server address. So the question is, we do not seem to define the profile name, then the name of the configuration file what is it? This in turn reflects the idea of ​​convention over configuration, where Spring Cloud convention, the application configuration file name composed in the following manner: {application} - {profile} .properties (or {application} - {profile} .yml). For example, we use this configuration file is config-client-dev.properties we only need to create a profile spring-cloud / helloworldConfig / config-client-dev.properties in GIT on it, reads as follows:

hello=Hello World from GIT

 4. Start config-client applications can access http: // locahost / 8881 / hello, you can see, the application itself does not directly configure hello specific content, did not specify a specific configuration file, these desired by the spring cloud framework presented to the config server.

 

The configuration update

At this point, configuration management simple example spring cloud has been completed, but the client does not automatically sense changes in the service side. For example, we modified the contents of the file in the GIT, but in any case refresh the client side of the page, can not reflect changes in the configuration. The following section describes the configuration of Spring Cloud auto-update mechanism

Guess you like

Origin www.cnblogs.com/7788IT/p/11324214.html