org.springframework.cloud.consul.discovery.ConsulDiscoveryClient Java Examples

The following examples show how to use org.springframework.cloud.consul.discovery.ConsulDiscoveryClient. 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: MossServerDiscoveryAutoConfiguration.java    From Moss with Apache License 2.0 6 votes vote down vote up
@Bean
public MultRegisterCenter initMultConsul(MultRegisterCenterService multRegisterCenterService,
                                         MultRegisterCenterServerMgmtConfig mgmtConfig) {
    Map<String, String> URL_MAP;
    MultRegisterCenter multRegisterCenter;
    log.info("start init MultRegisterCenter");
    URL_MAP = multRegisterCenterService.getRegisterCenterList();
    if (URL_MAP.isEmpty()) {
        multRegisterCenter = new MultRegisterCenter(new ConcurrentHashMap<>(), new ConcurrentHashMap<>());
        return multRegisterCenter;
    }
    Map<String, ConsulDiscoveryClient> multConsulMap = Maps.newConcurrentMap();
    Map<ConsulDiscoveryClient, HeartbeatMonitor> multHeartbeatMonitorMap = Maps.newConcurrentMap();


    URL_MAP.entrySet().forEach(e -> {
        log.info("start init consul server:{}", e.getKey() + "-" + e.getValue());
        ConsulDiscoveryClient consulClient = mgmtConfig.consulClient(e.getValue());
        multConsulMap.put(e.getKey(), consulClient);
        multHeartbeatMonitorMap.put(consulClient, new HeartbeatMonitor());
        log.info("init consul server:{} end!", e.getKey() + "-" + e.getValue());
    });
    multRegisterCenter = new MultRegisterCenter(multConsulMap, multHeartbeatMonitorMap);
    log.info("init MultRegisterCenter End!");
    return multRegisterCenter;
}
 
Example #2
Source File: MultRegisterCenterServerMgmtConfig.java    From Moss with Apache License 2.0 6 votes vote down vote up
/**
 * 动态添加一个注册中心
 *
 * @param registerCenterCode
 * @param registerCenterUrl
 */
public void addConsul(String registerCenterCode, String registerCenterUrl) {

    /**
     * 添加ConsulClient,如果有就先删除,再添加
     */
    Map<String, ConsulDiscoveryClient> multConsulMap = multRegisterCenter.getMultConsulMap();
    removeConsulClientByCode(registerCenterCode);
    ConsulDiscoveryClient consulClient = consulClient(registerCenterUrl);
    multConsulMap.put(registerCenterCode, consulClient);
    multRegisterCenter.getMultConsulCodeMap().put(consulClient, registerCenterCode);

    /**
     * 添加 HeartbeatMonitor
     */
    Map<ConsulDiscoveryClient, HeartbeatMonitor> multHeartbeatMonitorMap = multRegisterCenter.getMultHeartbeatMonitorMap();
    multHeartbeatMonitorMap.remove(registerCenterCode);
    multHeartbeatMonitorMap.put(consulClient, new HeartbeatMonitor());
    applicationContext.publishEvent(new RegisterCenterRefreshEvent(consulClient));
}
 
Example #3
Source File: DiscoveryClientConfigServiceAutoConfigurationTests.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
@Test
public void onWhenRequested() throws Exception {
	setup("server.port=0", "spring.cloud.config.discovery.enabled=true",
			"logging.level.org.springframework.cloud.config.client=DEBUG",
			"spring.cloud.consul.discovery.test.enabled:true",
			"spring.application.name=discoveryclientconfigservicetest",
			"spring.jmx.enabled=false", "spring.cloud.consul.discovery.port:7001",
			"spring.cloud.consul.discovery.hostname:foo",
			"spring.cloud.config.discovery.service-id:configserver");

	assertThat(this.context
			.getBeanNamesForType(ConsulConfigServerAutoConfiguration.class).length)
					.isEqualTo(1);
	ConsulDiscoveryClient client = this.context.getParent()
			.getBean(ConsulDiscoveryClient.class);
	verify(client, atLeast(2)).getInstances("configserver");
	ConfigClientProperties locator = this.context
			.getBean(ConfigClientProperties.class);
	assertThat(locator.getUri()[0]).isEqualTo("http://foo:7001/");
}
 
Example #4
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
/**
 * 应用启动完Ready的事件
 *
 * @param event
 */
@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
    Map<String, ConsulDiscoveryClient> map = multRegisterCenter.getMultConsulMap();
    map.entrySet().forEach(e -> {
        consulDiscoveryClient.set(e.getValue());
        discover();
        consulDiscoveryClient.remove();
    });
}
 
