第六篇:分布式配置中心(springcloudconfig)

PP孩的家      2022-04-07     645

关键词:

一、简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。

在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。

在spring cloud config 组件中,分两个角色,一是config server,二是config client。

二、构建Config Server

跟前篇一样,新建工程eureka-config-server。pom.xml如下:

<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>com.sun</groupId>
  <artifactId>eureka-config-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    
    <name>config-server</name>
    <description>Demo project for Spring Boot</description>
    
    <parent>
        <groupId>com.sun</groupId>
        <artifactId>springcloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <dependencies>
        <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>
    </dependencies>
</project>
View Code

在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,代码如下:

package com.sun;

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);
    }
}
View Code

需要在程序的配置文件application.yml文件配置以下:

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/sunfengjiajia/SpringcloudConfig  #配置git仓库地址
          searchPaths: respo                                      #配置仓库路径
          label: master                                           #配置仓库的分支
          username: 
          password: 
server:
  port: 8888
View Code

如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用。

远程仓库https://gitee.com/sunfengjiajia/SpringcloudConfig 中有个文件config-client-dev.properties文件中有一个属性:

foo = foo version 3

启动程序:访问http://localhost:8888/foo/dev,显示:

{"name":"foo","profiles":["dev"],"label":null,"version":"eb06f7707a76ebaabba2ef930a8d0b463875daa5","state":null,"propertySources":[]}

证明配置服务中心可以从远程程序获取配置信息。

http请求地址和资源文件映射如下:

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

三、构建一个config client

重新创建一个springboot项目,取名为eureka-config-client,其pom文件:

<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>com.sun</groupId>
  <artifactId>eureka-config-client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>
    
    <parent>
        <groupId>com.sun</groupId>
        <artifactId>springcloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <dependencies>
        <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>
    </dependencies>
</project>
View Code

其配置文件application.yml:

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      uri: http://localhost:8888/
server:
  port: 8881
View Code
  • spring.cloud.config.label 指明远程仓库的分支
  • spring.cloud.config.profile

    • dev开发环境配置文件
    • test测试环境
    • pro正式环境
  • spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址。

程序的入口类,写一个API接口“/hi”,返回从配置中心读取的foo变量的值,代码如下:

package com.sun;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/hi")
    public String hi(){
        return foo;
    }
}
View Code

打开网址访问:http://localhost:8881/hi,网页显示:

foo version 3

这就说明,config-client从config-server获取了foo的属性,而config-server是从git仓库读取的。

四、高可用的分布式配置中心(Spring Cloud Config)

  1、改造eureka-server工程

  前面讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用。

继续前面的config-server工程,添加依赖:

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

此时的pom.xml文件如下:

<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>com.sun</groupId>
  <artifactId>eureka-config-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    
    <name>config-server</name>
    <description>Demo project for Spring Boot</description>
    
    <parent>
        <groupId>com.sun</groupId>
        <artifactId>springcloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <dependencies>
        <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-server</artifactId>
        </dependency>
    </dependencies>
</project>
View Code

在配置文件application.yml中需要添加服务中心基本配置,如下:

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

此时的application.yml如下:

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/sunfengjiajia/SpringcloudConfig  #配置git仓库地址
          searchPaths: respo                                      #配置仓库路径
          label: master                                           #配置仓库的分支
          username: 
          password: 
          
server:
  port: 8888
  
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

  
View Code

  最后需要在程序的启动类Application加上@EnableEurekaServer的注解。启动类ConfigServerApplication:

  

package com.sun;

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

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

 

  2、改造config-client

将其注册微到服务注册中心,作为Eureka客户端,需要pom文件加上起步依赖spring-cloud-starter-netflix-eureka-client,代码如下:

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

pom.xml如下:

<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>com.sun</groupId>
  <artifactId>eureka-config-client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>
    
    <parent>
        <groupId>com.sun</groupId>
        <artifactId>springcloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <dependencies>
        <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>
    </dependencies>
</project>
View Code

配置文件application.yml。加上服务注册地址为http://localhost:8888/eureka/

application.yml如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      #uri: http://localhost:8888/
    discovery:
      enabled: true
      serviceId: config-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8881

 

  • spring.cloud.config.discovery.enabled 是从配置中心读取文件。
  • spring.cloud.config.discovery.serviceId 配置中心的servieId,即服务名。

这时发现,在读取配置文件不再写ip地址,而是服务名,这时如果配置服务部署多份,通过负载均衡,从而高可用。

给客户端启动类加上注解:@EnableEurekaClient

ConfigClientApplication启动类如下:

package com.sun;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/hi")
    public String hi(){
        return foo;
    }
}
View Code

依次启动config-server,config-client 

访问网址:http://localhost:8888/

访问http://localhost:8881/hi,浏览器显示:

foo version 3

史上最简单的springcloud教程|第六篇:分布式配置中心(springcloudconfig)

...务来保存各个服务的配置文件。它就是SpringCloudConfig。在分布式系统中 查看详情

史上最简单的springcloud教程|第六篇:分布式配置中心(springcloudconfig)(finchley版本)

