Spring Cloud 入门教程 - 搭建配置中心服务

简介

Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等。咱们没必要先知道每一个组件有什么做用,随着教程的深刻,咱们会逐渐接触到它们。一个分布式服务大致结构见下图(图片来自于:spring.io):前端

使用Spring Cloud搭建分布式的系统十分简单,咱们只须要几行简单的配置就能启动一系列的组件,而后能够在代码中控制、使用和管理这些组件。Spring Cloud使用Spring Boot做为基础框架,能够参考个人上一篇博客介绍如何建立一个Spring Boot项目, Spring Boot 2.0.1 入门教程。本教程将教你们如何配置服务中心服务,并经过web客户端读取配置。java

基础环境

  • JDK 1.8git

  • Maven 3.3.9github

  • IntelliJ 2018.1web

  • Gitspring

项目源码

Gitee码云apache

建立 Web Client

首先用IntelliJ建立一个Maven项目,pom.xml文件内容以下:json


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?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>cn.zxuqian</groupId>
   <artifactId>web</artifactId>
   <version>1.0-SNAPSHOT</version>

   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.1.RELEASE</version>
   </parent>

   <dependencies>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-config</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-web</artifactId>
       </dependency>
   </dependencies>

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Finchley.M9</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>

   <properties>
       <java.version>1.8</java.version>
   </properties>


   <build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>

   <repositories>
       <repository>
           <id>spring-milestones</id>
           <name>Spring Milestones</name>
           <url>https://repo.spring.io/libs-milestone</url>
           <snapshots>
               <enabled>false</enabled>
           </snapshots>
       </repository>
   </repositories>

</project>


  • dependencyManagement 能够为全部的依赖指定统一的版本号,这里的Spring-cloud依赖版本均为Finchley.M9,而后使用repository指定此版本的仓库。bootstrap

  • spring-cloud-starter-config提供了访问配置中心服务的API接口。api

添加控制器

新建一个控制器类 cn.zxuqian.controllers.HelloController, 并添加以下代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package cn.zxuqian.controllers;

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;

@RefreshScope
@RestController
public class HelloController {

   @Value("${message: 本地消息}")
   private String message;

   @RequestMapping("/message")
   public String message() {

       return this.message;
   }

}


一个简单的控制器,匹配/message路径,并返回message变量的值。这里先不用管 @RefreshScope 这个注解,等下会用到时再讲。@Value会取来自配置中心服务的配置项,或本地环境变量等等,此处取了配置中心的message的值,并给了它一个默认值”本地消息”,即若是远程配置中心不可用时,此变量将会用默认值初始化。

添加配置文件

bootstrap.xml

咱们须要在web客户端项目彻底启动以前去加载配置中心的配置项,因此须要在src/main/resources下建立bootstrap.yml文件,而后指定此客户端的名字和远程配置中心的uri:


1
2
3
4
5
6
spring:
 application:
   name: web-client
 cloud:
   config:
     uri: http://localhost:8888


yml相比properties文件更加简洁,不用写不少重复的前缀,上边的内容能够转换为对应的properties:


1
2
spring.application.name=web-client
spring.cloud.config.uri=http://localhost:8888


  • spring.application.name指定了此项目的名字,用来取配置中心相同文件名的配置文件,即配置中心应有一文件名为web-client.yml的配置文件。

  • spring.cloud.config.uri指定了远程配置中心服务的uri地址,默认为http://localhost:8888

建立配置中心项目

新建一个Maven项目,使用以下pom配置:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?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>cn.zxuqian</groupId>
   <artifactId>config-server</artifactId>
   <version>1.0-SNAPSHOT</version>

   <packaging>jar</packaging>

   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.1.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <java.version>1.8</java.version>
   </properties>

   <dependencies>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-config-server</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>Finchley.M9</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>

   <repositories>
       <repository>
           <id>spring-milestones</id>
           <name>Spring Milestones</name>
           <url>https://repo.spring.io/libs-milestone</url>
           <snapshots>
               <enabled>false</enabled>
           </snapshots>
       </repository>
   </repositories>
</project>


spring-cloud-config-server即为配置中心服务的核心依赖。

配置Application

新建一个Java类cn.zxuqian.Application,添加以下代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
package cn.zxuqian;

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);
   }
}


  • 使用@EnableConfigServer这一条注解便可把该Maven项目做为配置中心服务启动。

新建Git仓库

配置中心的文件都是基于版本控制的,因此须要在本地新建一个git仓库来保存配置文件。或者也可用公共远程git仓库,github, 码云等。在任意位置(如项目的上级目录)建立一空白文件夹,这里叫作config,可使用任何名字。而后进入此文件夹下,运行


1
$ git init


来初始化git仓库,而后新建一个文件,名为web-client.yml,并添加以下内容:


1
message: 此条消息来自于cofig server


注意此文件名须要和以前在web项目中配置的spring.application.name保持一致。这个文件的内容就是web客户端要获取的message的值。建立完成以后,运行以下git命令提交到本地仓库:


1
2
$ git add web-client.yml
$ git commit -m "added web-client.yml"


配置git仓库位置

咱们须要为配置中心指定上述建立的git仓库地址。在src/main/resources下建立applicaiton.yml文件,提供以下内容:


1
2
3
4
5
6
7
8
server:
 port: 8888
spring:
 cloud:
   config:
     server:
       git:
         uri: ${HOME}/development/codes/backend/gitee/config


此文件指定了配置中心服务的端口号,和保存配置文件的git仓库目录,若是是远程仓库,能够直接指定url地址。到此,配置中心服务建立完成。

测试

首先启动配置中心服务,使用spring-boot maven插件:spring-boot:run。启动成功后再启动web客户端,访问http://localhost:8080/message,若是看到此条消息来自于cofig server即配置成功。而后关闭配置中心服务,再重启web客户端,访问http://localhost:8080/message,咱们就会看到本地消息

动态更新配置

那么每次配置更新后都要重启是否是很麻烦?Spring boot提供了spring-boot-starter-actuator组件,用来进行生产环境的维护,如检查健康信息等。还记得上面HelloController@RefreshScope注解吗?使用它咱们能够动态的加载配置中心修改后的配置。而后咱们还须要在配置中心的web-client.yml添加以下内容用以暴露acurator的/refresh终端api。


1
2
3
4
5
6
7
message: 此条消息来自于cofig server

management:
 endpoints:
   web:
     exposure:
       include: "*"


更新完成后提交的git仓库,而后重启配置中心服务和web客户端。修改message为


1
message: 更新:此条消息来自于cofig server


而后发送一个空的post请求到/refresh


1
$ curl http://localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json"


以后刷新页面,便可看到更新后的消息。

总结

如今咱们搭建好了一个配置中心服务,它是根据每一个组件的spring.application.name来决定读取哪一个配置文件,而后咱们用了acurator的/refreshapi在运行时刷新配置项。另外配置项都是基于版本控制的,能够方便的进行还原和更新。经过这个教程能够看到Spring Cloud的各组件的配置至关简单,基本就只用一条注解就能够建立一个完整的服务组件。


本文分享自微信公众号 - 峰华前端工程师(qiantu_me)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索