mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
add management info to registry metadata
This commit is contained in:
parent
7fdd8fa3e2
commit
c4043b5c12
@ -20,8 +20,11 @@ import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.discovery.ManagementServerPortUtils;
|
||||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -40,17 +43,23 @@ import static com.alibaba.nacos.api.PropertyKeyConst.*;
|
|||||||
*/
|
*/
|
||||||
public class NacosRegistration implements Registration, ServiceInstance {
|
public class NacosRegistration implements Registration, ServiceInstance {
|
||||||
|
|
||||||
|
private static final String MANAGEMENT_PORT = "management.port";
|
||||||
|
private static final String MANAGEMENT_CONTEXT_PATH = "management.context-path";
|
||||||
|
private static final String MANAGEMENT_ADDRESS = "management.address";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private NacosDiscoveryProperties nacosDiscoveryProperties;
|
private NacosDiscoveryProperties nacosDiscoveryProperties;
|
||||||
|
|
||||||
private NamingService nacosNamingService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment environment;
|
private ApplicationContext context;
|
||||||
|
|
||||||
|
private NamingService nacosNamingService;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
nacosDiscoveryProperties.overrideFromEnv(environment);
|
|
||||||
|
Environment env = context.getEnvironment();
|
||||||
|
nacosDiscoveryProperties.overrideFromEnv(context.getEnvironment());
|
||||||
|
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
properties.put(SERVER_ADDR, nacosDiscoveryProperties.getServerAddr());
|
properties.put(SERVER_ADDR, nacosDiscoveryProperties.getServerAddr());
|
||||||
@ -67,6 +76,21 @@ public class NacosRegistration implements Registration, ServiceInstance {
|
|||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Integer managementPort = ManagementServerPortUtils.getPort(context);
|
||||||
|
if (null != managementPort) {
|
||||||
|
Map<String, String> metadata = nacosDiscoveryProperties.getMetadata();
|
||||||
|
metadata.put(MANAGEMENT_PORT, managementPort.toString());
|
||||||
|
String contextPath = env
|
||||||
|
.getProperty("management.server.servlet.context-path");
|
||||||
|
String address = env.getProperty("management.server.address");
|
||||||
|
if (!StringUtils.isEmpty(contextPath)) {
|
||||||
|
metadata.put(MANAGEMENT_CONTEXT_PATH, contextPath);
|
||||||
|
}
|
||||||
|
if (!StringUtils.isEmpty(address)) {
|
||||||
|
metadata.put(MANAGEMENT_ADDRESS, address);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -85,9 +109,7 @@ public class NacosRegistration implements Registration, ServiceInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setPort(int port) {
|
public void setPort(int port) {
|
||||||
if (nacosDiscoveryProperties.getPort() < 0) {
|
this.nacosDiscoveryProperties.setPort(port);
|
||||||
this.nacosDiscoveryProperties.setPort(port);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user