zoukankan      html  css  js  c++  java
  • springcloud记录篇6-分布式配置中心

    一。分布式配置中心简介

     Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment和PropertySource抽象的映射,所以它除了适用于Spring构建的应用程序,也可以在任何其他语言运行的应用程序中使用。作为一个应用可以通过部署管道来进行测试或者投入生产,我们可以分别为这些环境创建配置,并且在需要迁移环境的时候获取对应环境的配置来运行

    如果修改 客户端1的环境为producet 获取的就是clientA-product.properties配置 配置随着环境的改变而改变

    二。springcloud config server

    使用springcloud的配置中心 和章节1的图示结果一致 参考http://cloud.spring.io/spring-cloud-static/Dalston.SR5/single/spring-cloud.html#_spring_cloud_config_server
    首先创建configserver的maven项目 添加依赖

    <parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.9.RELEASE</version>
    	</parent>
    	<properties>
    		<CLOUD-VERSION>Dalston.SR5</CLOUD-VERSION>
    	</properties>
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>${CLOUD-VERSION}</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
           <dependencies>
      	<dependency>
      		<groupId>org.springframework.cloud</groupId>
      		<artifactId>spring-cloud-config-server</artifactId>
      	</dependency>
      </dependencies>
      src/main/resources目录下 添加配置文件application.yml
    server: 
      port: 8888
    spring:
      cloud:
        config:
          server:
            git:
              uri: file:///E:/workspace3/wk/wk_config 
      application: 
          name: CONFIGSERVER
    E:/workspace3/wk/wk_config目录可以使用eclipse创建一个项目 然后 右键 team share  git 创建一个本地git 或者
    去github上注册账号创建一个github的项目 uri上粘贴github的地址  
    wk_config目录下 添加一个 文件 configclient-dev.properties 添加一个键值对  name=zs 用于测试
    添加运行类
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigMain {
    
    	public static void main(String[] args) {
    		 SpringApplication.run(ConfigMain.class, args);
    	}
    
    }
    至此配置中心服务器配置完成 接下来配置客户端
    添加一个maven项目 configclient
    添加maven依赖
    <parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.9.RELEASE</version>
    	</parent>
    	<properties>
    		<CLOUD-VERSION>Dalston.SR5</CLOUD-VERSION>
    	</properties>
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>${CLOUD-VERSION}</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
           <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>
      </dependencies>
     添加bootstrap.yml
    spring:
      cloud:
        config:
         uri: https://localhost:8888
     添加application.yml
     
    spring: 
      application: 
        name: configclient
      profiles: 
        active: dev
     添加main测试类
    @SpringBootApplication
    public class ConfigMain {
    
    	public static void main(String[] args) {
    		 SpringApplication.run(ConfigMain.class, args);
    	}
    
    }
    添加控制类
    package cn.et.manager.test;
    
    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.RestController;
    
    @RestController
    @RefreshScope  //用于当配置修改后自动刷新
    public class TestController {
    	@Value("${name}")
    	String name;
    	@GetMapping("/test")
    	public String test(){
    		return name;
    	}
    }
    
      访问 http://localhost:8080/test 发现输出了之前配置中心的zs 将 name=zs 改成ls 发现不会自动刷新
      访问 http://localhost:8080/refresh后 发现自动刷新了 此时方式比较麻烦 后面zookeeper配置中心自动监控刷新
      配置中心可以使用注册和发现来实现多配置中心高可用 具体参考http://cloud.spring.io/spring-cloud-static/Dalston.SR5/single/spring-cloud.html#discovery-first-bootstrap

    三 使用zookeeper作为配置中心
     使用zookeeper作为配置中心可以利用zookeeper本身的高可用和数据的高一致性 zookeeper作为配置中心能自动监控属性值的变化后  自动更新 以下参考http://cloud.spring.io/spring-cloud-static/Dalston.SR5/single/spring-cloud.html#spring-cloud-zookeeper-config
     zookeeper作为配置中心后 不在需要配置中心的项目  eclipse安装zookeeper插件 添加更新地址 选择第三项安装
      http://www.massedynamic.org/eclipse/updates/ 

    可以看到zookeeper explorer视图 打开 new zookeeper connection  (本机下载一个zookeeper服务器点击 zk-server.bat启动)输入以下

    在zookeeper邮件 new node 添加一些 节点 配置中心的节点新建规则 
    /根节点名称/应用名称-profile/键=值
    比如 我的根节点名称 使用的名字是wkconf 我的项目名称是wkmanager wkmanager新建一个值 是name=zs

    接下来 就新建wk_manger的项目 添加依赖

    <parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.9.RELEASE</version>
    	</parent>
    	<properties>
    		<CLOUD-VERSION>Dalston.SR5</CLOUD-VERSION>
    	</properties>
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>${CLOUD-VERSION}</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
           <dependencies>
      	<dependency>
      		<groupId>org.springframework.cloud</groupId>
      		<artifactId>spring-cloud-starter-zookeeper-config</artifactId>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework.cloud</groupId>
      		<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
      	</dependency>
      	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
      </dependencies>
    添加 bootstrap.yml

    spring:
      cloud:
        zookeeper:
          connect-string: localhost:2181
          config:
            enabled: true  
            root: wkconf
            defaultContext: wkmanager
            profileSeparator: '-'
            watcher: 
              enabled: true
            
    添加application.yml
    spring: 
      application: 
        name: wkmanager
      profiles: 
        active: dev
    
    添加main类
    package cn.et.manager;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    /**
     * manager的主入口
     * @author jiaozi
     *
     */
    @SpringBootApplication
    @EnableDiscoveryClient 
    public class ManagerMain {
    
    	public static void main(String[] args) {
    		 SpringApplication.run(ManagerMain.class, args);
    	}
    
    }
    
    添加controller测试获取配置
    package cn.et.manager.test;
    
    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.RestController;
    
    @RestController
    @RefreshScope
    public class TestController {
    	@Value("${name}")
    	String name;
    	@GetMapping("/test")
    	public String test(){
    		return name;
    	}
    }
    
    访问 http://localhost:8080/test 发现输出了之前配置中心的zs 将 name=zs 改成ls 发现自动刷新










  • 相关阅读:
    截屏 多难未遂
    捕捉异常
    Android中缓存记忆
    Android中的线程池
    悄悄为Android中解决部分适配问题哦!
    java中的服务端跳转与客户端跳转区别与联系
    doget(),doput()方法的使用
    基本概念--同步,异步
    java--SimpleDataFormat类
    java--9
  • 原文地址:https://www.cnblogs.com/liaomin416100569/p/9331148.html
Copyright © 2011-2022 走看看