栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

zookeeper服务注册

zookeeper服务注册

避坑
  1. 关闭防火墙
  2. 修改tocat端口解决8080端口占用
  3. 解决zookeeper maven冲突

    org.springframework.cloud
    spring-cloud-starter-zookeeper-discovery
    
    
        
            org.apache.zookeeper
            zookeeper
        
    



    org.apache.zookeeper
    zookeeper
    安装版本号

相关命令 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

        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.jia.learn
            cloud-api-commons
            1.0-SNAPSHOT
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
            
            
                
                    org.apache.zookeeper
                    zookeeper
                
            
        
        
        
            org.apache.zookeeper
            zookeeper
            3.6.3
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
配置文件
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;


@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
 
        
            com.jia.learn
            cloud-api-commons
            ${project.version}
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
            
            
                
                    org.apache.zookeeper
                    zookeeper
                
            
        
        
        
            org.apache.zookeeper
            zookeeper
            3.6.3
            
                
                    slf4j-log4j12
                    org.slf4j
                
            
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
配置文件
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;


@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]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/654403.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号