本文整理汇总了Java中com.ecwid.consul.v1.ConsulClient类的典型用法代码示例。如果您正苦于以下问题:Java ConsulClient类的具体用法?Java ConsulClient怎么用?Java ConsulClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConsulClient类属于com.ecwid.consul.v1包,在下文中一共展示了ConsulClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setupWatch
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
private void setupWatch(ApplicationEventPublisher eventPublisher, GetValue getValue, String context, String aclToken) {
ConsulClient consul = mock(ConsulClient.class);
List<GetValue> getValues = null;
if (getValue != null) {
getValues = Arrays.asList(getValue);
}
Response<List<GetValue>> response = new Response<>(getValues, 1L, false, 1L);
when(consul.getKVValues(eq(context), nullable(String.class), any(QueryParams.class))).thenReturn(response);
if (StringUtils.hasText(aclToken)) {
configProperties.setAclToken(aclToken);
}
LinkedHashMap<String, Long> initialIndexes = new LinkedHashMap<>();
initialIndexes.put(context, 0L);
ConfigWatch watch = new ConfigWatch(configProperties, consul, initialIndexes);
watch.setApplicationEventPublisher(eventPublisher);
watch.start();
watch.watchConfigKeyValues();
}
示例2: init
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@PostConstruct
public void init() {
consulClient = new ConsulClient(agentHost);
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
log.info("begin to load from registry");
servicesPassing.clear();
servicesFailing.clear();
loadAllServiceFromConsul();
} catch (Throwable e) {
log.error(e.getMessage(), e);
}
}
}, 0, 1, TimeUnit.MINUTES);
}
示例3: run
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@Override
public void run() {
Lock lock = new Lock(new ConsulClient(), "lock-key", checkTtl);
try {
// 获取分布式互斥锁(参数含义:阻塞模式、每次尝试获取锁的间隔500ms、尝试n次)
if (lock.lock(true, 500L, null)) {
log.info("Thread {} start!", flag);
// 处理业务逻辑
Thread.sleep(new Random().nextInt(5000));
log.info("Thread {} end!", flag);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
示例4: beforeClass
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@BeforeClass
public static void beforeClass() {
assumeTrue(CanConnect.to(new InetSocketAddress(CONSUL_HOST, CONSUL_PORT)));
ConsulClient client = new ConsulClient();
Response<List<CatalogService>> response = client.getCatalogService("vault",
QueryParams.DEFAULT);
if (response.getValue().isEmpty()) {
NewService service = new NewService();
service.setAddress("localhost");
service.setPort(8200);
service.setId("vault");
service.setName("vault");
client.agentServiceRegister(service);
}
}
示例5: setup
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@Before
public void setup() {
this.properties = new ConsulProperties();
this.client = new ConsulClient(properties.getHost(), properties.getPort());
this.client.setKVValue(ROOT+ APPLICATION_YML, "foo: bar\nmy.baz: ${foo}");
this.client.setKVValue(ROOT+ APPLICATION_DEV_YML, "foo: bar-dev\nmy.baz: ${foo}");
this.client.setKVValue(ROOT+"/master.ref", UUID.randomUUID().toString());
this.client.setKVValue(ROOT+APP_NAME_PROPS, "foo: bar-app\nmy.baz: ${foo}");
this.client.setKVValue(ROOT+APP_NAME_DEV_PROPS, "foo: bar-app-dev\nmy.baz: ${foo}");
this.context = new SpringApplicationBuilder(Config.class)
.web(false)
.run("--spring.application.name="+ APP_NAME,
"--spring.cloud.consul.config.prefix="+ROOT,
"--spring.cloud.consul.config.format=FILES",
"--spring.profiles.active=dev",
"spring.cloud.consul.config.watch.delay=1");
this.client = context.getBean(ConsulClient.class);
this.properties = context.getBean(ConsulProperties.class);
this.environment = context.getEnvironment();
}
示例6: firstCallDoesNotPublishEvent
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@Test
public void firstCallDoesNotPublishEvent() {
ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class);
configProperties.setFormat(FILES);
GetValue getValue = new GetValue();
String context = "/config/app.yml";
ConsulClient consul = mock(ConsulClient.class);
List<GetValue> getValues = Collections.singletonList(getValue);
Response<List<GetValue>> response = new Response<>(getValues, 1L, false, 1L);
when(consul.getKVValues(eq(context), anyString(), any(QueryParams.class))).thenReturn(response);
ConfigWatch watch = new ConfigWatch(configProperties, consul, new LinkedHashMap<String, Long>());
watch.setApplicationEventPublisher(eventPublisher);
watch.watchConfigKeyValues();
verify(eventPublisher, times(0)).publishEvent(any(RefreshEvent.class));
}
示例7: setup
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@Before
public void setup() {
this.properties = new ConsulProperties();
this.client = new ConsulClient(properties.getHost(), properties.getPort());
this.client.deleteKVValues(PREFIX);
this.client.setKVValue(KEY1, VALUE1);
this.client.setKVValue(KEY2, VALUE2);
this.context = new SpringApplicationBuilder(Config.class)
.web(false)
.run("--SPRING_APPLICATION_NAME="+ APP_NAME,
"--spring.cloud.consul.config.prefix="+ROOT,
"spring.cloud.consul.config.watch.delay=10");
this.client = context.getBean(ConsulClient.class);
this.properties = context.getBean(ConsulProperties.class);
this.environment = context.getEnvironment();
}
示例8: setup
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@Before
public void setup() {
this.properties = new ConsulProperties();
this.client = new ConsulClient(properties.getHost(), properties.getPort());
this.client.deleteKVValues(PREFIX);
this.client.setKVValue(KEY1, VALUE1);
this.client.setKVValue(KEY2, VALUE2);
this.context = new SpringApplicationBuilder(Config.class)
.web(false)
.run("--spring.application.name=testConsulPropertySourceLocatorAppNameCustomized",
"--spring.cloud.consul.config.name="+CONFIG_NAME,
"--spring.cloud.consul.config.prefix="+ROOT);
this.client = context.getBean(ConsulClient.class);
this.properties = context.getBean(ConsulProperties.class);
this.environment = context.getEnvironment();
}
开发者ID:spring-cloud,项目名称:spring-cloud-consul,代码行数:20,代码来源:ConsulPropertySourceLocatorAppNameCustomizedTests.java
示例9: TtlScheduler
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
public TtlScheduler(ConsulClient client){
this.client = client;
heartbeatServiceExecutor.scheduleAtFixedRate(new ConsulHeartbeatServiceTask(), ConsulConstants.HEARTBEAT_CIRCLE,
ConsulConstants.HEARTBEAT_CIRCLE, TimeUnit.MILLISECONDS);
heartbeatSessionExecutor.scheduleAtFixedRate(new ConsulHeartbeatSessionTask(), ConsulConstants.HEARTBEAT_CIRCLE,
ConsulConstants.HEARTBEAT_CIRCLE, TimeUnit.MILLISECONDS);
}
示例10: newNameResolver
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@Nullable
@Override
public ConsulNameResolver newNameResolver(final URI targetUri, final Attributes params) {
if (!SCHEME.equals(targetUri.getScheme())) {
return null;
}
final String targetPath = checkNotNull(targetUri.getPath(), "targetPath");
checkArgument(targetPath.startsWith("/"));
final String serviceName = targetPath.substring(1);
checkArgument(serviceName.length() > 0, "serviceName");
String consulHost = targetUri.getHost();
if (Strings.isNullOrEmpty(consulHost)) {
consulHost = DEFAULT_HOST;
}
int consulPort = targetUri.getPort();
if (consulPort == -1) {
consulPort = DEFAULT_PORT;
}
final String tag = Strings.emptyToNull(targetUri.getFragment());
final ConsulClient consulClient = ConsulClientManager.getInstance(consulHost, consulPort);
return new ConsulNameResolver(
consulClient /* CatalogClient */,
consulClient /* KeyValueClient */,
serviceName,
Optional.ofNullable(tag),
GrpcUtil.TIMER_SERVICE,
GrpcUtil.SHARED_CHANNEL_EXECUTOR
);
}
示例11: getInstance
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
/**
* Get or create the {@link ConsulClient} instance for the given {@code
* host} / {@code port} / {@link TLSConfig} triplet.
*
* @param host The hostname of the consul instance we are attempting to
* connect to.
* @param port The port of the consul instance we are attempting to connect
* to.
* @param tlsConfig The TLS configuration used to secure communication with
* the consul instance.
* @return The consul client for the given triplet.
*/
public static ConsulClient getInstance(
final String host,
final int port,
@Nullable final TLSConfig tlsConfig
) {
return INSTANCE.index.computeIfAbsent(new IndexKey(host, port, tlsConfig), (key) -> {
if (tlsConfig == null) {
return new ConsulClient(host, port);
} else {
return new ConsulClient(host, port, tlsConfig);
}
});
}
示例12: getInstance
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@Test
public void getInstance() throws Exception {
final ConsulClient c1 = ConsulClientManager.getInstance("localhost", 8500);
final ConsulClient c2 = ConsulClientManager.getInstance("localhost", 8500);
final ConsulClient c3 = ConsulClientManager.getInstance("127.0.0.1", 8500);
assertTrue(c1 == c2);
assertFalse(c2 == c3);
}
示例13: testLock
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@Test
public void testLock() throws Exception {
ConsulClient consulClient = new ConsulClient();
CheckTtl checkTtl = new CheckTtl("lock-1", consulClient);
new Thread(new LockRunner(1, new CheckTtl("lock-1", consulClient))).start();
new Thread(new LockRunner(2, new CheckTtl("lock-2", consulClient))).start();
new Thread(new LockRunner(3, new CheckTtl("lock-3", consulClient))).start();
new Thread(new LockRunner(4, new CheckTtl("lock-4", consulClient))).start();
new Thread(new LockRunner(5, new CheckTtl("lock-5", consulClient))).start();
Thread.sleep(30000L);
}
示例14: testSemaphore
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
@Test
public void testSemaphore() throws Exception {
ConsulClient consulClient = new ConsulClient();
new Thread(new SemaphoreRunner(1, new CheckTtl("semaphore-1", consulClient))).start();
new Thread(new SemaphoreRunner(2, new CheckTtl("semaphore-2", consulClient))).start();
new Thread(new SemaphoreRunner(3, new CheckTtl("semaphore-3", consulClient))).start();
new Thread(new SemaphoreRunner(4, new CheckTtl("semaphore-4", consulClient))).start();
new Thread(new SemaphoreRunner(5, new CheckTtl("semaphore-5", consulClient))).start();
new Thread(new SemaphoreRunner(6, new CheckTtl("semaphore-6", consulClient))).start();
new Thread(new SemaphoreRunner(7, new CheckTtl("semaphore-7", consulClient))).start();
new Thread(new SemaphoreRunner(8, new CheckTtl("semaphore-8", consulClient))).start();
new Thread(new SemaphoreRunner(9, new CheckTtl("semaphore-9", consulClient))).start();
new Thread(new SemaphoreRunner(10, new CheckTtl("semaphore-10", consulClient))).start();
Thread.sleep(50000L);
}
示例15: ConsulRegistry
import com.ecwid.consul.v1.ConsulClient; //导入依赖的package包/类
public ConsulRegistry(URL url) {
super(url);
if (url.isAnyHost()) {
throw new IllegalStateException("registry address == null");
}
consulClient = new ConsulClient(url.getHost(), url.getPort());
}