zoukankan      html  css  js  c++  java
  • Spring Cloud 入门教程 搭建配置中心服务

    简介

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

    Spring cloud架构

    使用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 入门教程 - 搭建配置中心服务

  • 相关阅读:
    Windows10 下Apache服务器搭建
    Visual Studio 2019及其注册码
    清理300多台MySQL数据库的过期binlog日志
    JS获取和设置光标的位置
    在android客户端加载html源代码总结
    OpenGL ES2学习笔记(9)-- 转换矩阵
    可行性研究报告 之机房收费系统的可行性研究报告
    eclipse 配置Maven问题解决办法:新建maven工程时报错:Could not resolve archetype org.apache.maven.archetypes .
    常见的证书格式和相互转换
    九度OJ 题目1384:二维数组中的查找
  • 原文地址:https://www.cnblogs.com/zxuqian/p/8979656.html
Copyright © 2011-2022 走看看