com.ecwid.consul.v1.kv.model.GetValue Java Examples
The following examples show how to use
com.ecwid.consul.v1.kv.model.GetValue.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsulClient.java From saluki with Apache License 2.0 | 6 votes |
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; }
Example #2
Source File: KeyValueConsulClient.java From consul-api with Apache License 2.0 | 6 votes |
@Override public Response<GetValue> getKVValue(String key, String token, QueryParams queryParams) { UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null; HttpResponse httpResponse = rawClient.makeGetRequest("/v1/kv/" + key, tokenParams, queryParams); if (httpResponse.getStatusCode() == 200) { List<GetValue> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<GetValue>>() { }.getType()); if (value.size() == 0) { return new Response<GetValue>(null, httpResponse); } else if (value.size() == 1) { return new Response<GetValue>(value.get(0), httpResponse); } else { throw new ConsulException("Strange response (list size=" + value.size() + ")"); } } else if (httpResponse.getStatusCode() == 404) { return new Response<GetValue>(null, httpResponse); } else { throw new OperationException(httpResponse); } }
Example #3
Source File: KeyValueConsulClient.java From consul-api with Apache License 2.0 | 6 votes |
@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; HttpResponse httpResponse = rawClient.makeGetRequest("/v1/kv/" + keyPrefix, recurseParam, tokenParam, queryParams); if (httpResponse.getStatusCode() == 200) { List<GetValue> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<GetValue>>() { }.getType()); return new Response<List<GetValue>>(value, httpResponse); } else if (httpResponse.getStatusCode() == 404) { return new Response<List<GetValue>>(null, httpResponse); } else { throw new OperationException(httpResponse); } }
Example #4
Source File: ConsulPropertySource.java From spring-cloud-consul with Apache License 2.0 | 6 votes |
public void init() { if (!this.context.endsWith("/")) { this.context = this.context + "/"; } Response<List<GetValue>> response = this.source.getKVValues(this.context, this.configProperties.getAclToken(), QueryParams.DEFAULT); this.initialIndex = response.getConsulIndex(); final List<GetValue> values = response.getValue(); ConsulConfigProperties.Format format = this.configProperties.getFormat(); switch (format) { case KEY_VALUE: parsePropertiesInKeyValueFormat(values); break; case PROPERTIES: case YAML: parsePropertiesWithNonKeyValueFormat(values, format); } }
Example #5
Source File: ConfigWatchTests.java From spring-cloud-formula with Apache License 2.0 | 6 votes |
@Test public void firstCallDoesNotPublishEvent() { ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class); this.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(ArgumentMatchers.eq(context), ArgumentMatchers.anyString(), ArgumentMatchers.any(QueryParams.class))) .thenReturn(response); ConfigWatch watch = new ConfigWatch(this.configProperties, consul, bmsAuthClient, new LinkedHashMap<String, Long>()); watch.setApplicationEventPublisher(eventPublisher); watch.watchConfigKeyValues(context); verify(eventPublisher, times(0)).publishEvent(ArgumentMatchers.any(RefreshEvent.class)); }
Example #6
Source File: ConfigWatchTests.java From spring-cloud-consul with Apache License 2.0 | 6 votes |
@Test public void firstCallDoesNotPublishEvent() { ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class); this.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(this.configProperties, consul, new LinkedHashMap<String, Long>()); watch.setApplicationEventPublisher(eventPublisher); watch.watchConfigKeyValues(); verify(eventPublisher, times(0)).publishEvent(any(RefreshEvent.class)); }
Example #7
Source File: ConfigWatchTests.java From spring-cloud-consul with Apache License 2.0 | 6 votes |
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)) { this.configProperties.setAclToken(aclToken); } LinkedHashMap<String, Long> initialIndexes = new LinkedHashMap<>(); initialIndexes.put(context, 0L); ConfigWatch watch = new ConfigWatch(this.configProperties, consul, initialIndexes); watch.setApplicationEventPublisher(eventPublisher); watch.start(); watch.watchConfigKeyValues(); }
Example #8
Source File: ContenderValue.java From consul-distributed-lock with Apache License 2.0 | 5 votes |
/** * 根据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); }
Example #9
Source File: ConsulFilesPropertySource.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
public void init(GetValue getValue) { if (this.getContext().endsWith(".yml") || this.getContext().endsWith(".yaml")) { parseValue(getValue, Format.YAML); } else if (this.getContext().endsWith(".properties")) { parseValue(getValue, Format.PROPERTIES); } else { throw new IllegalStateException( "Unknown files extension for context " + this.getContext()); } }
Example #10
Source File: ConsulPropertySource.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
public void init() { if (!this.context.endsWith("/")) { this.context = this.context + "/"; } if (configProperties.isTokenEnabled() && StringUtils.isEmpty(bmsAuthClient.getToken())) { bmsAuthClient.getTokenFromServer(configProperties.getAuthUri()); } logger.info("Try to get KV from consul for context: " + this.context); Response<List<GetValue>> response = this.source.getKVValues(this.context, this.bmsAuthClient.getToken(), new QueryParams(ConsistencyMode.STALE)); this.initialIndex = response.getConsulIndex(); final List<GetValue> values = response.getValue(); Format format = this.configProperties.getFormat(); switch (format) { case KEY_VALUE: parsePropertiesInKeyValueFormat(values); logger.info("Properties for context " + this.context + "is "); for (Map.Entry<String, Object> entry : this.properties.entrySet()) { logger.info(entry.getKey() + ": " + entry.getValue().toString()); } break; case PROPERTIES: break; case YAML: parsePropertiesWithNonKeyValueFormat(values, format); break; default: break; } }
Example #11
Source File: ConsulFilesPropertySource.java From spring-cloud-consul with Apache License 2.0 | 5 votes |
public void init(GetValue value) { if (this.getContext().endsWith(".yml") || this.getContext().endsWith(".yaml")) { parseValue(value, YAML); } else if (this.getContext().endsWith(".properties")) { parseValue(value, PROPERTIES); } else { throw new IllegalStateException( "Unknown files extension for context " + this.getContext()); } }
Example #12
Source File: ConsulPropertySource.java From spring-cloud-consul with Apache License 2.0 | 5 votes |
/** * Parses the properties in key value style i.e., values are expected to be either a * sub key or a constant. * @param values values to parse */ protected void parsePropertiesInKeyValueFormat(List<GetValue> values) { if (values == null) { return; } for (GetValue getValue : values) { String key = getValue.getKey(); if (!StringUtils.endsWithIgnoreCase(key, "/")) { key = key.replace(this.context, "").replace('/', '.'); String value = getValue.getDecodedValue(); this.properties.put(key, value); } } }
Example #13
Source File: ConsulRateLimitPreFilterTest.java From spring-cloud-zuul-ratelimit with Apache License 2.0 | 5 votes |
@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_10.0.0.100_anonymous"; String remaining = this.response.getHeader(HEADER_REMAINING + 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(HEADER_REMAINING + key); assertEquals("1", remaining); }
Example #14
Source File: ConsulRateLimitPreFilterTest.java From spring-cloud-zuul-ratelimit with Apache License 2.0 | 5 votes |
@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(); }
Example #15
Source File: ConsulRateLimiterTest.java From spring-cloud-zuul-ratelimit with Apache License 2.0 | 5 votes |
@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))).thenAnswer(invocation -> { throw new IOException(); }); ConsulRateLimiter consulRateLimiter = new ConsulRateLimiter(rateLimiterErrorHandler, consulClient, objectMapper); Rate rate = consulRateLimiter.getRate(""); assertThat(rate).isNull(); }
Example #16
Source File: ConsulRateLimiter.java From spring-cloud-zuul-ratelimit with Apache License 2.0 | 5 votes |
@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; }
Example #17
Source File: ConsulPropertySource.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
protected void parseValue(GetValue getValue, Format format) { String value = getValue.getDecodedValue(); if (value == null) { return; } Properties props = generateProperties(value, format); for (Map.Entry entry : props.entrySet()) { this.properties.put(entry.getKey().toString(), entry.getValue()); } }
Example #18
Source File: Semaphore.java From consul-distributed-lock with Apache License 2.0 | 5 votes |
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(); } } }
Example #19
Source File: Semaphore.java From consul-distributed-lock with Apache License 2.0 | 5 votes |
/** * 释放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(); }
Example #20
Source File: ConsulDataSource.java From Sentinel with Apache License 2.0 | 5 votes |
/** * Get data from Consul (blocking). * * @param key data key in Consul * @param index the index of data in Consul. * @param waitTime time(second) for waiting get updated value. * @return the value associated to the key, or null if error occurs */ private Response<GetValue> getValue(String key, long index, long waitTime) { try { return client.getKVValue(key, new QueryParams(waitTime, index)); } catch (Throwable t) { RecordLog.warn("[ConsulDataSource] Failed to get value for key: " + key, t); } return null; }
Example #21
Source File: ConsulPropertySource.java From spring-cloud-consul with Apache License 2.0 | 5 votes |
/** * Parses the properties using the format which is not a key value style i.e., either * java properties style or YAML style. * @param values values to parse * @param format format in which the values should be parsed */ protected void parsePropertiesWithNonKeyValueFormat(List<GetValue> values, ConsulConfigProperties.Format format) { if (values == null) { return; } for (GetValue getValue : values) { String key = getValue.getKey().replace(this.context, ""); if (this.configProperties.getDataKey().equals(key)) { parseValue(getValue, format); } } }
Example #22
Source File: ConsulDataSource.java From Sentinel with Apache License 2.0 | 5 votes |
@Override public String readSource() throws Exception { if (this.client == null) { throw new IllegalStateException("Consul has not been initialized or error occurred"); } Response<GetValue> response = getValueImmediately(ruleKey); if (response != null) { GetValue value = response.getValue(); lastIndex = response.getConsulIndex(); return value != null ? value.getDecodedValue() : null; } return null; }
Example #23
Source File: ConsulPropertySource.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
/** * Parses the properties using the format which is not a key value style i.e., either * java properties style or YAML style. * * @param values values to parse * @param format format in which the values should be parsed */ protected void parsePropertiesWithNonKeyValueFormat(List<GetValue> values, Format format) { if (values == null) { return; } for (GetValue getValue : values) { String key = getValue.getKey().replace(this.context, ""); if (this.configProperties.getDataKey().equals(key)) { parseValue(getValue, format); } } }
Example #24
Source File: ConfigWatchTests.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
private void setupWatchWithIllegalIndex(ApplicationEventPublisher eventPublisher, String context, Response<List<GetValue>> response) { ConsulClient consul = mock(ConsulClient.class); when(consul.getKVValues(ArgumentMatchers.eq(context), nullable(String.class), ArgumentMatchers.any(QueryParams.class))).thenReturn(response); LinkedHashMap<String, Long> initialIndexes = new LinkedHashMap<>(); initialIndexes.put(context, -1L); startWatch(eventPublisher, consul, initialIndexes); }
Example #25
Source File: ConfigWatchTests.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
private void setupWatch(ApplicationEventPublisher eventPublisher, String context, Response<List<GetValue>> response) { ConsulClient consul = mock(ConsulClient.class); when(consul.getKVValues(ArgumentMatchers.eq(context), nullable(String.class), ArgumentMatchers.any(QueryParams.class))).thenReturn(response); LinkedHashMap<String, Long> initialIndexes = new LinkedHashMap<>(); initialIndexes.put(context, 0L); startWatch(eventPublisher, consul, initialIndexes); }
Example #26
Source File: ConsulPropertySource.java From spring-cloud-consul with Apache License 2.0 | 5 votes |
protected void parseValue(GetValue getValue, ConsulConfigProperties.Format format) { String value = getValue.getDecodedValue(); if (value == null) { return; } Properties props = generateProperties(value, format); for (Map.Entry entry : props.entrySet()) { this.properties.put(entry.getKey().toString(), entry.getValue()); } }
Example #27
Source File: ConfigWatchTests.java From spring-cloud-consul with Apache License 2.0 | 5 votes |
@Test public void watchPublishesEventWithAcl() { ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class); setupWatch(eventPublisher, new GetValue(), "/app/", "2ee647bd-bd69-4118-9f34-b9a6e9e60746"); verify(eventPublisher, atLeastOnce()).publishEvent(any(RefreshEvent.class)); }
Example #28
Source File: ConfigWatchTests.java From spring-cloud-consul with Apache License 2.0 | 5 votes |
@Test public void watchPublishesEvent() { ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class); setupWatch(eventPublisher, new GetValue(), "/app/"); verify(eventPublisher, times(1)).publishEvent(any(RefreshEvent.class)); }
Example #29
Source File: ConfigWatchTests.java From spring-cloud-consul with Apache License 2.0 | 5 votes |
@Test public void watchForFileFormatPublishesEvent() { ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class); this.configProperties.setFormat(FILES); setupWatch(eventPublisher, new GetValue(), "/config/app.yml"); verify(eventPublisher, atLeastOnce()).publishEvent(any(RefreshEvent.class)); }
Example #30
Source File: ConfigWatchTests.java From spring-cloud-consul with Apache License 2.0 | 4 votes |
private void setupWatch(ApplicationEventPublisher eventPublisher, GetValue getValue, String context) { setupWatch(eventPublisher, getValue, context, null); }