不惑之年的硬件牛人转到软件自学之spring cloud:(十一)集群配置中心SVN/GIT

      前言:笔者曾经有18年的硬件研发经验,从(1)51单片机到(2)FPGA到(3)嵌入式ARM(ARM9到CORTEX A9)全都研发设计过,产品从(1)B超的整机研发到(2)智能家居系统到(3)无线电监测机到(4)平板电脑研发到(5)路灯智能控制到(5)工业电脑均有涉及,从(1)普通的电子技术工程师到(2)副总工程师到(3)副总经理到(4)事业部总经理。。。目前已经步入不惑之年的我对于物联网技术的热衷,决定从硬件开始全面转到物联技术框架之一的spring cloud技术,把我的整个学习经历和大家一起分享,也期待在之后有更多机会和大家一起合作,探讨。

      今天是:2018年5月3日      研究主题:集群配置中心SVN/GIT

       一、使用集群配置中心服务的目的

      Spring Cloud Config为分布式系统提供了配置服务器和配置客户端,通过对它们的配置,可以很好地管理集群中的配置文件。在实际应用时,我们会将配置文件存放到外部系统(例如Git、SV等),Spring Cloud Config的服务器与客户端会到这些外部系统中读取、使用这些配置。

       配置服务器主要功能如下:

       1、提供访问配置的服务接口;

       2、对属性进行加密和解密;

       3、可以简单地嵌入Spring Boot的应用中。

       配置客户端主要功能如下:

       1、绑定配置服务器,使用远程的属性来初始化Spring容器;

       2、对属性进行加密和解密;

       3、属性改变时,可以对它们进行重新加载;

       4、提供了与配置相关的几个管理端点;

       5、在初始化引导程序的上下为难时,进行绑定配置服务器和属性解密等工作,当然,也可以实现其他工作。

       框架如下

                              

           好了,以上纯理论的文字就敲到这儿,内容是直接拷贝的,在此特别说明一下。

           二、构建简单的配置例子

           1、安装所需要的软件:VisualSVN-Server-3.6.4-x64和TortoiseSVN-1.9.7.27907-x64-svn-1.9.7

         2、安装SVN服务器VisualSVN-Server-3.6.4-x64后进入安装目录下面:C:\Program Files\VisualSVN Server\bin找到                    VisualSVN Server.msc文件,建立新用户:chenjb730

                    

                         

                 新建一个项目:cjb-config

                           

                           

                           

                          

                           

             3、将上节的项目“cjb-rabbitmq-server”改为“cjb-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.example.demo</groupId>
   <artifactId>cjb-config-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</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.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      <dependency>
         <groupId>org.tmatesoft.svnkit</groupId>
         <artifactId>svnkit</artifactId>
         <version>1.9.0</version>
      </dependency>
   </dependencies>

</project>

    第二,在主类“CjbApplication”中增加注解@EnableConfigServer表示开启配置服务器的功能

package com.example.demo;


import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.stream.annotation.EnableBinding;

import java.util.Scanner;


@SpringBootApplication
@EnableEurekaClient
@EnableBinding(CjbSendService.class)
@EnableConfigServer

public class CjbApplication {

   public static void main(String[] args) {
      // 读取控制台输入作为端口参数
      Scanner scan = new Scanner(System.in);
      String port = scan.nextLine();
      // 设置启动的服务器端口
      new SpringApplicationBuilder(CjbApplication.class).properties(
            "server.port=" + port).run(args);
   }


}

  第三,在“application.yml”中做如下修改:

