zoukankan      html  css  js  c++  java
  • linux服务器安装zookeeper本地项目远程连接

    zookeeper linux 服务器安装,本地idea连接

    先决条件:一台linux服务器,服务器里面已经安装好java环境(自行百度)

     

    mkdir zookeeper #创建文件夹
    ​
    cd zookeeper    #进入文件夹
    ​
    wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.5.8/apache-zookeeper-3.5.8-bin.tar.gz    #下载zookeeper,这个是编译后的二进制包
    ​
    tar -zxvf apache-zookeeper-3.5.8-bin.tar.gz     #解压安装包
    ​
    cd apache-zookeper-3.5.8-bin        #进入解压后的文件夹
    cd conf     #进入配置文件夹
    cp zoo_sample.cfg zoo.cfg       #复制配置文件zoo_sample.cfg并命名为zoo.cfg
    ​
    vi zoo.cfg      #编辑配置文件 s编辑,esc + :wq保存退出
    ​
    vi /etc/profile     #配置系统环境变量
    #加上下面内容 路径根据实际情况修改
    export ZOOKEEPER_HOME=/usr/zookeeper/apache-zookeeper-3.5.7-bin/
    PATH=$ZOOKEEPER_HOME/bin:$PATH
    ​
    source /etc/profile     #让刚刚添加的配置生效
    ​
    #切换到bin目录
    cd ..
    cd bin
    ​
    ./zkServer.sh start     #启动服务
    ./zkServer.sh status    #查看状态
    ./zkServer.sh stop      #关闭服务
    tail 日志文件名称         #查看日志(存放在logs目录)
    ./zkCli.sh              #打开客户端

    zookeeper服务就搭建好了

     

    在阿里云控制台把2181端口放开,配置安全组规则

     

    准备就绪,实现代码

    新建maven项目,引入依赖

    <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>
            <!--先排除自带的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.5.8</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>

     

    建application.yml文件

    #8004表示注册到zookeeper服务器的服务提供者端口号
    server:
      port: 8004
    ​
    ​
    #服务别名----注册zookeeper到注册中心名称
    spring:
      application:
        name: cloud-zookeeper-demo
      cloud:
        zookeeper:
          connect-string: 阿里云公网IP:2181

     

    controller层

    @RestController
    public class PaymentController {
    ​
        @Value("${server.port}")
        private String serverPort;
    ​
        @RequestMapping(value = "/payment/zk")
        public String paymentZk(){
            return "springcloud with zookeeper: "+serverPort+"	"+ UUID.randomUUID().toString();
        }
    }

     

    启动类

    @SpringBootApplication
    @EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
    public class PaymentMain8004 {
    ​
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8004.class,args);
        }
    }

     

    启动不报错,访问后的结果

     

  • 相关阅读:
    (转)性能测试---并发用户理解
    (转)基于DDD的现代ASP.NET开发框架--ABP分层架构
    (转)Web自动化测试中的接口测试
    (转) 一致性Hash算法在Memcached中的应用
    Memcached工作原理及常见问题
    Memcached介绍及相关知识
    .net 面试题总结
    使用IDEA工具配置和运行vue项目(详细其中的坑)
    关于伪分布zookeeper集群启动出错(Error contacting service. It is probably not running.)
    常用查找和排序
  • 原文地址:https://www.cnblogs.com/liweixml/p/13885687.html
Copyright © 2011-2022 走看看