array(2) { ["docs"]=> array(10) { [0]=> array(10) { ["id"]=> string(3) "428" ["text"]=> string(77) "Visual Studio 2017 单独启动MSDN帮助(Microsoft Help Viewer)的方法" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(8) "DonetRen" ["tagsname"]=> string(55) "Visual Studio 2017|MSDN帮助|C#程序|.NET|Help Viewer" ["tagsid"]=> string(23) "[401,402,403,"300",404]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400964" ["_id"]=> string(3) "428" } [1]=> array(10) { ["id"]=> string(3) "427" ["text"]=> string(42) "npm -v;报错 cannot find module "wrapp"" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "zzty" ["tagsname"]=> string(50) "node.js|npm|cannot find module "wrapp“|node" ["tagsid"]=> string(19) "[398,"239",399,400]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400760" ["_id"]=> string(3) "427" } [2]=> array(10) { ["id"]=> string(3) "426" ["text"]=> string(54) "说说css中pt、px、em、rem都扮演了什么角色" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(12) "zhengqiaoyin" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400640" ["_id"]=> string(3) "426" } [3]=> array(10) { ["id"]=> string(3) "425" ["text"]=> string(83) "深入学习JS执行--创建执行上下文(变量对象,作用域链,this)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "Ry-yuan" ["tagsname"]=> string(33) "Javascript|Javascript执行过程" ["tagsid"]=> string(13) "["169","191"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511399901" ["_id"]=> string(3) "425" } [4]=> array(10) { ["id"]=> string(3) "424" ["text"]=> string(30) "C# 排序技术研究与对比" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "vveiliang" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(8) ".Net Dev" ["catesid"]=> string(5) "[199]" ["createtime"]=> string(10) "1511399150" ["_id"]=> string(3) "424" } [5]=> array(10) { ["id"]=> string(3) "423" ["text"]=> string(72) "【算法】小白的算法笔记:快速排序算法的编码和优化" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "penghuwan" ["tagsname"]=> string(6) "算法" ["tagsid"]=> string(7) "["344"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511398109" ["_id"]=> string(3) "423" } [6]=> array(10) { ["id"]=> string(3) "422" ["text"]=> string(64) "JavaScript数据可视化编程学习(二)Flotr2,雷达图" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "chengxs" ["tagsname"]=> string(28) "数据可视化|前端学习" ["tagsid"]=> string(9) "[396,397]" ["catesname"]=> string(18) "前端基本知识" ["catesid"]=> string(5) "[198]" ["createtime"]=> string(10) "1511397800" ["_id"]=> string(3) "422" } [7]=> array(10) { ["id"]=> string(3) "421" ["text"]=> string(36) "C#表达式目录树(Expression)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "wwym" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(4) ".NET" ["catesid"]=> string(7) "["119"]" ["createtime"]=> string(10) "1511397474" ["_id"]=> string(3) "421" } [8]=> array(10) { ["id"]=> string(3) "420" ["text"]=> string(47) "数据结构 队列_队列实例:事件处理" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "idreamo" ["tagsname"]=> string(40) "C语言|数据结构|队列|事件处理" ["tagsid"]=> string(23) "["246","247","248",395]" ["catesname"]=> string(12) "数据结构" ["catesid"]=> string(7) "["133"]" ["createtime"]=> string(10) "1511397279" ["_id"]=> string(3) "420" } [9]=> array(10) { ["id"]=> string(3) "419" ["text"]=> string(47) "久等了,博客园官方Android客户端发布" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(3) "cmt" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511396549" ["_id"]=> string(3) "419" } } ["count"]=> int(200) } 222 Spring-Cloud之Config配置中心-7 - 爱码网

  一、我们前面基本上都是讲解的Spring Cloud Netflix的组件,下面我们会重点说Spring Cloud Config分布式配置中心。为什么需要这个组件来管理配置呢?在分布式应用开发过程中我们的大小服务会特别多,如果一个个去配置的话会带来更多的工作量,所以Spring Cloud 催生一种集中配置的方式,这种方式就是Config组件。这个主键主要介绍一下几个部分。

  1)Config Server从本地读取配置。

  2)Config Server从远程仓库Git上面读取配置。

  3)搭建高可用的Config Server集群。

  4)使用Spring Cloud Bus刷新配置。

  二、Config Server从本地读取配置。

  1、Config Server编写。

  1)首先在Springboot和SpringCloud的基础上加入一下依赖。

        <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>

  2)编写启动项

