編寫可執行的jar

這裏寫了一個批量刪除zookeeper節點的工具jar,關於如何生成可執行jar請參照我的另外一篇博客:Maven生成可以直接運行的jar包的多種方式

主要是在main函數中對傳入的參數進行處理:

package com.zk;

import com.zk.util.ZookeeperCuratorUtils;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.CollectionUtils;

import java.util.List;


/**
 * 程序主入口
 * Created by sunnyLu on 2017/5/27.
 */
public class Main {
    public static void main(String[] args) {

        try {
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:/*.xml");
            String connectString = args[0];//zk鏈接地址
            String type = args[1];//操作類型
            String rmrNode = args[2];//用於模糊匹配的節點內容
            CuratorFramework client = ZookeeperCuratorUtils.clientOne(connectString);
            if ("brmr".equals(type)){//批量刪除
                List<String> childNodeList = ZookeeperCuratorUtils.nodesList(client,"/dubbo");
                if (CollectionUtils.isEmpty(childNodeList))
                    print("沒有子節點");
                for (String node : childNodeList){
                    if (node.contains(rmrNode)){
                        ZookeeperCuratorUtils.deleteDataNode(client,"/dubbo"+"/"+node);
                        print(node);
                    }
                }
                print("移除完畢");
            }
        } catch (Exception e) {
            e.printStackTrace();
            printHelp();
        }
    }


    private static void printHelp() {
        print("===============================");
        print("應用配置導入程序");
        print("Author:sunnyLu");
        print("Email:******@qq.com");
        print("Tel:187****0464");
        print("參數有異常,請詳見REDEME.txt");
        print("祝使用愉快!!");
    }

    private static void print(String str) {
        System.out.println(str);
    }
}
package com.zk.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.ACLProvider;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooDefs.Perms;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;

/**
 * zk工具類
 * Created by sunnyLu on 2017/7/12.
 */
public class ZookeeperCuratorUtils {


    public static CuratorFramework clientOne(String connectString){
        // 連接時間 和重試次數
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        //CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
        CuratorFramework client = CuratorFrameworkFactory.builder().connectString(connectString)
                .sessionTimeoutMs(5000).retryPolicy(retryPolicy).build();
        client.start();
        return client;
    }


    public static CuratorFramework clientTwo(String connectString){

        //默認創建的根節點是沒有做權限控制的--需要自己手動加權限???----
        ACLProvider aclProvider = new ACLProvider() {
            private List<ACL> acl ;
            @Override
            public List<ACL> getDefaultAcl() {
                if(acl ==null){
                    ArrayList<ACL> acl = ZooDefs.Ids.CREATOR_ALL_ACL;
                    acl.clear();
                    acl.add(new ACL(Perms.ALL, new Id("auth", "admin:admin") ));
                    this.acl = acl;
                }
                return acl;
            }
            @Override
            public List<ACL> getAclForPath(String path) {
                return acl;
            }
        };
        String scheme = "digest";
        byte[] auth = "admin:admin".getBytes();
        int connectionTimeoutMs = 5000;
        String namespace = "testnamespace";
        CuratorFramework client = CuratorFrameworkFactory.builder().aclProvider(aclProvider).
                authorization(scheme, auth).
                connectionTimeoutMs(connectionTimeoutMs).
                connectString(connectString).
                namespace(namespace).
                retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000)).build();
        client.start();
        return client;
    }

    
    public static List<String> nodesList(CuratorFramework client,String parentPath) throws Exception{
        List<String> paths = client.getChildren().forPath(parentPath);
        return paths;
    }

    public static void deleteDataNode(CuratorFramework client,String path) throws Exception{
        Stat stat = client.checkExists().forPath(path);
        if (stat == null)
            return;
        client.delete().deletingChildrenIfNeeded().forPath(path);
    }

}
使用mvn clean package打包會在項目目錄的target目錄生成jar包及其所以依賴的jar

運行這個jar文件


查看zk節點信息會發現這個時候相關的zk節點已經刪除

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章