org.springframework.cloud.client.discovery.event.HeartbeatMonitor Java Examples

The following examples show how to use org.springframework.cloud.client.discovery.event.HeartbeatMonitor. 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: 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 #4
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 #5
Source File: LightminServerStandaloneDiscoveryConfiguration.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@Bean
public LightminApplicationDiscoveryListener lightminApplicationDiscoveryListener(
        final DiscoveryClient discoveryClient,
        final DiscoveryRegistrationBean discoveryRegistrationBean,
        final HeartbeatMonitor heartbeatMonitor) {
    return new LightminApplicationDiscoveryListener(discoveryClient, discoveryRegistrationBean, heartbeatMonitor);
}
 
Example #6
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((CloudEurekaClient) event.getSource());
    threadLocalmonitor.set(heartbeatMonitor);
    discoverIfNeeded((CloudEurekaClient) event.getSource(),event.getValue());
    threadLocalmonitor.remove();
}
 
Example #7
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
@EventListener
public void onApplicationEvent(HeartbeatEvent event) {
    HeartbeatMonitor heartbeatMonitor= getHeartbeatMonitorByClient((CloudEurekaClient) event.getSource());
    threadLocalmonitor.set(heartbeatMonitor);
    discoverIfNeeded((CloudEurekaClient) event.getSource(),event.getValue());
    threadLocalmonitor.remove();
}
 
Example #8
Source File: LightminApplicationDiscoveryListener.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
public LightminApplicationDiscoveryListener(final DiscoveryClient discoveryClient,
                                            final DiscoveryRegistrationBean discoveryRegistrationBean,
                                            final HeartbeatMonitor heartbeatMonitor) {
    this.discoveryClient = discoveryClient;
    this.discoveryRegistrationBean = discoveryRegistrationBean;
    this.heartbeatMonitor = heartbeatMonitor;
}
 
Example #9
Source File: MultRegisterCenter.java    From Moss with Apache License 2.0 5 votes vote down vote up
public MultRegisterCenter(Map<String, EurekaClient> multEurekaMap, Map<String, MossEurekaAutoServiceRegistration> multRegistrationMap,
                          Map<EurekaClient, HeartbeatMonitor> multHeartbeatMonitorMap) {
    this.multEurekaMap = multEurekaMap;
    this.multRegistrationMap = multRegistrationMap;
    this.multEurekaCodeMap = multEurekaMap.entrySet().stream().collect(Collectors.toMap(e -> e.getValue(), e -> e.getKey()));
    this.multHeartbeatMonitorMap=multHeartbeatMonitorMap;
}
 
Example #10
Source File: MultRegisterCenterServerMgmtConfig.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Bean
public MultRegisterCenter initMultNacos() {
    URL_MAP.put("nacos1","127.0.0.1:8848");
    Map<String, NamingService> multEurekaMap = Maps.newConcurrentMap();
    Map<String, MossNacosAutoServiceRegistration> multRegistrationMap = Maps.newConcurrentMap();
    Map<NamingService, HeartbeatMonitor> multHeartbeatMonitorMap=new ConcurrentHashMap<NamingService, HeartbeatMonitor>();
    URL_MAP.entrySet().forEach(e -> {
        NacosDiscoveryProperties nacosDiscoveryProperties=new NacosDiscoveryProperties();
        nacosDiscoveryProperties.setService("halo-moss");
        nacosDiscoveryProperties.setServerAddr(e.getValue());
        try {
            NamingService namingService=NacosFactory.createNamingService(e.getValue());
            com.alibaba.nacos.api.naming.pojo.Instance instance = new com.alibaba.nacos.api.naming.pojo.Instance();
            instance.setIp(inetUtils.findFirstNonLoopbackHostInfo().getIpAddress());
            instance.setPort(-1);
            instance.setWeight(1);
            instance.setClusterName("DEFAULT");
            namingService.registerInstance("halo-moss", instance);
            this.context.publishEvent(
                    new InstanceRegisteredEvent<>(this, namingService));
            multEurekaMap.put(e.getKey(),namingService);
            multHeartbeatMonitorMap.put(namingService,new HeartbeatMonitor());
        } catch (NacosException e1) {
            e1.printStackTrace();
        }
        //NacosServiceRegistry serviceRegistry=new NacosServiceRegistry();
        //AutoServiceRegistrationProperties autoServiceRegistrationProperties=new AutoServiceRegistrationProperties();
        //MossNacosAutoServiceRegistration autoServiceRegistration = new MossNacosAutoServiceRegistration(serviceRegistry,autoServiceRegistrationProperties,registration,registration);
        //autoServiceRegistration.setRegistration(registration);
        //multRegistrationMap.put(e.getKey(), autoServiceRegistration);

    });
    multRegisterCenter = new MultRegisterCenter(multEurekaMap, multRegistrationMap,multHeartbeatMonitorMap);
    return multRegisterCenter;
}
 