spring:
  application:
    name: cjb-rabbitmq-server
  profiles:
    active: subversion
  cloud:
    config:
      server:
        svn:
          uri: https://localhost/svn/cjb-config
          username: chenjb730
          password: xxxxxx(此处省略
        default-label: default-config
management:
  security:
    enabled: false

修改说明:management.security.enabled=false关闭认证;

                为了能让配置服务器连上SVN,需要先进行使用名称为subversion的配置。

第四,在F盘新建一个文件夹叫“test”,进入该文件夹点击右键,选择“SVN Checkout”:

                

                

               点击“OK”,在该文件中新建一个文件夹叫“default-config”并在该文件夹下面新建一个文件“cjb-test.yml”文件,并编辑如下:

                   

                   

                   点击右键,选择“SVN Commit....”,记住:千万不能直接在cjb-config项目下直接建一个“cjb-test.yml”,必须                    建一个“default-config”的文件夹,并在该文件夹下面建这个“cjb-test.yml”文件,为什么建“default-                               config”是因为必须和工程项目里面的“applicaion.yml”对应:        

management:
  security:
    enabled: false
spring:
  profiles:
    active: subversion
  cloud:
    config:
      server:
        svn:
          uri: https://localhost/svn/cjb-config
          username: chenjb730
          password: xxxxx(此处略)
        default-label: default-config

                    

                    选择“check”->“patch”里面新增的“cjb-test.yml”文件,并点击“OK”

                      

                      打开“VisualSVN Server”软件,我们可以看到在工程“cjb-config”下面新增了“cjb-test.yml”文件

                           

                     “OK”以上添加yml的配置文件大工告成,以上内容是在任何网上资料都没法找到这么详细的内容,我希望能对                          像我这样转行过来或新学的人所有帮助。

     第五,运行主类,并配置端口为8088,访问浏览器:http://localhost:8088/cjb-test.yml

                     

 4、将上节的项目“cjb-rabbitmq-client”改为“cjb-configok-client”作为配置服务器

                   第一,更改“pom.xml”加入相关依赖:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

                   第二,在“Cjbcontroller.class”中增加路径

@RequestMapping(value="/cjbconfig", method = RequestMethod.GET)
public String cjbconfig() {
   System.out.println("读取的值:" + cjbenv.getProperty("test.user.name"));
   return "cjb config is ok!";
}

                 第三,删除“applicatiom.yml”,增加“bootstrap.yml”,内容如下:    

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8088
      profile: dev
management:
  security:
    enabled: false

               以上部分,我之前跑了很久没有跑起来,主要原因把上面的:

                   name: config-client和profile: dev必须和SVN新建的文件名一致

              

                  另外不要将“uri”写成了“url”,这个特别需要注意;

         第四,在“VisualSVN Server”中的“default-config”目录下新建一个文件名叫“config-client-dev.yml”内容如下:

             

        上传到SVN中:

               

        第五,跑cjb-configok-client的服务,并在网页中输入如下网址:http://localhost:8080/cjbconfig

             

         第六,在服务cjb-configok-client中的打印信息中可以看到name名字已经打印出:chenjingbo了,

             

  三、配置加密和解密

        1、为服务器安装JCE

             服务器的加密和解密都需要依赖JCE,将压缩包:jce-policy-8.zip解压到JAVA_HOME的所在路径下面,在环境变量中                找到“JAVA_HOME”,变量值为:D:\Program Files\Java\jdk1.8.0_144

                                    

               将压缩包解压到该路径下面的“D:\Program Files\Java\jdk1.8.0_144\jre\lib\security”,覆盖里面的

                                                  

                        

              2、改造“cjb-config-server”项目,首先“pom.xml”中增加“httpclient”的依赖        

<dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>4.5.2</version>
</dependency>

               3、在“application.yml”中增加加密的key

encrypt:
  key: cjbKey

            4、我们新建一个类“EncryptClient”,用于向/encrypt端点发送POST请求,请求为“chenjb730”字符串进行加密。

package com.example.demo;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class EncryptClient {
    public static void main(String[] args) throws Exception{
        //创建默认的HttpClient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //调用POST方法请求服务
        HttpPost httpPost = new HttpPost("http://localhost:8088/encrypt");
        HttpEntity httpEntity = new StringEntity("chenjb730", Consts.UTF_8);
        httpPost.setEntity(httpEntity);
        //获取响应
        HttpResponse httpResponse = httpClient.execute(httpPost);
        //根据响应解析出字符串
        System.out.println(EntityUtils.toString(httpResponse.getEntity()));
    }
}

         首先启动“CjbApplication”主类,并输入8088端口号启动,紧接着启动“EncryptClient”类,我们会看到打印出了    “chenjb730” 的密文:“de389616151eed39228b32393dcf9c0c45734fde3ce6dc10492a49aa26f12070”        

       5、我们紧接着进行该密文的解密,新建一个类“DecryptClient”用于对该加密KEY的解密,复制之前建立的类“EncryptClient”改动两个部分内容:

         第一个:

HttpPost httpPost = new HttpPost("http://localhost:8088/encrypt");

           改为:

HttpPost httpPost = new HttpPost("http://localhost:8088/decrypt");

           第二个:

HttpEntity httpEntity = new StringEntity("chenjb730", Consts.UTF_8);

         改为:将之前生成的密文覆盖上面的“chenjb730"

HttpEntity httpEntity = new StringEntity("de389616151eed39228b32393dcf9c0c45734fde3ce6dc10492a49aa26f12070", Consts.UTF_8);

        完整代码如下:

package com.example.demo;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DecryptClient {
    public static void main(String[] args) throws Exception{
        //创建默认的HttpClient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //调用POST方法请求服务
        HttpPost httpPost = new HttpPost("http://localhost:8088/decrypt");
        HttpEntity httpEntity = new StringEntity("de389616151eed39228b32393dcf9c0c45734fde3ce6dc10492a49aa26f12070", Consts.UTF_8);
        httpPost.setEntity(httpEntity);
        //获取响应
        HttpResponse httpResponse = httpClient.execute(httpPost);
        //根据响应解析出字符串
        System.out.println(EntityUtils.toString(httpResponse.getEntity()));
    }
}

       紧接着启动类“DecryptClient”,打印出解密的“chenjb730”


    四、我们在SVN配置中心里面对于诸如“passwd”密码的关键字内容,如何实现加密和解密

     1、我们修改之前的文件“cjb-test.yml”,记住对于yml文件必须有单引号出现,并标记{cipher}告诉服务器这个是加密文件,请帮忙解密

     

    更改后一定记住要提交一次给SVN,右键“SVN Commit....”

   

   提交成功后访问网页:http://localhost:8088/cjb-test.yml,表示成功解密为“chenjb730”


五、如果需要完整代码的朋友,可以加入作者QQ群:智物联的spring cloud,入群说明:spring cloud代码需求


                       


猜你喜欢

转载自blog.csdn.net/weixin_38638578/article/details/80179046