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); } }
啟動不報錯,訪問后的結果