org.springframework.cloud.netflix.eureka.metadata.ManagementMetadataProvider Java Examples

The following examples show how to use org.springframework.cloud.netflix.eureka.metadata.ManagementMetadataProvider. 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: 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 #2
Source File: BrpcServiceRegistrationAutoConfiguration.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(value = EurekaInstanceConfig.class, search = SearchStrategy.CURRENT)
public EurekaInstanceConfigBean eurekaInstanceConfigBean(
        InetUtils inetUtils, ManagementMetadataProvider managementMetadataProvider) {
    EurekaInstanceConfigBean instance = new EurekaClientAutoConfiguration(env)
            .eurekaInstanceConfigBean(inetUtils, managementMetadataProvider);

    String brpcPort = env.getProperty(ENV_PORT_KEY);
    if (StringUtils.isNoneBlank(brpcPort)) {
        instance.getMetadataMap().put(META_DATA_PORT_KEY, brpcPort);
    }
    return instance;
}
 
Example #3
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 #4
Source File: MultRegisterCenterServerMgmtConfig.java    From Moss with Apache License 2.0 4 votes vote down vote up
public ManagementMetadataProvider serviceManagementMetadataProvider() {
    return new DefaultManagementMetadataProvider();
}
 
Example #5
Source File: MultRegisterCenterServerMgmtConfig.java    From Moss with Apache License 2.0 4 votes vote down vote up
/**
 * 构造 EurekaInstanceConfigBean
 *
 * @param inetUtils
 * @param env
 * @param managementMetadataProvider
 * @return
 */
public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils, ConfigurableEnvironment env
        , ManagementMetadataProvider managementMetadataProvider) {
    String hostname = getProperty("eureka.instance.hostname", env);
    boolean preferIpAddress = Boolean.parseBoolean(getProperty("eureka.instance.prefer-ip-address", env));
    String ipAddress = getProperty("eureka.instance.ip-address", env);
    boolean isSecurePortEnabled = Boolean.parseBoolean(getProperty("eureka.instance.secure-port-enabled", env));

    String serverContextPath = env.getProperty("server.context-path", "/");
    int serverPort = Integer.valueOf(env.getProperty("server.port", env.getProperty("port", "8080")));

    Integer managementPort = env.getProperty("management.server.port", Integer.class);// nullable. should be wrapped into optional
    String managementContextPath = env.getProperty("management.server.servlet.context-path");// nullable. should be wrapped into optional
    Integer jmxPort = env.getProperty("com.sun.management.jmxremote.port", Integer.class);//nullable
    EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);

    instance.setNonSecurePort(serverPort);
    instance.setInstanceId(getDefaultInstanceId(env));
    instance.setPreferIpAddress(preferIpAddress);
    instance.setSecurePortEnabled(isSecurePortEnabled);
    if (StringUtils.hasText(ipAddress)) {
        instance.setIpAddress(ipAddress);
    }

    if (isSecurePortEnabled) {
        instance.setSecurePort(serverPort);
    }

    if (StringUtils.hasText(hostname)) {
        instance.setHostname(hostname);
    }
    String statusPageUrlPath = getProperty("eureka.instance.status-page-url-path", env);
    String healthCheckUrlPath = getProperty("eureka.instance.health-check-url-path", env);

    if (StringUtils.hasText(statusPageUrlPath)) {
        instance.setStatusPageUrlPath(statusPageUrlPath);
    }
    if (StringUtils.hasText(healthCheckUrlPath)) {
        instance.setHealthCheckUrlPath(healthCheckUrlPath);
    }

    ManagementMetadata metadata = managementMetadataProvider.get(instance, serverPort,
            serverContextPath, managementContextPath, managementPort);

    if (metadata != null) {
        instance.setStatusPageUrl(metadata.getStatusPageUrl());
        instance.setHealthCheckUrl(metadata.getHealthCheckUrl());
        if (instance.isSecurePortEnabled()) {
            instance.setSecureHealthCheckUrl(metadata.getSecureHealthCheckUrl());
        }
        Map<String, String> metadataMap = instance.getMetadataMap();
        if (metadataMap.get("management.port") == null) {
            metadataMap.put("management.port", String.valueOf(metadata.getManagementPort()));
        }
    } else {
        //without the metadata the status and health check URLs will not be set
        //and the status page and health check url paths will not include the
        //context path so set them here
        if (StringUtils.hasText(managementContextPath)) {
            instance.setHealthCheckUrlPath(managementContextPath + instance.getHealthCheckUrlPath());
            instance.setStatusPageUrlPath(managementContextPath + instance.getStatusPageUrlPath());
        }
    }

    setupJmxPort(instance, jmxPort);
    return instance;
}
 
