本文整理汇总了Java中com.ecwid.consul.v1.kv.model.GetValue类的典型用法代码示例。如果您正苦于以下问题:Java GetValue类的具体用法?Java GetValue怎么用?Java GetValue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GetValue类属于com.ecwid.consul.v1.kv.model包,在下文中一共展示了GetValue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: lookupRouterMessage
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
public ConsulRouterResp lookupRouterMessage(String serviceName, long lastConsulIndex) {
QueryParams queryParams = new QueryParams(ConsulConstants.CONSUL_BLOCK_TIME_SECONDS, lastConsulIndex);
Response<GetValue> orgResponse = client.getKVValue(serviceName, queryParams);
GetValue getValue = orgResponse.getValue();
if (getValue != null && StringUtils.isNoneBlank(getValue.getValue())) {
String router = new String(Base64.decodeBase64(getValue.getValue()));
ConsulRouterResp response = ConsulRouterResp.newResponse()//
.withValue(router)//
.withConsulIndex(orgResponse.getConsulIndex())//
.withConsulLastContact(orgResponse.getConsulLastContact())//
.withConsulKnowLeader(orgResponse.isConsulKnownLeader())//
.build();
return response;
}
return null;
}
示例2: setupWatch
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的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();
}
示例3: runOnce
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
@VisibleForTesting
protected void runOnce() {
try {
Response<List<GetValue>> kvals = updateIndex(getRaw(watchParams()));
ImmutableMap<String, Object> full = convertToMap(kvals);
final WatchedUpdateResult result;
if (lastState.get() == null) {
result = WatchedUpdateResult.createFull(full);
} else {
result = incrementalResult(full, lastState.get());
}
lastState.set(full);
fireEvent(result);
} catch (Exception e) {
LOGGER.error("Error watching path", e);
}
}
示例4: getKVValue
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
@Override
public Response<GetValue> getKVValue(String key, String token, QueryParams queryParams) {
UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
RawResponse rawResponse = rawClient.makeGetRequest("/v1/kv/" + key, tokenParams, queryParams);
if (rawResponse.getStatusCode() == 200) {
List<GetValue> value = GsonFactory.getGson().fromJson(rawResponse.getContent(), new TypeToken<List<GetValue>>(){}.getType());
if (value.size() == 0) {
return new Response<GetValue>(null, rawResponse);
} else if (value.size() == 1) {
return new Response<GetValue>(value.get(0), rawResponse);
} else {
throw new ConsulException("Strange response (list size=" + value.size() + ")");
}
} else if (rawResponse.getStatusCode() == 404) {
return new Response<GetValue>(null, rawResponse);
} else {
throw new OperationException(rawResponse);
}
}
示例5: getKVValues
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
@Override
public Response<List<GetValue>> getKVValues(String keyPrefix, String token, QueryParams queryParams) {
UrlParameters recurseParam = new SingleUrlParameters("recurse");
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
RawResponse rawResponse = rawClient.makeGetRequest("/v1/kv/" + keyPrefix, recurseParam, tokenParam, queryParams);
if (rawResponse.getStatusCode() == 200) {
List<GetValue> value = GsonFactory.getGson().fromJson(rawResponse.getContent(), new TypeToken<List<GetValue>>() {
}.getType());
return new Response<List<GetValue>>(value, rawResponse);
} else if (rawResponse.getStatusCode() == 404) {
return new Response<List<GetValue>>(null, rawResponse);
} else {
throw new OperationException(rawResponse);
}
}
示例6: getKVValue
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
@Override
public Response<GetValue> getKVValue(String key, String token, QueryParams queryParams) {
UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
RawResponse rawResponse = rawClient.makeGetRequest("/v1/kv/" + key, tokenParams, queryParams);
if (rawResponse.getStatusCode() == 200) {
List<GetValue> value = GsonFactory.getGson().fromJson(rawResponse.getContent(), new TypeToken<List<GetValue>>() {
}.getType());
if (value.size() == 0) {
return new Response<GetValue>(null, rawResponse);
} else if (value.size() == 1) {
return new Response<GetValue>(value.get(0), rawResponse);
} else {
throw new ConsulException("Strange response (list size=" + value.size() + ")");
}
} else if (rawResponse.getStatusCode() == 404) {
return new Response<GetValue>(null, rawResponse);
} else {
throw new OperationException(rawResponse);
}
}
示例7: init
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
public void init() {
if (!this.context.endsWith("/")) {
this.context = this.context + "/";
}
Response<List<GetValue>> response = source.getKVValues(context,
configProperties.getAclToken(), QueryParams.DEFAULT);
initialIndex = response.getConsulIndex();
final List<GetValue> values = response.getValue();
ConsulConfigProperties.Format format = configProperties.getFormat();
switch (format) {
case KEY_VALUE:
parsePropertiesInKeyValueFormat(values);
break;
case PROPERTIES:
case YAML:
parsePropertiesWithNonKeyValueFormat(values, format);
}
}
示例8: firstCallDoesNotPublishEvent
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的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));
}
示例9: release
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
/**
* 释放session、并从lock中移除当前的sessionId
*
* @throws IOException
*/
public void release() throws IOException {
if (this.acquired) {
// remove session int /.lock's holders list
while (true) {
String contenderKey = keyPath + "/" + sessionId;
String lockKey = keyPath + "/.lock";
GetValue lockKeyContent = consulClient.getKVValue(lockKey).getValue();
if (lockKeyContent != null) {
// lock值转换
ContenderValue contenderValue = ContenderValue.parse(lockKeyContent);
contenderValue.getHolders().remove(sessionId);
PutParams putParams = new PutParams();
putParams.setCas(lockKeyContent.getModifyIndex());
consulClient.deleteKVValue(contenderKey);
boolean c = consulClient.setKVValue(lockKey, contenderValue.toString(), putParams).getValue();
if (c) {
break;
}
}
}
}
// remove session key
this.acquired = false;
clearSession();
}
示例10: clearInvalidHolder
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
public void clearInvalidHolder(ContenderValue contenderValue) throws IOException {
log.debug("Semaphore limited {}, remove invalid session...", contenderValue.getLimit());
// 获取/semaphore/<key>/下的所有竞争者session
Map<String, String> aliveSessionMap = new HashMap<>();
List<GetValue> sessionList = consulClient.getKVValues(keyPath).getValue();
for (GetValue value : sessionList) {
String session = value.getSession();
if (session == null || value.getSession().isEmpty()) {
continue;
}
aliveSessionMap.put(session, "");
}
String lockKey = keyPath + "/.lock";
GetValue lockKeyContent = consulClient.getKVValue(lockKey).getValue();
if (lockKeyContent != null) {
// 清理holders中存储的不在semaphore/<key>/<session>中的session(说明该session已经被释放了)
List<String> removeList = new LinkedList<>();
for(int i = 0; i < contenderValue.getHolders().size(); i ++) {
String holder = contenderValue.getHolders().get(i);
if (!aliveSessionMap.containsKey(holder)) {
// 该session已经失效,需要从holder中剔除
removeList.add(holder);
}
}
if (removeList.size() > 0) {
contenderValue.getHolders().removeAll(removeList);
// 清理失效的holder
PutParams putParams = new PutParams();
putParams.setCas(lockKeyContent.getModifyIndex());
consulClient.setKVValue(lockKey, contenderValue.toString(), putParams).getValue();
}
}
}
示例11: parse
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
/**
* 根据consul中获取的/.lock值来转换
*
* @param lockKeyContent
* @return
*/
@SneakyThrows
public static ContenderValue parse(GetValue lockKeyContent) {
// 获取Value信息,decode BASE64
BASE64Decoder decoder = new BASE64Decoder();
byte[] v = decoder.decodeBuffer(lockKeyContent.getValue());
String lockKeyValueDecode = new String(v);
// 根据json转换为ContenderValue对象
Gson gson = new Gson();
return gson.fromJson(lockKeyValueDecode, ContenderValue.class);
}
示例12: getRate
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
@Override
protected Rate getRate(String key) {
Rate rate = null;
GetValue value = this.consulClient.getKVValue(key).getValue();
if (value != null && value.getDecodedValue() != null) {
try {
rate = this.objectMapper.readValue(value.getDecodedValue(), Rate.class);
} catch (IOException e) {
log.error("Failed to deserialize Rate", e);
}
}
return rate;
}
示例13: testGetRateException
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
@Test
public void testGetRateException() throws IOException {
GetValue getValue = new GetValue();
getValue.setValue("");
when(consulClient.getKVValue(any())).thenReturn(new Response<>(getValue, 1L, true, 1L));
when(objectMapper.readValue(anyString(), eq(Rate.class))).thenThrow(new IOException());
ConsulRateLimiter consulRateLimiter = new ConsulRateLimiter(rateLimiterErrorHandler, consulClient, objectMapper);
Rate rate = consulRateLimiter.getRate("");
assertThat(rate).isNull();
}
示例14: testRateLimitExceedCapacity
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
@Test
@Override
@SuppressWarnings("unchecked")
public void testRateLimitExceedCapacity() throws Exception {
Response<GetValue> response = mock(Response.class);
GetValue getValue = mock(GetValue.class);
when(this.consulClient.getKVValue(anyString())).thenReturn(response);
when(response.getValue()).thenReturn(getValue);
when(getValue.getDecodedValue()).thenReturn(this.objectMapper.writeValueAsString(this.rate(-1)));
super.testRateLimitExceedCapacity();
}
示例15: testRateLimit
import com.ecwid.consul.v1.kv.model.GetValue; //导入依赖的package包/类
@Test
@Override
@SuppressWarnings("unchecked")
public void testRateLimit() throws Exception {
Response<GetValue> response = mock(Response.class);
GetValue getValue = mock(GetValue.class);
when(this.consulClient.getKVValue(anyString())).thenReturn(response);
when(response.getValue()).thenReturn(getValue);
when(getValue.getDecodedValue()).thenReturn(this.objectMapper.writeValueAsString(this.rate(1)));
this.request.setRequestURI("/serviceA");
this.request.setRemoteAddr("10.0.0.100");
assertTrue(this.filter.shouldFilter());
for (int i = 0; i < 2; i++) {
this.filter.run();
}
String key = "null_serviceA_serviceA_10.0.0.100_anonymous";
String remaining = this.response.getHeader(RateLimitPreFilter.REMAINING_HEADER + key);
assertEquals("0", remaining);
TimeUnit.SECONDS.sleep(2);
when(getValue.getDecodedValue()).thenReturn(this.objectMapper.writeValueAsString(this.rate(2)));
this.filter.run();
remaining = this.response.getHeader(RateLimitPreFilter.REMAINING_HEADER + key);
assertEquals("1", remaining);
}