5.4 SpringCloud build a distribution center as well as problem-solving> My program ape road: Fourth fourteen chapters

First, the official website

  Description [English]

 

 

  Description [Chinese]

 

 

   Popular Chinese [explain] - explain the source: --https: //blog.csdn.net/forezp/article/details/81041028

In a distributed system, due to the huge number of multi-service, service profile in order to facilitate unified management, real-time updates, so they need the central component of a distributed configuration. In Spring Cloud, there is a central component of a distributed configuration spring cloud config, which supports the configuration service in memory configuration services (ie local), also supported on the remote Git repository. In the spring cloud config assembly, the two roles, one config server, the second is config client.

Second, the configuration center server configuration and startup, as follows:

Step 1: Create a SpringBoot project, named Config-server, as follows:

 

 

 Step Two: introducing dependent (POM below)

 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.2.2.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.example</groupId>
12     <artifactId>config</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>config</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19         <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
20     </properties>
21 
22     <dependencies>
23         <dependency>
24             <groupId>org.springframework.cloud</groupId>
25             <artifactId>spring-cloud-config-server</artifactId>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.cloud</groupId>
29             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
30         </dependency>
31 
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-test</artifactId>
35             <scope>test</scope>
36             <exclusions>
37                 <exclusion>
38                     <groupId>org.junit.vintage</groupId>
39                     <artifactId>junit-vintage-engine</artifactId>
40                 </exclusion>
41             </exclusions>
42         </dependency>
43     </dependencies>
44 
45     <dependencyManagement>
46         <dependencies>
47             <dependency>
48                 <groupId>org.springframework.cloud</groupId>
49                 <artifactId>spring-cloud-dependencies</artifactId>
50                 <version>${spring-cloud.version}</version>
51                 <type>pom</type>
52                 <scope>import</scope>
53             </dependency>
54         </dependencies>
55     </dependencyManagement>
56 
57     <build>
58         <plugins>
59             <plugin>
60                 <groupId>org.springframework.boot</groupId>
61                 <artifactId>spring-boot-maven-plugin</artifactId>
62             </plugin>
63         </plugins>
64     </build>
65 
66 </project>

下载依赖完成后

第三步:启动类配置注解

       配置:@EnableConfigServer   如下:

 1 package com.example.config;
 2 import org.springframework.boot.SpringApplication;
 3 import org.springframework.boot.autoconfigure.SpringBootApplication;
 4 import org.springframework.cloud.config.server.EnableConfigServer;
 5 @SpringBootApplication
 6 @EnableConfigServer
 7 public class ConfigApplication {
 8     public static void main(String[] args) {
 9         SpringApplication.run(ConfigApplication.class, args);
10     }
11 
12 }

  第四步:编写application.properties配置文件  

1 server.port=8760
2 spring.application.name=Config-Server
3 #用的是gitlab
4 spring.cloud.config.server.git.uri=https://git.lug.ustc.edu.cn/fanyuyi/hello.git
5 spring.cloud.config.server.git.search-paths=respo
6 spring.cloud.config.label=master
7 spring.cloud.config.server.git.username=
8 spring.cloud.config.server.git.password=
9 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

git配置

 

 

 

1.spring.cloud.config.server.git.uri=

 

   2.spring.cloud.config.server.git.search-paths=

 

 

 

 3.spring.cloud.config.label=

 

 

   4.dev

第五步:启动

       1.启动Eureka注册中心

       2.启动Config-Server

第六步:访问

http://localhost:8760/aa/dev             (aa是随便写的)得到如下图的json

三、配置中心客户端配置并启动、如下:

第一步:创建一个SpringBoot工程,名称为Config-Client,如下:

 第二步:引入依赖(pom如下)

 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.2.2.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.example</groupId>
12     <artifactId>client</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>client</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19         <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
20     </properties>
21 
22     <dependencies>
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-starter-web</artifactId>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.cloud</groupId>
29             <artifactId>spring-cloud-starter-config</artifactId>
30         </dependency>
31         <dependency>
32             <groupId>org.springframework.cloud</groupId>
33             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
34         </dependency>
35 
36         <dependency>
37             <groupId>org.springframework.boot</groupId>
38             <artifactId>spring-boot-starter-test</artifactId>
39             <scope>test</scope>
40             <exclusions>
41                 <exclusion>
42                     <groupId>org.junit.vintage</groupId>
43                     <artifactId>junit-vintage-engine</artifactId>
44                 </exclusion>
45             </exclusions>
46         </dependency>
47     </dependencies>
48 
49     <dependencyManagement>
50         <dependencies>
51             <dependency>
52                 <groupId>org.springframework.cloud</groupId>
53                 <artifactId>spring-cloud-dependencies</artifactId>
54                 <version>${spring-cloud.version}</version>
55                 <type>pom</type>
56                 <scope>import</scope>
57             </dependency>
58         </dependencies>
59     </dependencyManagement>
60 
61     <build>
62         <plugins>
63             <plugin>
64                 <groupId>org.springframework.boot</groupId>
65                 <artifactId>spring-boot-maven-plugin</artifactId>
66             </plugin>
67         </plugins>
68     </build>
69 
70 </project>

第三步:写一个controller

 

 

 

 1 package com.example.client.web;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestParam;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @RestController
 9 public class MyControler {
10 
11     @Value("${foo}")
12     String foo;
13 
14     @RequestMapping("/hi")
15     public String web(@RequestParam(name="name",defaultValue = "lisi") String name){
16         return "hello my name is ["+name+"]"+"-"+foo;
17     }
18 }

第四步:编写application.properties配置文件 ,不,是bootstrap.priperties配置文件

 

 

 

1 server.port=8080
2 spring.cloud.config.profile=dev
3 spring.cloud.config.label=master
4 spring.cloud.config.uri=http://localhost:8760/
5 spring.application.name=Client
6 eureka.client.service-url.defaultZone=http://localhost:8761/eureka

第五步:启动

       1.Eureka注册中心-已启动、Config-Server-已启动

       2.启动Config-Client

第六步:访问

          http://localhost:8080/hi

 

 

 四、说明

   1.Http 服务有以下形式的资源:

(来自官网)--需细读理解
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

2.配置git(官网说明)
Spring cloud 配置服务器从各种来源为远程客户端提供配置。 下面的示例从 git 仓库获取配置(必须提供) ,如下面的示例所示:

  

五、其他博客问题归纳

1.解决了默认为config配置端口8888覆盖问题
     把application.properties改为bootstrap.properties 解决了默认为config配置端口8888覆盖问题

2.不加载config服务
     client的pom配置中不能加spring-cloud-config-server

3.远程gitlab仓库的{application}-{profile}.properties名称为application-dev.properties。application改成别的会找不到配置文件

 

Guess you like

Origin www.cnblogs.com/fanyuyi-boke/p/qiao_duo_shao_nian_dai_ma_neng_ba_shou_zhi_mo_ping44.html