Example #6
Source File: BrpcServiceRegistrationAutoConfiguration.java    From brpc-java with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ManagementMetadataProvider serviceManagementMetadataProvider() {
    return new DefaultManagementMetadataProvider();
}
 
Example #7
Source File: EurekaClientAutoConfiguration.java    From spring-cloud-netflix with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ManagementMetadataProvider serviceManagementMetadataProvider() {
	return new DefaultManagementMetadataProvider();
}
 
Example #8
Source File: EurekaClientAutoConfiguration.java    From spring-cloud-netflix with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(value = EurekaInstanceConfig.class,
		search = SearchStrategy.CURRENT)
public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils,
		ManagementMetadataProvider managementMetadataProvider) {
	String hostname = getProperty("eureka.instance.hostname");
	boolean preferIpAddress = Boolean
			.parseBoolean(getProperty("eureka.instance.prefer-ip-address"));
	String ipAddress = getProperty("eureka.instance.ip-address");
	boolean isSecurePortEnabled = Boolean
			.parseBoolean(getProperty("eureka.instance.secure-port-enabled"));

	String serverContextPath = env.getProperty("server.servlet.context-path", "/");
	int serverPort = Integer.parseInt(
			env.getProperty("server.port", env.getProperty("port", "8080")));

	Integer managementPort = env.getProperty("management.server.port", Integer.class);
	String managementContextPath = env
			.getProperty("management.server.servlet.context-path");
	Integer jmxPort = env.getProperty("com.sun.management.jmxremote.port",
			Integer.class);
	EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);

	instance.setNonSecurePort(serverPort);
	instance.setInstanceId(getDefaultInstanceId(env));
	instance.setPreferIpAddress(preferIpAddress);
	instance.setSecurePortEnabled(isSecurePortEnabled);
	if (StringUtils.hasText(ipAddress)) {
		instance.setIpAddress(ipAddress);
	}

	if (isSecurePortEnabled) {
		instance.setSecurePort(serverPort);
	}

	if (StringUtils.hasText(hostname)) {
		instance.setHostname(hostname);
	}
	String statusPageUrlPath = getProperty("eureka.instance.status-page-url-path");
	String healthCheckUrlPath = getProperty("eureka.instance.health-check-url-path");

	if (StringUtils.hasText(statusPageUrlPath)) {
		instance.setStatusPageUrlPath(statusPageUrlPath);
	}
	if (StringUtils.hasText(healthCheckUrlPath)) {
		instance.setHealthCheckUrlPath(healthCheckUrlPath);
	}

	ManagementMetadata metadata = managementMetadataProvider.get(instance, serverPort,
			serverContextPath, managementContextPath, managementPort);

	if (metadata != null) {
		instance.setStatusPageUrl(metadata.getStatusPageUrl());
		instance.setHealthCheckUrl(metadata.getHealthCheckUrl());
		if (instance.isSecurePortEnabled()) {
			instance.setSecureHealthCheckUrl(metadata.getSecureHealthCheckUrl());
		}
		Map<String, String> metadataMap = instance.getMetadataMap();
		metadataMap.computeIfAbsent("management.port",
				k -> String.valueOf(metadata.getManagementPort()));
	}
	else {
		// without the metadata the status and health check URLs will not be set
		// and the status page and health check url paths will not include the
		// context path so set them here
		if (StringUtils.hasText(managementContextPath)) {
			instance.setHealthCheckUrlPath(
					managementContextPath + instance.getHealthCheckUrlPath());
			instance.setStatusPageUrlPath(
					managementContextPath + instance.getStatusPageUrlPath());
		}
	}

	setupJmxPort(instance, jmxPort);
	return instance;
}