简介

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. 线程在Linux中的实现

          早在以前,我们就知道,CPU调度的基本单位是线程,而进程是拥有资源的基本单位,进程是用进程描述符表示的,那么线程是怎么实现和表示的呢?       线程机制是现代编程技术中常用的一种抽象概 ...

  2. Mybatis简单入门

    MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用 ...

  3. Java --Annotation学习心得体会及笔记

    相对于注释这种给程序员看的信息: 注解,就是给程序看的解释性的语言,其作用就相当于配置文件的存在.其存在的意义在于以下几点: 优点: 方便的使程序员看到相关项的关联位置及关联方式等信息. 缺点: 由于 ...

  4. 《java入门第一季》之有趣的集合小案例---获取10个【1-20之间】的随机数,要求不能重复。

    import java.util.ArrayList; import java.util.Random; /* * 获取10个[1-20之间]的随机数,要求不能重复.(注意:不是获取10个数,如果单纯 ...

  5. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  6. 关于MySQL insert into ... select 的锁情况

    摘要:       一直以为"insert into tb select * from tbx" 这样的导入操作是会把tbx表给锁住的,在锁期间是不允许任何操作(保证一致性).看完 ...

  7. iOS&nbsp;APP设计规范大全

    目前最为齐全的iOS APP设计规范大全,Mark一个- 欢迎参考本文,未经许可,严禁转载!

  8. org/w3c/dom/ElementTraversal 错误解决办法

    org/w3c/dom/ElementTraversal 错误解决办法 不记得之前几天把什么maven依赖包删除了,今天利用htmlunit运行代码的时候报了下面的错误: Exception in t ...

  9. MinerBean.java 数据库表 miner bean

    MinerBean.java 数据库表 miner bean package com.iteye.injavawetrust.miner; import java.util.Date; /** * 数 ...

  10. 经过一段的努力,终于成为CSDN博客专家,感谢大家支持

    感谢CSDN提供这么好的一个技术学习平台,通过各路大神的博客我成长了许多,同时也感谢支持我的朋友们,我会继续努力,用心去写好博客.还请继续关注我~ 谢谢!