不惑之年的硬件牛人转到软件自学之spring cloud:(十二)集群配置中心SVN/GIT与其他框架Euraka/Zuul/Spring cloud bus的整合

前言:笔者曾经有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月5日      研究主题:集群配置中心SVN/GIT与其他框架Euraka/Zuul/RabbitMQ的整合

      前面我学习了很多组件,包括:Euraka/Zuul/RabbitMQ等,这么多组件,怎么和集群配置中心结合起来呢?下面就慢慢学来。

      一、整体框图如下:

     

      二、拷贝以前章节相关的项目到这个文件夹下面

            

       三、更改“cjb-config-server”

       1、在“pom.xml”中增加euerka的依赖

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

     2、在“application.yml”中增加注册中心eureka的网址

eureka: 
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/

    四、更改“cjb-configok-client”

    1、由于cjb-configok-client会接收RabbiMQ传过来的消息,因此必须增加amqp的依赖,增加“pom.xml”内容:

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

     2、更改“bootstrap.yml”内容:

spring:
  application:
    name: config-client
  cloud:
    config:
      discovery:
        enabled: true
        service-id: cjb-config-server
      profile: dev
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
management:
  security:
    enabled: false
     以上内容除了增加注册到eureka的服务中心外,由于完全有可能部署多个配置服务实例,因此开启客户端的配置发现功能,让客户端去发现、使用配置服务器的服务。因此使用discovery并用service-id的方式发现这个cjb-config-server的服务。

   3、我们将类“PersonClient”里面的内容删掉,防止在“Cjbcontroller”中提示错误

@FeignClient(name = "cjb-zuul-server",fallback = PersonClientFallback.class)

   4、在类“Cjbcontroller”中更改“/cjbconfig”的值,增加读取值cjbname即可

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

   5、运行主类“CjbClient”并在端口8080启动,注册到euerka注册中心后,输入网址:http://localhost:8080/cjbconfig


同时在打印信息中出现了“读取的值:chenjingbo”


五、更改“cjb-zuul-server”

1、在“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-zuul-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-zuul</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
         <version>1.5.4.RELEASE</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-bus-amqp</artifactId>
      </dependency>
   </dependencies>

</project>

2、增加主类“CjbApplication”的注解:

@EnableZuulProxy
@SpringBootApplication
@RestController
3、修改“bootstrap.yml”中的配置文件“cjb-zuul-dev”
spring:
  application:
    name: cjb-zuul
  cloud:
    config:
      discovery:
        enabled: true
        service-id: cjb-config-server
      profile: dev
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
management:
  security:
    enabled: false

4、同步需要在“VisualSVN Server”中新建一个文件:cjb-zuul-dev.yml


以上内容需要注意:第三排的:cjbzuul和第四排的:path: /cjbzuul里面的内容需要一致,否则访问不了

5、上传到相应文件夹下面


6、“OK”,一切准备就绪,跑应用,并在端口8100中,输入网址:http://localhost:8100/cjbzuul,就会自动跳转到网易网址。


六、刷新配置更改“cjb-rabbitmq-server”

目的:SVN仓库上面的配置更新后,如果要刷新配置,前面需要调用服务的/refresh端点。如果集群中存在多个要刷新的节点,逐个去刷新配置将会产生巨大的工作量。要解决该问题,可以用BUS消息的方式实现全部配置的刷新。上面的框图需要增加一个:外部程序访问:/bus/refresh的路径


1、在“pom.xml”中增加依赖,如果要用rabbitmq的代理服务器的话必须增加如下依赖:

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

2、删除不相关的类,只保留主类“CjbApplication”即可,其他都不用改

package com.example.demo;


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

import java.util.Scanner;


@SpringBootApplication
@EnableEurekaClient


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


}

3、跑主类在端口8000中,在eureka注册中心看一下跑起来没有:


4、由上图中看到,整个rabbiMQ的通道除了“cjb-rabbitmq-server”用外,其他两个:“cjb-configok-server”和“cjb-zuul-server”也要用到rabbitMQ通道,因此都需要在这两个项目的“pom.xml”中增加:

    “cjb-configok-server”中的“pom.xml”中增加:

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

     “cjb-zuul-server”中的“pom.xml”中增加:

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

    以上部分在之前的更改时就已经改好了,这里只是特别说明一下。

   怎么使用这个配置刷新呢?我花了一些时间研究如下(重新梳理一下以上内容):

   5、启动“cjb-rabbitmq-server”的服务,并运行在端口:8000

   6、上图中的“外部程序访问:/bus/refresh”我们在“cjb-configok-client”中新建一个访问类叫“Testrabbitmq”,主要用于访问:http://localhost:8000/bus/resfresh这个是访问“cjb-rabbitmq-server”的服务端口找到该程序的/bus/refresh的配置刷新路径

package com.example.demo;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Testrabbitmq {
    public static void main(String[] args) throws Exception{
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://localhost:8000/bus/refresh");
        HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
        System.out.println(EntityUtils.toString(httpResponse.getEntity()));
    }
}

在“Cjbcontroller”类中确认一下访问路径:/cjbconfig主要访问的文件是“bootstrap.yml”中的“config-client-dev”文件:

name: config-client
cloud:
  config:
    discovery:
      enabled: true
      service-id: cjb-config-server
    profile: dev

而“config-client-dev”文件的内容是,也就是需要显示name=“chenjingbo”:


我们运行“cjb-configok-client”这个类,并在网页中输入:http://localhost:8080/cjbconfig,可以访问到:chenjingbo


7、同理,我们也要访问到“cjb-zuul-server”里面的路径:/cjbzl,我们在该项目里面“cjbcontroller”增加如下内容:

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

并在“bootstrap.yml”中确认文件“cjb-zuul-dev”的 name:

spring:
  application:
    name: cjb-zuul
  cloud:
    config:
      discovery:
        enabled: true
        service-id: cjb-config-server
      profile: dev

打开“cjb-zuul-dev.yml”文件,name=chenjingbo730:


运行这个“cjb-zuul-server”在端口8100上面,输入网址:http://localhost:8100/cjbzl


8、以上思路已经理顺了,但不知道读者能理顺不?反正要仔细慢慢实现。以下就做更改了哈

首先,更改“config-client-dev”文件的内容是,也就是需要显示name=“chenjingbo-gai”:


其次,更改“cjb-zuul-dev.yml”文件,name=chenjingbo730-gai:


第三,不要忘了重新对修改的文件进行“SVN Commit....”


第四,就是重点了,需要在次运行一个“cjb-configok-client”下面的“Testrabbitmq”类


记住,以上顺序不能打乱,必须是先提交再运行“Testrabbitmq”类,否则会出问题

第五,分别刷新网页

http://localhost:8080/cjbconfig


http://localhost:8100/cjbzl


以上已经“OK”,达到了自动更新配置的目的,但以上步骤必须记住,不能乱!!!

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


                        

猜你喜欢

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