做网站标配,图片做视频在线观看网站,大型html5浅蓝色网站设计公司dede模板,工信部备案网站1.什么是Atomix#xff1f;
Atomix是一个能用的Java框架#xff0c;用来构建高可用的分布式系统。它是基于RAFT协议的实现#xff0c;为用户提供了各种原子数据结构#xff0c;比如map/set/integer等#xff0c;这些数据结构都可以在整个集群中共享并保证一致性#xff…1.什么是Atomix
Atomix是一个能用的Java框架用来构建高可用的分布式系统。它是基于RAFT协议的实现为用户提供了各种原子数据结构比如map/set/integer等这些数据结构都可以在整个集群中共享并保证一致性同时也提供了LeaderElection的原子对象用来注册候选主结点、监听相关事件等的功能。
大多数分布式应用都需要一些有状态的组件来实现一致性和容错性。Atomix是一个可嵌入的库有助于实现分布式资源的容错和一致性。
它提供了一套丰富的API用于管理其资源如集合、组和并发的工具。
2.代码工程
pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringboot-demo/artifactIdgroupIdcom.et/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdatomix/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetatomix.version3.1.12/atomix.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-autoconfigure/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdio.atomix/groupIdartifactIdatomix/artifactIdversion${atomix.version}/version/dependencydependencygroupIdio.atomix/groupIdartifactIdatomix-raft/artifactIdversion${atomix.version}/version/dependency/dependencies
/project
建立集群
private static Atomix buildAtomix() {ListString raftMembers Collections.singletonList(node1);//创建atomixreturn Atomix.builder(AtomixCluster.class.getClassLoader()).withClusterId(my-cluster).withMemberId(node1).withHost(127.0.0.1).withPort(6789).withMembershipProvider(BootstrapDiscoveryProvider.builder().build()).withManagementGroup(RaftPartitionGroup.builder(system).withNumPartitions(1).withDataDirectory(new File(LOCAL_DATA_DIR, system)).withMembers(raftMembers).build()).addPartitionGroup(RaftPartitionGroup.builder(groupName).withNumPartitions(raftMembers.size()).withDataDirectory(new File(LOCAL_DATA_DIR, data)).withMembers(raftMembers).build()).build();
}
分布式Map存储
Atomix atomix buildAtomix();
//atomix启动并加入集群
atomix.start().join();//创建atomixMap
AsyncAtomicMapObject, Object asyncAtomicMap atomix.atomicMapBuilder(myCfgName).withProtocol(MultiRaftProtocol.builder(groupName).withRecoveryStrategy(Recovery.RECOVER).withMaxRetries(MAX_RETRIES).build()).withReadOnly(false).build().async();
//进行数据存储
asyncAtomicMap.put(HBLOG, http://www.liuhaihua.cn);
//进行查询
CompletableFutureVersionedObject myBlog asyncAtomicMap.get(HBLOG);
VersionedObject objectVersioned myBlog.get();
System.out.printf(value:%s version:%s%n, objectVersioned.value(), objectVersioned.version());
选举
//Elector
AsyncLeaderElector leaderElector atomix.leaderElectorBuilder(leader).withProtocol(MultiRaftProtocol.builder(groupName).withRecoveryStrategy(Recovery.RECOVER).withMaxRetries(MAX_RETRIES).withMaxTimeout(Duration.ofMillis(15000L)).build()).withReadOnly(false).build().async();
//获取出当前节点
Member localMember atomix.getMembershipService().getLocalMember();
System.out.println(localMember: localMember.toString());
String topic this is a topic;
//根据某一topic选举出leader,返回的是选举为leader的节点
Leadership leadership (Leadership) leaderElector.run(topic, localMember.toString()).get();
System.out.println( leadership);
//get leadership
Leadership topicLeadership (Leadership) leaderElector.getLeadership(topic).get();
System.out.println(------------ topicLeadership);
//输出所有的topic对应的leader
Map topicLeadershipMaps (Map) leaderElector.getLeaderships().get();
System.out.println( topicLeadershipMaps.toString());
以上只是一些关键代码所有代码请参见下面代码仓库
代码仓库
https://github.com/Harries/springboot-demo
3.总结
atomix的api远不止本例中的两个还有其他很多的api。如分布式锁、分布式事务、分布式自增id、分布式队列、分布式信息号等这些在atomix中都有实现详细可见atomix的类方法更多关于atomix的相关介绍可以在对应的github中找到 https://github.com/atomix/atomix-archive
需要注意的是基于java的atomix现已停止维护这里仅用作学习目的在分布式系统中体验一下。
4.引用
Raft Consensus Algorithmhttps://github.com/maemual/raft-zh_cn/blob/master/raft-zh_cn.mdHelm chart repository for Atomix | atomixhttps://raft.github.io/raft.pdfJava CompletableFuture 详解