SpringCloud集成zookeeper实现服务注册并访问功能

2022-11-13 11:11:27 功能 注册 集成

SpringCloud整合zookeeper实现服务注册

前言:

zookeeper是一个分布式协调工具,可以实现注册中心功能。换而言之,zookeeper和eureka一样,是用于充当服务注册功能服务器的一个springcloud插件。而随着eureka停止更新,zookeeper也成为了市面大量使用的替代eureka的一门技术,为分布式项目实现注册中心的功能。

一.环境准备

1.linux环境下的zookeeper环境

如果还未配置zookeeper环境的同学可以移步我的另外一篇文章喔,那里对如何在Linux环境下安装zookeeper有着很详细的介绍~

linux上安装zookeeper 启动和关闭的教程

2.一个简单的springcloud父项目环境

在cloud2022项目中的pom.xml导入springcloud依赖和一些常规依赖

<?xml version="1.0" encoding="UTF-8"?>
 
<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.canrioyuan</groupId>
    <artifactId>cloud2022</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <!--父工程的打包方式必须为pom:表示父工程不写代码,只在父工程的pom.xml文件中使用配置版本,
        子工程的打包方式才是war:发布在服务器上的工程,如网站或者是服务,maven会自动帮助我们指明这个工程为WEB工程
                        jar:默认打程jar工程,相当于打包-->
 
    <modules>
        <module>cloud-provider-payment8001</module>
        <module>cloud-consumer-order80</module>
        <module>cloud-api-commons</module>
        <module>cloud-eureka-server7001</module>
        <module>cloud-eureka-server7002</module>
        <module>cloud-provider-payment8002</module>
        <module>cloud-provider-payment8004</module>
    </modules>
 
    <!-- 统一管理jar包版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.18.24</lombok.version>
        <Mysql.version>8.0.21</mysql.version>
        <druid.version>1.1.21</druid.version>
        <mybatis-plus.spring.boot.version>3.5.1</mybatis-plus.spring.boot.version>
    </properties>
 
    <!-- 子模块继承之后,提供作用:定版本+子module不用写groupId和version  -->
    <dependencyManagement>
        <!--只是声明依赖,并不实现引入-->
        <dependencies>
            <!--Spring Boot 2.2.2-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud Hoxton.SR1-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud alibaba 2.1.0.RELEASE-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <optional>true</optional>
            </dependency>
 
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

二.编写服务提供者模块

1.在cloud2022下创建一个cloud-provider-payment8004作为服务提供者

2.在pom.xml文件中添加常规依赖和zookeeper、springboot整合zookeeper客户端等依赖

        <!-- 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>
            <!--除去自带的zookeeper依赖-->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 
        <!--导入符合安装版本的zookeeper依赖-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

3.编写application.yaml

#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
  port: 8004
#服务别名----注册zookeeper到注册中心名称
spring:
  application:
    name: cloud-provider-payment
    #连接zookeeper
  cloud:
    zookeeper:
      connect-string: 192.168.154.133:2181  #此处为对应的zookeeper客户端地址

4.编写主启动类PaymentMain8004

package com.canrioyuan;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 
@SpringBootApplication
@EnableDiscoveryClient   //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class PaymentMain8004 {
 
    public  static void main(String[] args){
        SpringApplication.run(PaymentMain8004.class,args);
    }
}

5.编写PaymentController

package com.canrioyuan.controller;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.UUID;
 
@RestController
@Slf4j
public class PaymentController {
    
    //获取配置文件中端口对应的值
    @Value("${server.port}")
    private String serverPort;
 
    @RequestMapping(value = "/payment/zk")
    public String paymentzk()
    {
        return "springcloud with zookeeper: "+serverPort+"\t"+ UUID.randomUUID().toString();
    }
 
}

三.测试服务提供者模块是否搭建成功

1.运行zookeeper的客户端

进入zookeeper安装目录下的bin目录,启动zookeeper服务器

[root@zookeeper1 bin]# ./zkServer.sh start

启动zookeeper客户端

[root@zookeeper1 bin]# zkCli.sh

启动成功

可以看到,此时zookeeper中的节点只有locks和zookeeper:

2.运行PaymentMain8004

运行成功后如下图所示:

3.查看zookeeper中的节点

我们再一次查看zookeeper下的节点,可以看到services已经被注册且services下的节点即为我们application.yaml为该模块配置的名字

4.访问http://localhost:8004/payment/zk

出现如下字符串则证明搭建成功

四.编写服务消费者模块

1.在cloud2022下创建一个cloud-consumerzk-order80作为服务消费者

2.在pom.xml文件中添加常规依赖和zookeeper、springboot整合zookeeper客户端等依赖

 <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>
            <!--先排除自带的zookeeper-->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--添加zookeeper3.4.9版本-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

3.编写application.yaml

server:
  port: 80
 
spring:
  application:
    name: cloud-consumer-order
  cloud:
    #注册到zookeeper地址
    zookeeper:
      connect-string: 192.168.154.133:2181   #zookeeper环境所在的Ip地址

4.编写主启动类PaymentMain8004

package com.canrioyuan;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class OrderZk80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderZk80.class,args);
    }
}

5.编写配置类ApplicationContextConfig(用于注册RestTemplate)

package com.canrioyuan.controller;
 
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
 
@Configuration
public class ApplicationContextConfig {
    
    @Bean  //将RestTemplate注册到容器中
    
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

6.编写OrderController

package com.canrioyuan.controller;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
import javax.annotation.Resource;
 
@RestController
public class OrderZkController {
    //声明固定的服务访问前缀
    public static final String INVOKE_URL ="http://cloud-provider-payment";
    //注入容器中注册的RestTemplate
    @Resource
    private RestTemplate restTemplate;
 
    @GetMapping(value="/consumer/payment/zk")
    public String PaymentInfo(){
        String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk", String.class);
        System.out.println("消费者调用支付服务_result:"+result);
        return result;
    }
}

五.测试服务消费者模块是否搭建成功

1.启动该服务消费者模块

2.访问http://localhost/consumer/payment/zk

结果如下则搭建成功

至此,我们SpringCloud集成zookeeper实现服务注册的教程就结束啦~

到此这篇关于SpringCloud集成zookeeper实现服务注册并访问功能的文章就介绍到这了,更多相关SpringCloud集成zookeeper内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章