Example #11
Source File: MultRegisterCenterServerMgmtConfig.java    From Moss with Apache License 2.0 5 votes vote down vote up
/**
 * 动态添加一个注册中心
 *
 * @param registerCenterCode
 * @param registerCenterUrl
 */
public void addEureka(String registerCenterCode, String registerCenterUrl) {

    ManagementMetadataProvider managementMetadataProvider = serviceManagementMetadataProvider();
    EurekaClientConfigBean configBean = eurekaClientConfigBean(env);
    configBean.getServiceUrl().clear();
    configBean.getServiceUrl().put(EurekaClientConfigBean.DEFAULT_ZONE, registerCenterUrl);
    EurekaInstanceConfigBean instanceConfigBean = eurekaInstanceConfigBean(inetUtils, env, managementMetadataProvider);
    instanceConfigBean.setEnvironment(env);
    instanceConfigBean.setAppname(instanceConfigBean.getAppname());
    ApplicationInfoManager manager = eurekaApplicationInfoManager(instanceConfigBean);


    /**
     * 添加EurekaClient,如果有就先删除,再添加
     */
    Map<String, EurekaClient> multEurekaMap = multRegisterCenter.getMultEurekaMap();
    revomeEurekaClientByCode(registerCenterCode);
    EurekaClient eurekaClient = eurekaClient(manager, configBean);
    multEurekaMap.put(registerCenterCode, eurekaClient);
    multRegisterCenter.getMultEurekaCodeMap().put(eurekaClient, registerCenterCode);

    /**
     * 添加autoServiceRegistration,如果有就先删除,再添加
     */
    Map<String, MossEurekaAutoServiceRegistration> multRegistrationMap = multRegisterCenter.getMultRegistrationMap();
    revomeServiceRegistration(registerCenterCode);
    EurekaRegistration registration = eurekaRegistration(eurekaClient, instanceConfigBean, manager, healthCheckHandler);
    MossEurekaAutoServiceRegistration autoServiceRegistration = eurekaAutoServiceRegistration(context, eurekaServiceRegistry(), registration, registration);
    autoServiceRegistration.start();
    multRegistrationMap.put(registerCenterCode, autoServiceRegistration);

    /**
     * 添加 HeartbeatMonitor
     */
    Map<EurekaClient, HeartbeatMonitor> multHeartbeatMonitorMap = multRegisterCenter.getMultHeartbeatMonitorMap();
    multHeartbeatMonitorMap.remove(registerCenterCode);
    multHeartbeatMonitorMap.put(eurekaClient, new HeartbeatMonitor());

}
 
Example #12
Source File: MultRegisterCenter.java    From Moss with Apache License 2.0 5 votes vote down vote up
public MultRegisterCenter(Map<String, NamingService> multEurekaMap, Map<String, MossNacosAutoServiceRegistration> multRegistrationMap,
                          Map<NamingService, HeartbeatMonitor> multHeartbeatMonitorMap) {
    this.multNacosMap = multEurekaMap;
    this.multRegistrationMap = multRegistrationMap;
    this.multNacosCodeMap = multEurekaMap.entrySet().stream().collect(Collectors.toMap(e -> e.getValue(), e -> e.getKey()));
    this.multHeartbeatMonitorMap=multHeartbeatMonitorMap;
}
 
Example #13
Source File: LightminServerStandaloneDiscoveryConfiguration.java    From spring-batch-lightmin with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(HeartbeatMonitor.class)
public HeartbeatMonitor heartbeatMonitor() {
    return new HeartbeatMonitor();
}
 
