zookeeper分布式(一)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>train-rpc</artifactId>
    <version>0.0.1</version>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <zookeeper.version>3.4.9</zookeeper.version>
        <zkclient.version>0.2-SNAPSHOT</zkclient.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0</version>
        </dependency>

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-All</artifactId>
        </dependency>
        <!-- Msgpack序列化 -->
        <dependency>
            <groupId>org.msgpack</groupId>
            <artifactId>msgpack</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.10</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>2.4.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>2.4.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-test</artifactId>
                <version>2.4.2</version>
            </dependency>
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-All</artifactId>
                <version>4.1.16.Final</version>
                <!--<version>4.1.6.Final</version>-->
            </dependency>
            <!-- Msgpack序列化 -->
            <dependency>
                <groupId>org.msgpack</groupId>
                <artifactId>msgpack</artifactId>
                <version>0.6.12</version>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.12</version>
            </dependency>
        </dependencies>
    </dependencyManagement>




    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    
</project>
package com.test.zookeeper.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;

/**
 * Created by test
 * 2020/9/28.
 */
public class CuratorDemo {

    private CuratorFramework curatorFramework;

    private String curatorRoot = "/curatorRoot";

    @Before
    public void init() throws Exception {
        curatorFramework = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(3000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();

        curatorFramework.start();
        //curatorFramework.delete().deletingChildrenIfNeeded().forPath(curatorRoot);
        String path = curatorFramework.create().creatingParentsIfNeeded().forPath(curatorRoot);
        System.out.println("init success,create root node:" + path);
    }

    @After
    public void destroy() {
        try {
            curatorFramework.delete().deletingChildrenIfNeeded().forPath(curatorRoot);
            System.out.println("come destroy,delete all the nodes of " + curatorRoot);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void createPersistentNode() {
        try {
            String path = curatorFramework.create()
                    .creatingParentsIfNeeded()
                    .withMode(CreateMode.EPHEMERAL)
                    .forPath(curatorRoot + "/demo1");
            System.out.println(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void getChildren() {
        try {
            String path1 = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(curatorRoot + "/c1/c11/c11");
            System.out.println("create path1=" + path1);
            String path2 = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(curatorRoot + "/c2/c22");
            System.out.println("create path2=" + path2);
            List<String> strings = curatorFramework.getChildren().forPath(curatorRoot);
            System.out.println(strings);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void getValue() {
        try {
            byte[] bytes = curatorFramework.getData().forPath(curatorRoot);
            System.out.println(new String(bytes));
            //System.out.println(InetAddress.getLocalHost());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void exists() {
        try {
            Stat stat = curatorFramework.checkExists().forPath(curatorRoot + "/c1");
            System.out.println(stat);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void update() {
        try {
            Stat path = curatorFramework.setData().forPath(curatorRoot, "hello,world".getBytes());
            byte[] bytes = curatorFramework.getData().forPath(curatorRoot);
            System.out.println(new String(bytes));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testWatch_NodeCache(){
        //监控当前节点数据变化   https://blog.csdn.net/sinat_19687693/article/details/50977567
        NodeCache nodeCache = new NodeCache(curatorFramework, curatorRoot);

        nodeCache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("node changed of the path"+curatorRoot);
            }
        });
        try {
            nodeCache.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.update();
    }

    @Test
    public void testWatch_PathChildrenCache(){
        //PathChildrenCache --监控当前节点子节点的变化 新增 数据改变 删除 https://blog.csdn.net/sinat_19687693/article/details/50977567
        PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework,curatorRoot,true);
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
                System.out.println("---------------------event begin-----------------------------------------");
                System.out.println("pathChildrenCacheEvent="+pathChildrenCacheEvent.getType()+"  curatorFramework="+curatorFramework);
                List<String> list = curatorFramework.getChildren().forPath(curatorRoot);
                System.out.println("childNodeList of root is "+list);
                System.out.println("---------------------event end-----------------------------------------");
            }
        });
        try {
            pathChildrenCache.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(curatorRoot+"/node1/node11");
            System.out.println("创建了新节点,观察事件是否触发呢?");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package com.test.zookeeper.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.concurrent.CountDownLatch;

/**
 * 利用curator 操作zookeeper实现分布式锁
 * 2020/9/28.
 */
public class DistributedLock {

    private static CuratorFramework curatorFramework;

    static {
        curatorFramework = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
        curatorFramework.start();
    }

    static String path = "/lock";

    public static void main(String[] args) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final InterProcessMutex lock = new InterProcessMutex(curatorFramework, path);
        for (int i = 0; i < 30; i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        countDownLatch.await();
                        //获得锁
                        lock.acquire();
                        System.out.println(Thread.currentThread().getName()+"获得锁,睡1秒后释放锁");
                        Thread.sleep(1000);
                        lock.release();
                        System.out.println(Thread.currentThread().getName()+"已释放锁,其他线程可以过来争抢锁了");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            },"Thread" + i).start();
        }
        countDownLatch.countDown();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生就业服务平台管理系统按照操作主体分为管理员和用户。管理员的功能包括学生档案管理、字典管理、试卷管理、试卷选题管理、试题表管理、考试记录表管理、答题详情表管理、错题表管理、法律法规管理、法律法规收藏管理、法律法规留言管理、就业分析管理、论坛管理、企业管理、简历管理、老师管理、简历投递管理、新闻资讯管理、新闻资讯收藏管理、新闻资讯留言管理、学生信息管理、宣传管理、学生管理、职位招聘管理、职位收藏管理、招聘咨询管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 大学生就业服务平台管理系统可以提高大学生就业服务平台信息管理问题的解决效率,优化大学生就业服务平台信息处理流程,保证大学生就业服务平台信息数据的安全,它是一个非常可靠,非常安全的应用程序。 管理员权限操作的功能包括管理新闻信息,管理大学生就业服务平台信息,包括考试管理,培训管理,投递管理,薪资管理等,可以管理新闻信息。 考试管理界面,管理员在考试管理界面中可以对界面中显示,可以对考试信息的考试状态进行查看,可以添加新的考试信息等。投递管理界面,管理员在投递管理界面中查看投递种类信息,投递描述信息,新增投递信息等。新闻信息管理界面,管理员在新闻信息管理界面中新增新闻信息,可以删除新闻信息。新闻信息类型管理界面,管理员在新闻信息类型管理界面查看新闻信息的工作状态,可以对新闻信息的数据进行导出,可以添加新新闻信息的信息,可以编辑新闻信息信息,删除新闻信息信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值