package com.cetc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

  3)编写配置application.yaml

server:
  port: 8683
#  port: 8684
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared # 指定本地配置目录
  profiles:
    active: native # 本地读取配置

  说明:这里profiles.active这里一定要配置native从本地读取。spring.cloud.server.native.search-locations为指定扫描的路径。

  4)编写需要配置的客户端配置文件config-client.yaml

  Spring-Cloud之Config配置中心-7

server:
  port: 8685
version: 1.0

  说明:config-client必须和服务名称一样。如何存在不同环境,比如:dev,test,prod等环境。可以通过spring.profiles.active来激活指定文件。

  服务端的配置就完成了。那么下面编写客户端。

  2、编写Config Client客户端。

  1)加入相关配置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>

  2)编写启动项和测试接口。

package com.cetc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}
package com.cetc.web.rest;

import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/test")
public class TestResource {

    @Value("${version}")
    private String version;

    @GetMapping("/getVersion")
    public String getVersion() {
        return version;
    }
}

  3)重点来了,这里我们不编写application.yaml类似的文件了,这里我们使用bootstrap.yaml。为什么是bootstrap.yaml,因为bootstrap相对于application有优先执行权

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://127.0.0.1:8683
      fail-fast: true

  说明:这里的spring.cloud.config的uri为服务配置地址。fail-fast为快速失败配置。

  3、测试。启动Config Server、Config Client端口为8683,8685

  Config Client启动过程节点。

  Spring-Cloud之Config配置中心-7

   Spring-Cloud之Config配置中心-7

   测试接口:

  Spring-Cloud之Config配置中心-7

   三、Config Server从Git仓库获取配置文件。可以自己使用github,也可以自己打架gitlab服务。我这里是自己搭建的gitlab。

  1)上面的工程基本上都是一样的,需要修改的地方就是Config Server 的application.yaml配置文件,修改如下:

server:
  port: 8683
#  port: 8684
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: http://127.0.0.1/lilin/SpringCloudConfig.git
          search-paths: shared
          username: lilin
          password:
      label: master

  说明:一般配置文件都是私有的,所以一般都是使用账号登录,如果需要公开的可以使用github等方式。label:为那个分支。search-paths为搜索路径。

  2)在Git上面编写config-client.yaml配置文件,为了方便我把之前的传上Git了。

   Spring-Cloud之Config配置中心-7

   3)启动Config ServerConfig Client,端口8683,8685

  Config Server的展示如下:

  Spring-Cloud之Config配置中心-7

   客户端和从本地获取没有什么区别。

  测试接口如下:

  Spring-Cloud之Config配置中心-7

   四、构建高可用的Config Server集群。为了方便管理,配置采用注册方式,所以需要加入Eureka服务注册中心。这里我们还是使用以前的注册中心Eureka Server端口8670。结构图如下

  Spring-Cloud之Config配置中心-7

  1、改造Config Server

  1)在上面工程中,加入Eureka Client依赖。

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

  2)在上面工程中,加入启动配置

package com.cetc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

  3)在上面工程基础上,修改配置。

server:
  port: 8683
#  port: 8684
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: http://127.0.0.1/lilin/SpringCloudConfig.git
          search-paths: shared
          username: lilin
          password:
      label: master
eureka:
  client:
    service-url:
      defaultZone:
        http://127.0.0.1:8670/eureka/ # 实际开发中建议使用域名的方式

  2、改造Config Client.

  1)在上面工程基础上,加入Euraka Client依赖。

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

  2)在上面工程基础上,修改启动项。

package com.cetc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

  3)修改bootstrap.yaml配置文件。

spring:
  application:
    name: config-client
  cloud:
    config:
      fail-fast: true
      discovery:
        enabled: true
        service-id: config-server
