zoukankan      html  css  js  c++  java
  • SpringCloud2.0 Config 分布式配置中心 基础教程(十一)

    Spring Cloud Config 简介

    Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring Environment和PropertySource抽象,因此它们非常适合Spring应用程序,但可以与任何语言运行的任何应用程序一起使用。当应用程序通过部署管道从开发到测试并进入生产时,您可以管理这些环境之间的配置,并确保应用程序具有迁移时需要运行的所有内容。服务器存储后端的默认实现使用git,因此它可以轻松支持配置环境的标记版本,以及可用于管理内容的各种工具。添加替代实现并使用Spring配置插入它们很容易。

    Spring Cloud Config 特点

    Spring Cloud Config Server 功能:

    • 用于外部配置的HTTP,基于资源的API(名称 - 值对或等效的YAML内容)
    • 加密和解密属性值(对称或非对称)
    • 使用可轻松嵌入Spring Boot应用程序 @EnableConfigServer

    Spring Cloud Config Client 功能:

    • 绑定到Config Server并Environment使用远程属性源初始化Spring
    • 加密和解密属性值(对称或非对称)

    实践操作

    1、创建【配置中心服务端】,即 Spring Cloud Server

    1.1、新建 Spring Boot 工程,工程名称:springcloud-config-server

    1.2、工程 pom.xml 文件添加如下依赖:

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    1.3、在工程启动类中,添加注解 @EnableConfigServer

    package com.miniooc.configserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    /**
     * ConfigServerApplication
     *
     * @author 宋陆
     * @version 1.0.0
     */
    @EnableConfigServer
    @SpringBootApplication
    public class ConfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    
    }
    

    1.4、新建工程配置文件 application.yml ,配置内容(加载本地配置方案):

    server:
      port: 13801
    
    spring:
      application:
        name: config-server
      profiles:
        active: native # 加载本地配置
      cloud:
        config:
          server:
            native:
              # 不指定路径的话,默认搜索 resources 目录
              search-locations: C:/openspace/springcloud-config-server/config/
    
    eureka:
      instance:
        hostname: localhost
        # 表示eureka client间隔多久去拉取服务注册信息,默认为30秒,如果要迅速获取服务注册状态,可以缩小该值
        lease-renewal-interval-in-seconds: 15
        # 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
        # 默认为90秒
        # 如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。
        # 如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。
        # 该值至少应该大于 leaseRenewalIntervalInSeconds
        lease-expiration-duration-in-seconds: 45
      client:
        serviceUrl:
          defaultZone: http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
    

    上述配置内容,为Config Server加载【本地配置】方案

    2、创建【配置中心客户端】,即 Spring Cloud Client

    2.1、新建 Spring Boot 工程,工程名称:springcloud-config-client

    2.2、工程 pom.xml 文件添加如下依赖:

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    2.3、新建工程配置文件 bootstrap.yml (注:不是application.yml),配置内容:

    spring:
      application:
        name: config-client
      cloud:
        config:
          label: master
          profile: dev
          fail-fast: true
          uri: http://localhost:13801
    

    2.4、新建工程从服务端请求的配置文件 config-client-dev.yml

    客户端从服务端获取资源配置的路径规则如下:

    • /{application}/{profile}[/{label}]
    • /{application}-{profile}.yml
    • /{label}/{application}-{profile}.yml
    • /{application}-{profile}.properties
    • /{label}/{application}-{profile}.properties

    本例,使用的是第二条规则,命名文件。

    配置内容:

    server:
      port: 52601
    
    spring:
      application:
        name: config-client
    
    eureka:
      instance:
        hostname: localhost
        # 表示eureka client间隔多久去拉取服务注册信息,默认为30秒,如果要迅速获取服务注册状态,可以缩小该值
        lease-renewal-interval-in-seconds: 15
        # 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
        # 默认为90秒
        # 如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。
        # 如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。
        # 该值至少应该大于 leaseRenewalIntervalInSeconds
        lease-expiration-duration-in-seconds: 45
      client:
        serviceUrl:
          defaultZone: http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
    # 配置客户端输出演示用
    info: local-config-client-dev
    

    config-client-dev.yml 文件放置在 1.4 小节 search-locations 指定的目录 C:/openspace/springcloud-config-server/config/ 中,如果,服务端不配置该参数,就放在默认目录,即服务端的resources根目录中。

    2.5、创建【配置客户端】控制器类 EurekaClientController

    package com.miniooc.configclient.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * ConfigClientController
     *
     * @author 宋陆
     * @version 1.0.0
     */
    @RestController
    public class ConfigClientController {
    
        @Value("${info}")
        private String info;
    
        /**
         * 提供的一个restful服务
         *
         * @return 返回  配置中的info信息
         */
        @RequestMapping("/info")
        public String info() {
            return info;
        }
    }
    

    这个controller主要是为了演示是否成功读取到了【配置服务端】的配置文件。

    3、运行演示

    3.1、启动【服务中心】集群,工程名:springcloud-eureka-server

    3.2、启动【配置中心服务端】,工程名:springcloud-config-server

    3.3、启动【配置中心客户端】,工程名:springcloud-config-client

    3.4、打开浏览器,访问配置中心客户端restful服务,http://localhost:52601/info

    客户端成功从服务端获取到了资源文件,并进行了输出。以上的配置中心服务端是从本地加载资源文件发给客户端的。下面我们改造服务端,让服务端从远程git获取资源文件返回给客户端。

    4、修改【配置中心服务端】,从远程git加载资源文件

    4.1、修改【配置中心服务端】工程配置文件 application.yml ,配置内容(加载远程git配置方案):

    server:
      port: 13801
    
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              # 配置git仓库的地址
              uri: https://gitee.com/songchuanlu/springcloud-config-repo/
              # git仓库地址下的相对地址,可以配置多个,用,分割。
              # search-paths: config-repo
              # git仓库的账号
              # username:
              # git仓库的密码
              # password:
    
    eureka:
      instance:
        hostname: localhost
        # 表示eureka client间隔多久去拉取服务注册信息,默认为30秒,如果要迅速获取服务注册状态,可以缩小该值
        lease-renewal-interval-in-seconds: 15
        # 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
        # 默认为90秒
        # 如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。
        # 如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。
        # 该值至少应该大于 leaseRenewalIntervalInSeconds
        lease-expiration-duration-in-seconds: 45
      client:
        serviceUrl:
          defaultZone: http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
    

    4.2、重新启动【配置中心服务端】,工程名:springcloud-config-server

    4.3、重新启动【配置中心客户端】,工程名:springcloud-config-client

    4.4、刷新配置中心客户端restful服务,http://localhost:52601/info

    客户端成功从服务端获取到了资源文件,并进行了输出。且输出的信息是配置中心服务端从远程git仓库加载的资源文件。

    5、修改【配置中心客户端】,通过服务名访问【配置中心服务端】

    为了保证配置中心服务端的服务高可用,一般会进行集群配置。那么配置中心客户端,就要采取访问服务名的方式,访问配置中心服务端

    5.1、修改【配置中心客户端】工程配置文件 bootstrap.yml ,配置内容

    spring:
      application:
        name: config-client
      cloud:
        config:
          label: master
          profile: dev
          fail-fast: true
          #uri: http://localhost:13801
          discovery:
            enabled: true
            service-id: config-server
    

    5.2、重新启动【配置中心客户端】,工程名:springcloud-config-client

    5.3、刷新配置中心客户端restful服务,http://localhost:52601/info

  • 相关阅读:
    Android 侧滑(双向滑动菜单)效果
    Android中PopupWindow中有输入框时无法弹出输入法的解决办法
    Android 调用图库选择图片实现和参数详解
    5.抽象类篇
    4.事件篇
    3.委托篇
    2.结构篇
    1.枚举篇
    读取excel到数据库里面
    Windows系统安装docker
  • 原文地址:https://www.cnblogs.com/songlu/p/9989366.html
Copyright © 2011-2022 走看看