Example #5
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
@EventListener
public void registerCenterRefresh(RegisterCenterRefreshEvent event) {
    Map<String, ConsulDiscoveryClient> map = multRegisterCenter.getMultConsulMap();
    map.entrySet().forEach(e -> {
        consulDiscoveryClient.set(e.getValue());
        discover();
        consulDiscoveryClient.remove();
    });
}
 
Example #6
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
@EventListener
public void onInstanceRegistered(InstanceRegisteredEvent<?> event) {
    Map<String, ConsulDiscoveryClient> map = multRegisterCenter.getMultConsulMap();
    map.entrySet().forEach(e -> {
        consulDiscoveryClient.set(e.getValue());
        discover();
        consulDiscoveryClient.remove();
    });
}
 
Example #7
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
@EventListener
public void onParentHeartbeat(ParentHeartbeatEvent event) {
    HeartbeatMonitor heartbeatMonitor = getHeartbeatMonitorByClient((ConsulDiscoveryClient) event.getSource());
    threadLocalmonitor.set(heartbeatMonitor);
    discoverIfNeeded((ConsulDiscoveryClient) event.getSource(), event.getValue());
    threadLocalmonitor.remove();
}
 
Example #8
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
@EventListener
public void onApplicationEvent(HeartbeatEvent event) {
    Map<String, ConsulDiscoveryClient> map = multRegisterCenter.getMultConsulMap();
    map.entrySet().forEach(e -> {
        HeartbeatMonitor heartbeatMonitor = getHeartbeatMonitorByClient(e.getValue());
        threadLocalmonitor.set(heartbeatMonitor);
        discoverIfNeeded(e.getValue(), event.getValue());
        threadLocalmonitor.remove();
    });

}
 
Example #9
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
private void discoverIfNeeded(ConsulDiscoveryClient source, Object value) {
    if (threadLocalmonitor.get().update(value)) {
        consulDiscoveryClient.set(source);
        discover();
        consulDiscoveryClient.remove();
    }
}
 
Example #10
Source File: MultRegisterCenterServerMgmtConfig.java    From Moss with Apache License 2.0 5 votes vote down vote up
public void removeConsulClientByCode(String registerCenterCode) {
    Map<String, ConsulDiscoveryClient> multConsulMap = multRegisterCenter.getMultConsulMap();
    ConsulDiscoveryClient oldConsulClient = multConsulMap.get(registerCenterCode);
    if (null != oldConsulClient) {
        multRegisterCenter.getMultConsulCodeMap().remove(oldConsulClient);
        multRegisterCenter.getMultHeartbeatMonitorMap().remove(oldConsulClient);
        multConsulMap.remove(registerCenterCode);
    }
}
 
Example #11
Source File: TestConsulDiscoveryClientBootstrapConfiguration.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Bean
public ConsulDiscoveryClient consulDiscoveryClient(
		ConsulDiscoveryProperties properties) {
	ConsulDiscoveryClient client = mock(ConsulDiscoveryClient.class);
	ServiceInstance instance = new DefaultServiceInstance("configserver1",
			"configserver", properties.getHostname(), properties.getPort(), false);
	given(client.getInstances("configserver")).willReturn(Arrays.asList(instance));
	return client;
}
 
Example #12
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 4 votes vote down vote up
private String getCodeByClient(ConsulDiscoveryClient client) {
    return multRegisterCenter.getMultConsulCodeMap().get(client);
}
 
Example #13
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 4 votes vote down vote up
private HeartbeatMonitor getHeartbeatMonitorByClient(ConsulDiscoveryClient client) {
    return multRegisterCenter.getMultHeartbeatMonitorMap().get(client);
}
 
Example #14
Source File: MultRegisterCenter.java    From Moss with Apache License 2.0 4 votes vote down vote up
public MultRegisterCenter(Map<String, ConsulDiscoveryClient> multConsulMap, Map<ConsulDiscoveryClient, HeartbeatMonitor> multHeartbeatMonitorMap) {
    this.multConsulMap = multConsulMap;
    this.multHeartbeatMonitorMap = multHeartbeatMonitorMap;
    this.multConsulCodeMap = multConsulMap.entrySet().stream().collect(Collectors.toMap(e -> e.getValue(), e -> e.getKey()));
}
 
Example #15
Source File: MultRegisterCenterServerMgmtConfig.java    From Moss with Apache License 2.0 4 votes vote down vote up
public ConsulDiscoveryClient consulClient(String url) {
    return new ConsulDiscoveryClient(new ConsulClient(url), consulDiscoveryProperties);
}