Spring Boot2.0之 整合Zookeeper集群

Spring Boot2.0之 整合Zookeeper集群

普通的连接:

pom:


  4.0.0
  com.toov5.zookeeper
  zookeeper
  0.0.1-SNAPSHOT
	
		
			com.101tec
			zkclient
			0.10
			
				
					slf4j-api
					org.slf4j
				
				
					log4j
					log4j
				
				
					slf4j-log4j12
					org.slf4j
				
			
		
	

 代码: 

package com.toov5.controller;

import org.I0Itec.zkclient.ZkClient;

public class zkTest {

      public static void main(String[] args) {
        String connection = "192.168.91.1:2181,192.168.91.3:2181,192.168.91.4:2181";
        ZkClient zkClient = new ZkClient(connection);
        zkClient.createPersistent("/toov5_01");
        zkClient.close();
    }
    
    
}

 

运行查看结果:

Spring Boot2.0之 整合Zookeeper集群_第1张图片

 

SpringBoot整合:

配置文件抽取到 yml中

connection:192.168.91.1:2181,192.168.91.3:2181,192.168.91.4:2181
   

 整合:

package com.toov5.service;

import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class zkService {
   @Value("{connection}")
   private String connection;
    private ZkClient zkClient = new ZkClient(connection);
   
   public String creteNode(String path){   
       try {
           zkClient.createPersistent(path); //没返回结果 所以用try catch 方式解决哦
           return "ok";
    } catch (Exception e) {
           return "fail";
    }  
   }
     
    
}

pom.xml


  4.0.0
  com.toov5.zookeeper
  zookeeper
  0.0.1-SNAPSHOT
  
		org.springframework.boot
		spring-boot-starter-parent
		2.0.0.RELEASE
	
	
		
			com.101tec
			zkclient
			0.10
			
				
					slf4j-api
					org.slf4j
				
				
					log4j
					log4j
				
				
					slf4j-log4j12
					org.slf4j
				
			
		
		
			org.springframework.boot
			spring-boot-starter-web
		
	

  

你可能感兴趣的:(SpringBoot,Zookeeper)