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>
#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+"\t"+ UUID.randomUUID().toString(); } }
启动类
@SpringBootApplication @EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务 public class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); } }
启动不报错,访问后的结果