Example #14
Source File: MultRegisterCenter.java    From Moss with Apache License 2.0 4 votes vote down vote up
public void setMultHeartbeatMonitorMap(Map<NamingService, HeartbeatMonitor> multHeartbeatMonitorMap) {
    this.multHeartbeatMonitorMap = multHeartbeatMonitorMap;
}
 
Example #15
Source File: MultRegisterCenter.java    From Moss with Apache License 2.0 4 votes vote down vote up
public Map<NamingService, HeartbeatMonitor> getMultHeartbeatMonitorMap() {
    return multHeartbeatMonitorMap;
}
 
Example #16
Source File: MossServerDiscoveryAutoConfiguration.java    From Moss with Apache License 2.0 4 votes vote down vote up
@Bean(destroyMethod = "shutdown")
public MultRegisterCenter initMultEureka(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<String, EurekaClient>(),
                new ConcurrentHashMap<String, MossEurekaAutoServiceRegistration>(),
                new ConcurrentHashMap<EurekaClient, HeartbeatMonitor>());
        return multRegisterCenter;
    }
    Map<String, EurekaClient> multEurekaMap = Maps.newConcurrentMap();
    Map<String, MossEurekaAutoServiceRegistration> multRegistrationMap = Maps.newConcurrentMap();
    Map<EurekaClient, HeartbeatMonitor> multHeartbeatMonitorMap = new ConcurrentHashMap<EurekaClient, HeartbeatMonitor>();
    URL_MAP.entrySet().forEach(e -> {
        log.info("start init eureka server:{}", e.getKey() + "-" + e.getValue());
        ManagementMetadataProvider managementMetadataProvider
                = mgmtConfig.serviceManagementMetadataProvider();
        EurekaClientConfigBean configBean
                = mgmtConfig.eurekaClientConfigBean(env);
        configBean.getServiceUrl().clear();
        configBean.getServiceUrl().put(EurekaClientConfigBean.DEFAULT_ZONE, e.getValue());
        EurekaInstanceConfigBean instanceConfigBean
                = mgmtConfig.eurekaInstanceConfigBean(inetUtils, env, managementMetadataProvider);
        instanceConfigBean.setEnvironment(env);
        ApplicationInfoManager manager
                = mgmtConfig.eurekaApplicationInfoManager(instanceConfigBean);
        EurekaClient eurekaClient
                = mgmtConfig.eurekaClient(manager, configBean);
        EurekaRegistration registration
                = mgmtConfig.eurekaRegistration(eurekaClient, instanceConfigBean, manager, healthCheckHandler);
        MossEurekaAutoServiceRegistration autoServiceRegistration
                = mgmtConfig.eurekaAutoServiceRegistration(context, mgmtConfig.eurekaServiceRegistry(), registration, registration);
        multEurekaMap.put(e.getKey(), eurekaClient);
        multRegistrationMap.put(e.getKey(), autoServiceRegistration);
        multHeartbeatMonitorMap.put(eurekaClient, new HeartbeatMonitor());
        log.info("init eureka server:{} end!", e.getKey() + "-" + e.getValue());
    });
    multRegisterCenter = new MultRegisterCenter(multEurekaMap, multRegistrationMap, multHeartbeatMonitorMap);
    log.info("init MultRegisterCenter End!");
    return multRegisterCenter;
}
 
Example #17
Source File: MultRegisterCenter.java    From Moss with Apache License 2.0 4 votes vote down vote up
public void setMultHeartbeatMonitorMap(Map<EurekaClient, HeartbeatMonitor> multHeartbeatMonitorMap) {
    this.multHeartbeatMonitorMap = multHeartbeatMonitorMap;
}
 
Example #18
Source File: MultRegisterCenter.java    From Moss with Apache License 2.0 4 votes vote down vote up
public Map<EurekaClient, HeartbeatMonitor> getMultHeartbeatMonitorMap() {
    return multHeartbeatMonitorMap;
}
 
Example #19
Source File: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 4 votes vote down vote up
private HeartbeatMonitor getHeartbeatMonitorByClient(CloudEurekaClient client) {
    return multRegisterCenter.getMultHeartbeatMonitorMap().get(client);
}
 
Example #20
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 #21
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);
}