zookeeper服务注册

本文介绍了如何解决Spring Cloud应用中Zookeeper的端口冲突问题,包括修改Tomcat端口、排除自带Zookeeper依赖并添加指定版本。同时展示了Zookeeper的启动与管理命令,以及在生产环境中实现服务注册与消费的配置和代码示例。
摘要由CSDN通过智能技术生成

避坑

  1. 关闭防火墙
  2. 修改tocat端口解决8080端口占用
  3. 解决zookeeper maven冲突
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    <!--先排除自带的zookeeper3.5.3-->
    <exclusions>
        <exclusion>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!--添加zookeeper版本-->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>安装版本号</version>
</dependency>

相关命令

zookeeper

  • 启动: sh bin/zkServer.sh start
  • 查看服务状态: sh bin/zkServer.sh status
  • 停止: sh bin/zkServer.sh stop
  • 重启: sh bin/zkServer.sh restart
  • get /services/application/2bf4ff71-16ad-4965-b7ba-1e095c29dd7f

tomcat占用8080端口 换点端口号

sudo find / -name tomcat
找到tomcat/conf/server.xml 修改端口号

防火墙

  • 状态
    systemctl status firewalld
  • 开启
    systemctl start firewalld
  • 停止
    pkill -f firewall //杀死进程
    systemctl stop firewalld
  • 开机启动
    systemctl enable firewalld
  • 禁用开机启动
    systemctl disable firewalld

简单实现

开启zookeeper

生产者服务注册

maven

<dependencies>
        <!-- SpringBoot整合Web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
            <groupId>com.jia.learn</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- SpringBoot整合zookeeper客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!--先排除自带的zookeeper3.5.3-->
            <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.6.3</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>
    </dependencies>

配置文件

server:
  port: 8004

spring:
  application:
    name: cloud-payment-service
  cloud:
    zookeeper:
#      zookeeper ip+port
      connect-string: 114.55.173.209:2181

启动类注解

consul/zookeeper作为服务中心注入服务
@EnableDiscoveryClient

Controller

package com.jia.learn.controller;

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

import java.util.UUID;

/** 
 * @Description:  
 * @Author: LIANG
 * @Data: 2021/12/10 09:36:24
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {

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

    @GetMapping("/zk")
    public String paymentzk(){
        return "zookeeper port:"+port+ UUID.randomUUID().toString();
    }

}

消费者服务注册

maven

 <dependencies>
        <dependency>
            <groupId>com.jia.learn</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- SpringBoot整合zookeeper客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!--先排除自带的zookeeper3.5.3-->
            <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.6.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </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>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </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>
    </dependencies>

配置文件

server:
  port: 8000

spring:
  application:
    name: cloud-payment-consumerzk
  cloud:
    zookeeper:
      #      zookeeper ip+port
      connect-string: 114.55.173.209:2181

启动类注解

@EnableDiscoveryClient

配置类配置RestTemplate负载均衡

@LoadBalanced

Controller

package com.jia.learn.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @program: SpringCloudLearn
 * @description
 * @author: LIANG
 * @create: 2021-12-08 22:27
 **/
@RestController
@RequestMapping("/consumer")
public class OrderZKController {
    public static final String INVOKE_URL="http://CLOUD-PAYMENT-SERVER";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value="/payment/zk")
    public String paymentInfo(){
        String result = restTemplate.getForObject(INVOKE_URL + "/paymennt/zk", String.class);
        return result;
    }

}

查看服务

[root@iZbp1im64zbi66krifox9fZ bin]# ./zkCli.sh
Connecting to localhost:2181
*********************
[zk: localhost:2181(CONNECTED) 1] ls /
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /services
[cloud-payment-consumerzk, cloud-payment-service]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值