SpringCloud:注册中心——Zookeeper

一,SpringCloud注册中心

    1,SpringCloud支持三种方式的注册中心:Eureka,Zookeeper,Consul;本篇对Zookeeper中心进行分析

    2,SpringCloud集成Zookeeper实现注册中心,依然保持Zookeeper最基本的服务注册和服务发现方式,获取服务路径后最终实现服务调用。如下图,通过Zookeeper实现注册中心同样分为三个部分。

二,代码实现

    1,项目结构

        * 通过两台服务端演示负载均衡

    2,Zookeeper服务器

        * 此处采用之前搭建好的Zookeeper服务集群,在Linux系统上通过三台虚拟机实现,参考博文:                              https://blog.csdn.net/u011976388/article/details/81259886

    3,服务生产者

        a,Maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
  </parent>
  <!-- 管理依赖 -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.M7</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <!-- SpringBoot整合Web组件 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- SpringBoot整合zookeeper客户端 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>
  </dependencies>
  <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
  <repositories>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/libs-milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

        b,Server_1配置信息

###订单服务的端口号
server:
  port: 8002
###服务别名----服务注册到注册中心名称
spring:
  application:
    name: zk-member
  cloud:
    zookeeper:
      ### Zookeeper注册中心集群
      connect-string: 192.168.91.128:2181,192.168.91.129:2181,192.168.91.130:2181

        c,Server_2配置信息

###订单服务的端口号
server:
  port: 8001
###服务别名----服务注册到注册中心名称
spring:
  application:
    name: zk-member
  cloud:
    zookeeper:
      ### Zookeeper注册中心集群
      connect-string: 192.168.91.128:2181,192.168.91.129:2181,192.168.91.130:2181

        d,服务接口

package com.gupao.zookeeper.controller;

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

/**
 * @author pj_zhang
 * @create 2019-01-15 22:08
 **/
@RestController
public class MemberController {

    @Value("${server.port}")
    private Integer port;

    @RequestMapping("/getMember")
    public String getMember() {
        return "MemberController.getMember, port : " + port;
    }

}

        e, 启动Main函数

package com.gupao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author pj_zhang
 * @create 2019-01-15 22:16
 **/
@SpringBootApplication
// 通过Zookeeper注册中心注册/发现服务
@EnableDiscoveryClient
public class ZookeeperServerApp {

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

}

        f,启动效果,在Zookeeper节点上进行查看。发现生成了zk-member的父节点,并再下面生成了两个服务节点,其中之一具体节点信息如下JSON展示。表名服务端两台服务器已经全部注册到Zookeeper注册中心

{
	"name": "zk-member",
	"id": "ceb7f446-1214-42bc-a742-576feb108f39",
	"address": "USER-20180510PQ",
	"port": 8002,
	"sslPort": null,
	"payload": {
		"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
		"id": "application-1",
		"name": "zk-member",
		"metadata": {}
	},
	"registrationTimeUTC": 1547561383883,
	"serviceType": "DYNAMIC",
	"uriSpec": {
		"parts": [{
			"value": "scheme",
			"variable": true
		}, {
			"value": "://",
			"variable": false
		}, {
			"value": "address",
			"variable": true
		}, {
			"value": ":",
			"variable": false
		}, {
			"value": "port",
			"variable": true
		}]
	}
}

    4,服务消费者

        a,Maven依赖

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
  </parent>
  <!-- 管理依赖 -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.M7</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <!-- SpringBoot整合Web组件 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- SpringBoot整合zookeeper客户端 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>
    <!-- 声明式Feign调用依赖引入 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
  </dependencies>
  <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
  <repositories>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/libs-milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

       b,配置信息

###订单服务的端口号
server:
  port: 8003
###服务别名----服务注册到注册中心名称
spring:
  application:
    name: zk-order
  cloud:
    zookeeper:
      ### Zookeeper注册中心集群
      connect-string: 192.168.91.128:2181,192.168.91.129:2181,192.168.91.130:2181

        c,RestTemplate方式调用

             * 调用接口

package com.gupao.zookeeper.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author pj_zhang
 * @create 2019-01-15 22:01
 **/
@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/orderToMember")
    public String orderToMember() {
        return restTemplate.getForObject("http://zk-member/getMember", String.class);
    }

}

            * 启动Main函数

package com.gupao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author pj_zhang
 * @create 2019-01-15 21:56
 **/
@SpringBootApplication
// Zookeeper注册中心进行服务注册和发现
@EnableDiscoveryClient
public class ZookeeperClientApp {

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

    // 服务端通过application_name进行服务发现, 默认为多服务处理
    // 此处通过@LoadBalanced实现负载均衡
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

             * 效果演示

       d,声明式Feign方式调用

            * 项目结构

            * Feign接口

package com.gupao.zookeeper.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author pj_zhang
 * @create 2019-01-15 22:23
 **/
// 服务名称, 通过服务名称到注册中心发现服务
@FeignClient(name = "zk-member/")
public interface IMemberFeign {

    // 服务接口, 通过上一步发现的服务及所调用服务的注解路径, 拼接完整服务路径, 进行服务调用
    // 该接口注解路径必须与服务提供的注解路径一致
    @RequestMapping("/getMember")
    String getMember();

}

            * 调用接口

package com.gupao.zookeeper.controller;

import com.gupao.zookeeper.feign.IMemberFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author pj_zhang
 * @create 2019-01-15 22:01
 **/
@RestController
public class OrderController {

    // 注入Feign接口
    @Autowired
    private IMemberFeign memberFeign;

    @RequestMapping("/feignToMember")
    public String feignToMember() {
        // 直接通过Feign方法调用实现服务调用
        return memberFeign.getMember();
    }

}

            * 启动Main函数

package com.gupao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author pj_zhang
 * @create 2019-01-15 21:56
 **/
@SpringBootApplication
// 通过Zookeeper注册中心进行服务注册和发现
@EnableDiscoveryClient
// 通过声明式Feign方式调用服务
@EnableFeignClients
public class ZookeeperClientApp {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

         * 启动效果

猜你喜欢

转载自blog.csdn.net/u011976388/article/details/86500373