eureka:
  client:
    service-url:
      defaultZone:
        http://127.0.0.1:8670/eureka/ # 实际开发中建议使用域名的方式

  3、启动Eureka Server、2个Config Server、Config Client。端口8670,8683/8684、8685。

  Spring-Cloud之Config配置中心-7

   可以得知,启动一切正常。

  测试接口:

  Spring-Cloud之Config配置中心-7

   五、使用Spring Cloud bus刷新配置。

  1)简介:Spring Cloud Bus 是用轻量的消息代理将分布式的节点连接起来,可以用于广播配置文件的更改或者服务的监控管理。一个关键的意思就是,消息总线可以为微服务总监控,也可以实现应用之间的通信。Spring Cloud Bus 可以选择的组件有RabbitMQ,AMQPKafka等。我们这里使用RabbitMQ来实现消息总线的管理。
  2)为什么需要Spring Cloud Bus去刷新配置呢?如果有几十个微服务,并且每个微服务都存在很多实例,当更改配置时,需要重启很多微服务实例,会特别麻烦。Spring Cloud Bus就将这个过程进行了简化,当远程Git配置文件修改后,我们只需要向某一个微服务实例发送一个POST请求,通过消息组件通知其他微服务重新拉取配置文件。请求接口为:/actuator/bus-refresh(不同版本可能存在差异)。
  Spring-Cloud之Config配置中心-7
  3)我们开始改造Config Client。
  (1)首先需要加入两个依赖。
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

  (2)修改配置文件bootstrap.yaml。

spring:
  application:
    name: config-client
  cloud:
    config:
      fail-fast: true
      discovery:
        enabled: true
        service-id: config-server
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: admin
    password: admin
eureka:
  client:
    service-url:
      defaultZone:
        http://127.0.0.1:8670/eureka/ # 实际开发中建议使用域名的方式
management:
  endpoints:
    web:
      exposure:
        include: ["*"]

  说明:目前Spring 2.0版本是通过actuator的接口进行外部访问的所以在使用的时候,一定要配置management.endpoints.web.exposure.include。

  (3)在需要刷新配置的地方加入@RefreshScope注解,注意这个注解只针对于当前类生效。我们这里是获取的版本号所以在TestResource类加上。

package com.cetc.web.rest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/test")
@RefreshScope
public class TestResource {

    @Value("${version}")
    private String version;

    @GetMapping("/getVersion")
    public String getVersion() {
        return version;
    }
}

  (4)测试。启动Eureka Server,2个Config Server,2个Config Client端口分别为8670,8683/8684,8685/8686。

  Spring-Cloud之Config配置中心-7

   访问两个接口为:

  Spring-Cloud之Config配置中心-7

  (5)刷新配置。我们修改Git的上面config-client.yaml的版本号为2.0。

  Spring-Cloud之Config配置中心-7

 

   通过PostMan发送POST请求http://127.0.0.1:8685/actuator/bus-refresh,进行刷新。

  Spring-Cloud之Config配置中心-7

 

 

   发送请求测试,等待刷新:

  Spring-Cloud之Config配置中心-7

  从上面可以看出,我想一个接口发送了刷新请求,结果全部实例都进行了刷新,这样的结果好处就是。一次更改,全部可刷新。

  说明:通过对启动接口:

  Spring-Cloud之Config配置中心-7

 

 

   我们可以得知,刷新是可以带参数的。而这个参数的意思就是刷新特定实例参数。

  比如:http://127.0.0.1:8685/actuator/bus-refresh?destination=config-client:**

   六、总结:

  1)Spring Cloud Config简单来说就是对配置的简单化,让配置集中化。

  2)Spring Cloud Config通过Eureka可以实现集群化,达到高可用。

  3)通过对客户端进行改造加入Bus总线,可以实现一次更改,多个服务实例的更新。避免了多个服务重启的问题。

  4)Spring Cloud Config的配置获取方式有:本地(native)、Git、SVN、vault等方式。

  七、源码地址:https://github.com/lilin409546297/spring-cloud/tree/master/config

相关文章: