Search in sources :

Example 1 with Event

use of com.ecwid.consul.v1.event.model.Event in project spring-cloud-consul by spring-cloud.

the class ConsulInboundMessageProducer method getEvents.

// @Scheduled(fixedDelayString = "${spring.cloud.consul.binder.eventDelay:30000}")
public void getEvents() {
    try {
        List<Event> events = this.eventService.watch();
        for (Event event : events) {
            // Map<String, Object> headers = new HashMap<>();
            // headers.put(MessageHeaders.REPLY_CHANNEL, outputChannel.)
            String decoded = new String(decodeFromString(event.getPayload()));
            sendMessage(getMessageBuilderFactory().withPayload(decoded).build());
        }
    } catch (OperationException e) {
        if (logger.isErrorEnabled()) {
            logger.error("Error getting consul events: " + e);
        }
    } catch (Exception e) {
        if (logger.isErrorEnabled()) {
            logger.error("Error getting consul events: " + e.getMessage());
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Error getting consul events", e);
        }
    }
}
Also used : Event(com.ecwid.consul.v1.event.model.Event) Base64Utils.decodeFromString(org.springframework.util.Base64Utils.decodeFromString) OperationException(com.ecwid.consul.v1.OperationException) OperationException(com.ecwid.consul.v1.OperationException)

Example 2 with Event

use of com.ecwid.consul.v1.event.model.Event in project spring-cloud-consul by spring-cloud.

the class EventService method watch.

public List<Event> watch(Long lastIndex) {
    // TODO: parameterized or configurable watch time
    long index = -1;
    if (lastIndex != null) {
        index = lastIndex;
    }
    int eventTimeout = 5;
    if (this.properties != null) {
        eventTimeout = this.properties.getEventTimeout();
    }
    Response<List<Event>> watch = this.consul.eventList(EventListRequest.newBuilder().setQueryParams(new QueryParams(eventTimeout, index)).build());
    return filterEvents(readEvents(watch), lastIndex);
}
Also used : List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams)

Example 3 with Event

use of com.ecwid.consul.v1.event.model.Event in project spring-cloud-consul by spring-cloud.

the class ConsulConfigServerBootstrapper method initialize.

@Override
public void initialize(BootstrapRegistry registry) {
    if (!ClassUtils.isPresent("org.springframework.cloud.config.client.ConfigServerInstanceProvider", null) || // don't run if bootstrap enabled, how to check the property?
    ClassUtils.isPresent("org.springframework.cloud.bootstrap.marker.Marker", null)) {
        return;
    }
    // create consul client
    registry.registerIfAbsent(ConsulProperties.class, context -> {
        Binder binder = context.get(Binder.class);
        if (!isDiscoveryEnabled(binder)) {
            return null;
        }
        return binder.bind(ConsulProperties.PREFIX, Bindable.of(ConsulProperties.class), getBindHandler(context)).orElseGet(ConsulProperties::new);
    });
    registry.registerIfAbsent(ConsulClient.class, context -> {
        if (!isDiscoveryEnabled(context.get(Binder.class))) {
            return null;
        }
        ConsulProperties consulProperties = context.get(ConsulProperties.class);
        return ConsulAutoConfiguration.createConsulClient(consulProperties);
    });
    registry.registerIfAbsent(ConsulDiscoveryClient.class, context -> {
        Binder binder = context.get(Binder.class);
        if (!isDiscoveryEnabled(binder)) {
            return null;
        }
        ConsulClient consulClient = context.get(ConsulClient.class);
        ConsulDiscoveryProperties properties = binder.bind(ConsulDiscoveryProperties.PREFIX, Bindable.of(ConsulDiscoveryProperties.class), getBindHandler(context)).orElseGet(() -> new ConsulDiscoveryProperties(new InetUtils(new InetUtilsProperties())));
        return new ConsulDiscoveryClient(consulClient, properties);
    });
    // promote discovery client if created
    registry.addCloseListener(event -> {
        if (!isDiscoveryEnabled(event.getBootstrapContext().get(Binder.class))) {
            return;
        }
        ConsulDiscoveryClient discoveryClient = event.getBootstrapContext().get(ConsulDiscoveryClient.class);
        if (discoveryClient != null) {
            event.getApplicationContext().getBeanFactory().registerSingleton("consulDiscoveryClient", discoveryClient);
        }
    });
    registry.registerIfAbsent(ConfigServerInstanceProvider.Function.class, context -> {
        if (!isDiscoveryEnabled(context.get(Binder.class))) {
            return null;
        }
        ConsulDiscoveryClient discoveryClient = context.get(ConsulDiscoveryClient.class);
        return discoveryClient::getInstances;
    });
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) ConsulDiscoveryClient(org.springframework.cloud.consul.discovery.ConsulDiscoveryClient) ConfigServerInstanceProvider(org.springframework.cloud.config.client.ConfigServerInstanceProvider) ConsulClient(com.ecwid.consul.v1.ConsulClient) InetUtilsProperties(org.springframework.cloud.commons.util.InetUtilsProperties) InetUtils(org.springframework.cloud.commons.util.InetUtils) ConsulProperties(org.springframework.cloud.consul.ConsulProperties) ConsulDiscoveryProperties(org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties)

