简介

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

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

基础环境

  • JDK 1.8
  • Maven 3.3.9
  • IntelliJ 2018.1
  • Git

项目源码

Gitee码云

创建 Web Client

首先用IntelliJ创建一个Maven项目,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>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指定此版本的仓库。
  • spring-cloud-starter-config提供了访问配置中心服务的API接口。

添加控制器

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

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:

spring:
application:
name: web-client
cloud:
config:
uri: http://localhost:8888

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

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配置:

<?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,添加如下代码:

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,可以使用任何名字。然后进入此文件夹下,运行

$ git init

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

message: 此条消息来自于cofig server

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

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

配置git仓库位置

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

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。

message: 此条消息来自于cofig server

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

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

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

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

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

之后刷新页面,即可看到更新后的消息。

总结

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

欢迎访问我的博客原文:Spring Cloud 入门教程 - 搭建配置中心服务

Spring Cloud 入门教程 - 搭建配置中心服务的更多相关文章

  1. Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务

    前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息“hello world”. 但自己的配置文件中必须配置config serve ...

  2. Spring Cloud 入门教程(一): 服务注册

    1.  什么是Spring Cloud? Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁 ...

  3. Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务

    首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...

  4. Spring Cloud 入门教程(三): 配置自动刷新

    之前讲的配置管理, 只有在应用启动时会读取到GIT的内容, 之后只要应用不重启,GIT中文件的修改,应用无法感知, 即使重启Config Server也不行. 比如上一单元(Spring Cloud ...

  5. Spring Cloud 入门教程(九): 路由网关zuul

    在微服务架构中,需要几个关键的组件,服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个组件可以组建一个简单的微服务架构.客户端的请求首先经过负载均衡(zuul.Ngnix),再 ...

  6. Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡

    接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...

  7. Spring Cloud 入门教程(二): 配置管理

    使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring ...

  8. Spring Cloud入门教程(二):客户端负载均衡(Ribbon)

    对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...

  9. Spring Cloud 入门教程(七): 熔断机制 -- 断路器

    对断路器模式不太清楚的话,可以参看另一篇博文:断路器(Curcuit Breaker)模式,下面直接介绍Spring Cloud的断路器如何使用. SpringCloud Netflix实现了断路器库 ...

随机推荐

  1. 创建一个QT for Android的传感器应用应用程序(摘自笔者2015年将出的《QT5权威指南》,本文为试读篇)

     这个手册描述了使用Qt Quick面访的方式在Android和ios设备上开发QtQuick应用程序的方法.我们使用Qt Creator实现一个QtQuick应用程序,这个应用程序基于加速器的值 ...

  2. UNIX环境高级编程——主线程与子线程的退出关系

    我们在一个线程中经常会创建另外的新线程,如果主线程退出,会不会影响它所创建的新线程呢?下面就来讨论一下. 1.  主线程等待新线程先结束退出,主线程后退出.正常执行. 示例代码: #include & ...

  3. 一种公认提供toString的方法_JAVA核心技术卷轴Ⅰ

    从JAVA核心技术卷轴Ⅰ:基础知识中整理得到. import java.lang.reflect.AccessibleObject; import java.lang.reflect.Array; i ...

  4. C#之DirectoryInfo操作

    在C#中的System.IO命名空间下有大量的库供我们使用,下面一起来看一下DirectoryInfo的使用吧. code: using System; using System.Collection ...

  5. 阿里电话面试问题----100万个URL如何找到出现频率最高的前100个?

    内推阿里电话面试中面试官给我出的一个题: 我想的头一个解决方案,就是放到stl 的map里面对出现的频率作为pair的第二个字段进行排序,之后按照排序结果返回: 下面口说无凭,show your co ...

  6. DB 查询分析器 方便地创建DB2自定义函数

    DB 查询分析器 方便地创建DB2自定义函数                           马根峰            (广东联合电子服务股份有限公司, 广州 510300) 摘要       ...

  7. FFmpeg深入分析(一)

    最近在做一个关于监控的项目,要在iphone 客户端实现播放监控的实时视频以及录像视频.使用到了FFmpeg,看到这篇文章,写的非常不错.转自:http://blog.chinaunix.net/ui ...

  8. BottomSheet底部动作条使用

    底部动作条 底部动作条(Bottom Sheets)是一个从屏幕底部边缘向上滑出的一个面板,使用这种方式向用户呈现一组功能.底部动作条呈现了简单.清晰.无需额外解释的一组操作. 使用环境 底部动作条( ...

  9. Linux nohup 命令

    Linux nohup 命令 如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令.该命令可以在你退出帐户之后继续运行相应的进程.nohup就是不挂起的意思(no ...

  10. SpriteBuilder中节点位置类型为百分比时不能定位的解决

    Ball.ccb类型是Node,其中有个子节点为Color Node,其中物理使能. MainScene.ccb中加入一个物理节点,将Ball.ccb拖入其中,成为该物理节点的孩子,这时出现了一个&q ...