...务来保存各个服务的配置文件。它就是SpringCloudConfig。在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在SpringCloud中,有分布式配置中心组件springcloudconfig,它支... 查看详情

跟我学springcloud(finchley版)-19-配置中心-springcloudco

...管理配置成为我们必须解决的问题。本节来讨论如何使用SpringCloudConfig管理配置。为什么要使用配置中心集中管理配置。一个使用微服务架构的应用系统可能会包含成百上千个微服务,因此集中管理配置是非常有必要的;不同环... 查看详情

springcloud-第六篇hystrix参数配置

1:概述Hystrix使用Archaius作为配置属性的默认实现。官方配置文档:https://github.com/Netflix/Hystrix/wiki/Configuration每个属性有四个优先级,依次增大:1:代码的全局默认值2:动态全局默认属性可以使用全局属性文件来更改全局默认值... 查看详情

第六篇:配置docker容器加速器

背景说明鉴于国内网络稳定问题,到国外站点拉取docker镜像十分缓慢,故需要配置国内镜像以便提高镜像下载速度。1.使用这个url地址https://account.daocloud.io/signin注册一个账号并登录2.点击右上角箭头标记的加速选项3.复制下面的... 查看详情

flask第六篇flask实例化参数以及信号

一、实例化补充instance_path和instance_relative_config是配合来用的、这两个参数是用来找配置文件的,当用app.config.from_pyfile(‘settings.py‘)这种方式导入配置文件的时候会用到fromflaskimportFlask,requestapp=Flask(__name__,instance_path=None,instance_rel 查看详情

第六篇:面向对象

第六篇:面向对象  PYTHON-面向对象类绑定方法PYTHON-面向对象继承派生PYTHON-面向对象-练习-王者荣耀对砍游戏  查看详情

coreanimation文档翻译(第六篇)

?高级动画技巧配置属性动画或者关键帧动画的方式是多种多样的。需要同时执行多个动画或者顺序执行多个动画的APP,可以通过高级的方式同步这些动画的timing或者将这些动画绑定在一起。我们也可以使用其他类型的动画对象... 查看详情

第六篇vggnet——模型精讲

文章目录 查看详情

c++师傅领进门,修行靠个人第六篇:内存管理(代码片段)

本文目录1内存分布图2C语言和C++内存分配实现2.1C语言实现2.2C++实现3C语言和C++内存管理区别4内存泄漏1内存分布图注意:1.向下生长:地址由高到低2.向上生长:地址由低到高3.栈又叫堆栈,非静... 查看详情

c++师傅领进门,修行靠个人第六篇:内存管理(代码片段)

本文目录1内存分布图2C语言和C++内存分配实现2.1C语言实现2.2C++实现3C语言和C++内存管理区别4内存泄漏1内存分布图注意:1.向下生长:地址由高到低2.向上生长:地址由低到高3.栈又叫堆栈,非静... 查看详情

第六篇深入理解securityconfigurer源码(代码片段)

深入理解SecurityConfigurer一、SecurityConfigurer  SecurityConfigurer在SpringSecurity中是一个非常重要的角色。在前面的内容中曾经多次提到过,SpringSecurity过滤器链中的每一个过滤器,都是通过xxxConfigurer来进行配置的,而这些x... 查看详情

第六篇深入理解securityconfigurer源码(代码片段)

深入理解SecurityConfigurer一、SecurityConfigurer  SecurityConfigurer在SpringSecurity中是一个非常重要的角色。在前面的内容中曾经多次提到过,SpringSecurity过滤器链中的每一个过滤器,都是通过xxxConfigurer来进行配置的,而这些x... 查看详情

第六篇抽屉效果+uitabbarcontroller

依赖于第三方的框架RESideMenu1.AppDelegate.m中的实现-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{//Overridepointforcustomizationafterapplicationlaunch.//在 查看详情

关于ajax第六篇

使用回调函数回调函数是一种以参数形式传递给另一个函数的函数。如果您的网站上存在多个AJAX任务,那么您应该为创建XMLHttpRequest对象编写一个标准的函数,并为每个AJAX任务调用该函数。该函数调用应该包含URL以及发生onreadys... 查看详情

接口测试(java+testng+ant+jenkins)第六篇testng二

1、testng中,多次执行用例  @Test(dataProvider="data-provider")  在@Test标签后面加上参数来源:dataProvider(data-provider)   data-provider中有多少组数据,@Test就会使用数据执行多少次2、dataProvider的配置和使用@DataProvider(name="data-p... 查看详情

第六篇avplayer地址视频播放控件

1.引用头文件#importAVFoundation2.自定义AVPlayer(播放的机器)3.自定义AVPlayerItem(胶片)>>视频的URL转成AVAsset4.AVPlayerLayer(白板)  查看详情

第六篇elasticsearchexpress删除索引数据

express框架删除elasticsearch索引数据1.在elasticsearch.js文件下添加functiondeleteDocument(id){returnelasticClient.delete({index:indexName,type:"foods",id:id});}exports.deleteDocument=deleteDocument;2.在路由删除数据代码块中添加el 查看详情