Example 4 with Event

use of com.ecwid.consul.v1.event.model.Event in project spring-cloud-consul by spring-cloud.

the class ConfigWatch method watchConfigKeyValues.

@Timed("consul.watch-config-keys")
public void watchConfigKeyValues() {
    if (!this.running.get()) {
        return;
    }
    for (String context : this.consulIndexes.keySet()) {
        // are FILES)
        if (this.properties.getFormat() != FILES && !context.endsWith("/")) {
            context = context + "/";
        }
        try {
            Long currentIndex = this.consulIndexes.get(context);
            if (currentIndex == null) {
                currentIndex = -1L;
            }
            if (log.isTraceEnabled()) {
                log.trace("watching consul for context '" + context + "' with index " + currentIndex);
            }
            // use the consul ACL token if found
            String aclToken = this.properties.getAclToken();
            if (StringUtils.isEmpty(aclToken)) {
                aclToken = null;
            }
            Response<List<GetValue>> response = this.consul.getKVValues(context, aclToken, new QueryParams(this.properties.getWatch().getWaitTime(), currentIndex));
            // 200, reducing churn if there wasn't anything
            if (response.getValue() != null && !response.getValue().isEmpty()) {
                Long newIndex = response.getConsulIndex();
                if (newIndex != null && !newIndex.equals(currentIndex)) {
                    // time (-1) so index can be primed
                    if (!this.consulIndexes.containsValue(newIndex) && !currentIndex.equals(-1L)) {
                        if (log.isTraceEnabled()) {
                            log.trace("Context " + context + " has new index " + newIndex);
                        }
                        RefreshEventData data = new RefreshEventData(context, currentIndex, newIndex);
                        this.publisher.publishEvent(new RefreshEvent(this, data, data.toString()));
                    } else if (log.isTraceEnabled()) {
                        log.trace("Event for index already published for context " + context);
                    }
                    this.consulIndexes.put(context, newIndex);
                } else if (log.isTraceEnabled()) {
                    log.trace("Same index for context " + context);
                }
            } else if (log.isTraceEnabled()) {
                log.trace("No value for context " + context);
            }
        } catch (Exception e) {
            // only fail fast on the initial query, otherwise just log the error
            if (this.firstTime && this.properties.isFailFast()) {
                log.error("Fail fast is set and there was an error reading configuration from consul.");
                ReflectionUtils.rethrowRuntimeException(e);
            } else if (log.isTraceEnabled()) {
                log.trace("Error querying consul Key/Values for context '" + context + "'", e);
            } else if (log.isWarnEnabled()) {
                // simplified one line log message in the event of an agent
                // failure
                log.warn("Error querying consul Key/Values for context '" + context + "'. Message: " + e.getMessage());
            }
        }
    }
    this.firstTime = false;
}
Also used : RefreshEvent(org.springframework.cloud.endpoint.event.RefreshEvent) List(java.util.List) QueryParams(com.ecwid.consul.v1.QueryParams) Timed(io.micrometer.core.annotation.Timed)

Example 5 with Event

use of com.ecwid.consul.v1.event.model.Event in project spring-cloud-consul by spring-cloud.

the class ConsulSendingHandler method handleMessageInternal.

@Override
protected void handleMessageInternal(Message<?> message) {
    if (this.logger.isTraceEnabled()) {
        this.logger.trace("Publishing message" + message);
    }
    Object payload = message.getPayload();
    if (payload instanceof byte[]) {
        payload = Arrays.toString((byte[]) payload);
    }
    // TODO: support headers
    // TODO: support consul event filters: NodeFilter, ServiceFilter, TagFilter
    Response<Event> event = this.consul.eventFire(this.eventName, (String) payload, new EventParams(), QueryParams.DEFAULT);
// TODO: return event?
}
Also used : EventParams(com.ecwid.consul.v1.event.model.EventParams) Event(com.ecwid.consul.v1.event.model.Event)

Aggregations

Event (com.ecwid.consul.v1.event.model.Event)3 QueryParams (com.ecwid.consul.v1.QueryParams)2 List (java.util.List)2 ConsulClient (com.ecwid.consul.v1.ConsulClient)1 OperationException (com.ecwid.consul.v1.OperationException)1 EventParams (com.ecwid.consul.v1.event.model.EventParams)1 Timed (io.micrometer.core.annotation.Timed)1 Binder (org.springframework.boot.context.properties.bind.Binder)1 InetUtils (org.springframework.cloud.commons.util.InetUtils)1 InetUtilsProperties (org.springframework.cloud.commons.util.InetUtilsProperties)1 ConfigServerInstanceProvider (org.springframework.cloud.config.client.ConfigServerInstanceProvider)1 ConsulProperties (org.springframework.cloud.consul.ConsulProperties)1 ConsulDiscoveryClient (org.springframework.cloud.consul.discovery.ConsulDiscoveryClient)1 ConsulDiscoveryProperties (org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties)1 RefreshEvent (org.springframework.cloud.endpoint.event.RefreshEvent)1 Base64Utils.decodeFromString (org.springframework.util.Base64Utils.decodeFromString)1