本文整理汇总了Java中org.apache.curator.framework.recipes.leader.LeaderSelector类的典型用法代码示例。如果您正苦于以下问题:Java LeaderSelector类的具体用法?Java LeaderSelector怎么用?Java LeaderSelector使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LeaderSelector类属于org.apache.curator.framework.recipes.leader包,在下文中一共展示了LeaderSelector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
public void start() throws Exception {
client.start();
leaderSelector = new LeaderSelector(client, SnowflakeConfig.getLeaderPath(), this);
leaderSelector.autoRequeue();
LOGGER.info("start SnowflakeServer... ip: {}", getHostname());
leaderSelector.start();
while (!hasLeader()) {
Thread.sleep(1000);
}
initWorkerId();
ReporterHolder.metrics.register(MetricRegistry.name("SnowflakeServer", "workerId"), new Gauge<Integer>() {
@Override
public Integer getValue() {
return workerId;
}
});
}
示例2: ZooKeeperCommandExecutor
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
private ZooKeeperCommandExecutor(String replicaId, CommandExecutor delegate, CuratorFramework curator,
String zkPath, boolean createPathIfNotExist, File revisionFile,
int numWorkers, int maxLogCount, long minLogAgeMillis) {
super(replicaId);
this.delegate = delegate;
this.revisionFile = revisionFile;
this.curator = curator;
this.zkPath = zkPath;
this.createPathIfNotExist = createPathIfNotExist;
this.maxLogCount = maxLogCount;
this.minLogAgeMillis = minLogAgeMillis;
final ThreadPoolExecutor executor = new ThreadPoolExecutor(
numWorkers, numWorkers,
60, TimeUnit.SECONDS, new LinkedTransferQueue<>(),
new DefaultThreadFactory("zookeeper-command-executor", true));
executor.allowCoreThreadTimeOut(true);
this.executor = executor;
logWatcher = new PathChildrenCache(curator, absolutePath(LOG_PATH), true);
logWatcher.getListenable().addListener(this, MoreExecutors.directExecutor());
oldLogRemover = new OldLogRemover();
leaderSelector = new LeaderSelector(curator, absolutePath(LEADER_PATH), oldLogRemover);
leaderSelector.autoRequeue();
}
示例3: initialize
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
private void initialize() throws Exception {
if (!initialized) {
logger.info("lock");
synchronized (this) {
logger.info("lock acquired");
if (!initialized) {
String zkServers = WeixinConfig.getZKServerList();
logger.info("ZK servers: {}", zkServers);
client = CuratorFrameworkFactory.newClient(zkServers, new ExponentialBackoffRetry(1000, 10));
client.start();
client.blockUntilConnected();
logger.info("ZK connected");
if (WeixinConfig.isLeaderMode()) {
leaderSelector = new LeaderSelector(client, getZkLeaderPath(), this);
leaderSelector.autoRequeue();
leaderSelector.start();
}
value = new DistributedAtomicValue(client, getZkValuePath(), new ExponentialBackoffRetry(1000, 10));
initialized = true;
}
}
}
}
示例4: SchedulerSelector
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
public SchedulerSelector(WorkflowManagerImpl workflowManager, QueueFactory queueFactory, AutoCleanerHolder autoCleanerHolder)
{
this.workflowManager = workflowManager;
this.queueFactory = queueFactory;
this.autoCleanerHolder = autoCleanerHolder;
LeaderSelectorListener listener = new LeaderSelectorListenerAdapter()
{
@Override
public void takeLeadership(CuratorFramework client) throws Exception
{
SchedulerSelector.this.takeLeadership();
}
};
leaderSelector = new LeaderSelector(workflowManager.getCurator(), ZooKeeperConstants.getSchedulerLeaderPath(), listener);
leaderSelector.autoRequeue();
}
示例5: LeaderSelectorClient
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
public LeaderSelectorClient(String zookeeperAddress, String electionPath, String leaderPath,
String name) {
ZOOKEEPER_ADDRESS = zookeeperAddress;
ELECTION_PATH = electionPath;
if (leaderPath.endsWith(Constants.PATH_SEPARATOR)) {
LEADER_FOLDER = leaderPath;
} else {
LEADER_FOLDER = leaderPath + Constants.PATH_SEPARATOR;
}
NAME = name;
// create a leader selector using the given path for management
// all participants in a given leader selection must use the same path
// ExampleClient here is also a LeaderSelectorListener but this isn't required
CuratorFramework client =
CuratorFrameworkFactory.newClient(ZOOKEEPER_ADDRESS, new ExponentialBackoffRetry(
Constants.SECOND_MS, 3));
client.start();
LEADER_SELECTOR = new LeaderSelector(client, ELECTION_PATH, this);
LEADER_SELECTOR.setId(name);
// for most cases you will want your instance to requeue when it relinquishes leadership
LEADER_SELECTOR.autoRequeue();
}
示例6: start
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
/**
* Start the registration of the {@link #candidate} for leader election.
*/
@Override
public synchronized void start() {
if (!this.running) {
if (this.client.getState() != CuratorFrameworkState.STARTED) {
// we want to do curator start here because it needs to
// be started before leader selector and it gets a little
// complicated to control ordering via beans so that
// curator is fully started.
this.client.start();
}
this.leaderSelector = new LeaderSelector(this.client, buildLeaderPath(), new LeaderListener());
this.leaderSelector.setId(this.candidate.getId());
this.leaderSelector.autoRequeue();
this.leaderSelector.start();
this.running = true;
}
}
示例7: EventTableChangesToAggregateTopicTranslator
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
public EventTableChangesToAggregateTopicTranslator(CdcKafkaPublisher<EVENT> cdcKafkaPublisher, CdcProcessor<EVENT> cdcProcessor, CuratorFramework client, CdcConfig cdcConfig) {
this.cdcKafkaPublisher = cdcKafkaPublisher;
this.cdcProcessor = cdcProcessor;
this.cdcConfig = cdcConfig;
this.leaderSelector = new LeaderSelector(client, cdcConfig.getLeadershipLockPath(), new EventuateLeaderSelectorListener(this));
}
开发者ID:networknt,项目名称:light-eventuate-4j,代码行数:7,代码来源:EventTableChangesToAggregateTopicTranslator.java
示例8: MasterSelector
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
public MasterSelector(String name, CuratorFramework client, String path) {
this.name=name;
//leaderSelector 多个客户端监听同一个节点
this.leaderSelector=new LeaderSelector(client, path, this);
//保证在此实例释放领导权之后还可能获得领导权
leaderSelector.autoRequeue();
//启动
leaderSelector.start();
}
示例9: ClientSelector
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
public ClientSelector(String name,CuratorFramework client, String path) {
this.name=name;
//leaderSelector 多个客户端监听同一个节点
this.leaderSelector=new LeaderSelector(client, path, this);
//保证在此实例释放领导权之后还可能获得领导权
leaderSelector.autoRequeue();
//启动
leaderSelector.start();
}
示例10: SegmentContainerMonitor
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
/**
* Monitor to manage pravega host addition and removal in the cluster.
*
* @param hostStore The store to read and write the host container mapping data.
* @param client The curator client for coordination.
* @param balancer The host to segment container balancer implementation.
* @param minRebalanceInterval The minimum interval between any two rebalance operations in seconds.
* 0 indicates there can be no waits between retries.
*/
public SegmentContainerMonitor(HostControllerStore hostStore, CuratorFramework client, ContainerBalancer balancer,
int minRebalanceInterval) {
Preconditions.checkNotNull(hostStore, "hostStore");
Preconditions.checkNotNull(client, "client");
Preconditions.checkNotNull(balancer, "balancer");
leaderZKPath = ZKPaths.makePath("cluster", "faulthandlerleader");
segmentMonitorLeader = new SegmentMonitorLeader(hostStore, balancer, minRebalanceInterval);
leaderSelector = new LeaderSelector(client, leaderZKPath, segmentMonitorLeader);
//Listen for any zookeeper connectivity error and relinquish leadership.
client.getConnectionStateListenable().addListener(
(curatorClient, newState) -> {
switch (newState) {
case LOST:
log.warn("Connection to zookeeper lost, attempting to interrrupt the leader thread");
leaderSelector.interruptLeadership();
break;
case SUSPENDED:
if (leaderSelector.hasLeadership()) {
log.info("Zookeeper session suspended, pausing the segment monitor");
segmentMonitorLeader.suspend();
}
break;
case RECONNECTED:
if (leaderSelector.hasLeadership()) {
log.info("Zookeeper session reconnected, resume the segment monitor");
segmentMonitorLeader.resume();
}
break;
//$CASES-OMITTED$
default:
log.debug("Connection state to zookeeper updated: " + newState.toString());
}
}
);
}
示例11: SimpleClient
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
public SimpleClient(String clientName, CuratorFramework client, String path) {
this.clientName = clientName;
this.client = client;
this.path = path;
leaderSelector = new LeaderSelector(this.client,this.path, this);
leaderSelector.autoRequeue();
// initialize oracle change notification
ctx = new ClassPathXmlApplicationContext("spring-context.xml");
consumer = (DBNotifactionConsumer)ctx.getBean("consumer");
}
示例12: ExampleClient
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
public ExampleClient(CuratorFramework client, String path, String name)
{
this.name = name;
// create a leader selector using the given path for management
// all participants in a given leader selection must use the same path
// ExampleClient here is also a LeaderSelectorListener but this isn't required
leaderSelector = new LeaderSelector(client, path, this);
// for most cases you will want your instance to requeue when it relinquishes leadership
leaderSelector.autoRequeue();
}
示例13: KafkaEventStore
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
@Inject
public KafkaEventStore(@Named("event.store.kafka") KafkaConfig config, FieldDependencyBuilder.FieldDependency fieldDependency) {
config = checkNotNull(config, "config is null");
this.sourceFields = Sets.union(fieldDependency.dependentFields.keySet(),
fieldDependency.constantFields.stream().map(SchemaField::getName)
.collect(Collectors.toSet()));
Properties props = new Properties();
props.put("metadata.broker.list", config.getNodes().stream().map(HostAndPort::toString).collect(Collectors.joining(",")));
props.put("serializer.class", config.SERIALIZER);
ProducerConfig producerConfig = new ProducerConfig(props);
this.producer = new Producer(producerConfig);
CuratorFramework client = CuratorFrameworkFactory.newClient(config.getZookeeperNode().toString(),
new ExponentialBackoffRetry(1000, 3));
client.start();
try {
if (client.checkExists().forPath(ZK_OFFSET_PATH) == null)
client.create().forPath(ZK_OFFSET_PATH);
} catch (Exception e) {
LOGGER.error(e, format("Couldn't create event offset path %s", ZK_OFFSET_PATH));
}
new LeaderSelector(client, ZK_OFFSET_PATH, this).start();
}
示例14: run
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
@Override
public void run() {
try {
synchronized (this) {
// want this code to be mutually exclusive with close() .. so if in middle of setup, close
// method will wait till finished
if (closed.get()) {
return;
}
curatorFramework = CuratorUtil.newAppCurator(env.getConfiguration());
CuratorCnxnListener cnxnListener = new CuratorCnxnListener();
curatorFramework.getConnectionStateListenable().addListener(cnxnListener);
curatorFramework.start();
while (!cnxnListener.isConnected()) {
Thread.sleep(200);
}
leaderSelector = new LeaderSelector(curatorFramework, ZookeeperPath.ORACLE_SERVER, this);
pathChildrenCache =
new PathChildrenCache(curatorFramework, ZookeeperPath.ORACLE_SERVER, true);
pathChildrenCache.getListenable().addListener(this);
pathChildrenCache.start();
connect();
}
doWork();
} catch (Exception e) {
if (!closed.get()) {
log.error("Exception occurred in run() method", e);
} else {
log.debug("Exception occurred in run() method", e);
}
}
}
示例15: start
import org.apache.curator.framework.recipes.leader.LeaderSelector; //导入依赖的package包/类
public synchronized void start() throws Exception {
if (started) {
throw new IllegalStateException();
}
final InetSocketAddress addr = startServer();
curatorFramework = CuratorUtil.newAppCurator(env.getConfiguration());
curatorFramework.getConnectionStateListenable().addListener(cnxnListener);
curatorFramework.start();
while (!cnxnListener.isConnected()) {
Thread.sleep(200);
}
leaderSelector = new LeaderSelector(curatorFramework, ZookeeperPath.ORACLE_SERVER, this);
String leaderId = HostUtil.getHostName() + ":" + addr.getPort();
leaderSelector.setId(leaderId);
log.info("Leader ID = " + leaderId);
leaderSelector.start();
pathChildrenCache = new PathChildrenCache(curatorFramework, oraclePath, true);
pathChildrenCache.getListenable().addListener(this);
pathChildrenCache.start();
while (!cnxnListener.isConnected()) {
Thread.sleep(200);
}
log.info("Listening " + addr);
started = true;
}