mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #1495 from ChunMengLu/master
add NacosRegistrationCustomizer
This commit is contained in:
commit
f9835821af
@ -17,6 +17,7 @@
|
|||||||
package com.alibaba.cloud.nacos.registry;
|
package com.alibaba.cloud.nacos.registry;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
@ -58,12 +59,16 @@ public class NacosRegistration implements Registration, ServiceInstance {
|
|||||||
*/
|
*/
|
||||||
public static final String MANAGEMENT_ENDPOINT_BASE_PATH = "management.endpoints.web.base-path";
|
public static final String MANAGEMENT_ENDPOINT_BASE_PATH = "management.endpoints.web.base-path";
|
||||||
|
|
||||||
|
private List<NacosRegistrationCustomizer> registrationCustomizers;
|
||||||
|
|
||||||
private NacosDiscoveryProperties nacosDiscoveryProperties;
|
private NacosDiscoveryProperties nacosDiscoveryProperties;
|
||||||
|
|
||||||
private ApplicationContext context;
|
private ApplicationContext context;
|
||||||
|
|
||||||
public NacosRegistration(NacosDiscoveryProperties nacosDiscoveryProperties,
|
public NacosRegistration(List<NacosRegistrationCustomizer> registrationCustomizers,
|
||||||
|
NacosDiscoveryProperties nacosDiscoveryProperties,
|
||||||
ApplicationContext context) {
|
ApplicationContext context) {
|
||||||
|
this.registrationCustomizers = registrationCustomizers;
|
||||||
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
|
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
@ -105,6 +110,17 @@ public class NacosRegistration implements Registration, ServiceInstance {
|
|||||||
metadata.put(PreservedMetadataKeys.IP_DELETE_TIMEOUT,
|
metadata.put(PreservedMetadataKeys.IP_DELETE_TIMEOUT,
|
||||||
nacosDiscoveryProperties.getIpDeleteTimeout().toString());
|
nacosDiscoveryProperties.getIpDeleteTimeout().toString());
|
||||||
}
|
}
|
||||||
|
customize(registrationCustomizers, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void customize(
|
||||||
|
List<NacosRegistrationCustomizer> registrationCustomizers,
|
||||||
|
NacosRegistration registration) {
|
||||||
|
if (registrationCustomizers != null) {
|
||||||
|
for (NacosRegistrationCustomizer customizer : registrationCustomizers) {
|
||||||
|
customizer.customize(registration);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.registry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author L.cm
|
||||||
|
*/
|
||||||
|
public interface NacosRegistrationCustomizer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* customize NacosRegistration.
|
||||||
|
* @param registration NacosRegistration
|
||||||
|
*/
|
||||||
|
void customize(NacosRegistration registration);
|
||||||
|
|
||||||
|
}
|
@ -16,10 +16,13 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.nacos.registry;
|
package com.alibaba.cloud.nacos.registry;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
|
import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
|
||||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
@ -54,9 +57,11 @@ public class NacosServiceRegistryAutoConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
|
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
|
||||||
public NacosRegistration nacosRegistration(
|
public NacosRegistration nacosRegistration(
|
||||||
|
ObjectProvider<List<NacosRegistrationCustomizer>> registrationCustomizers,
|
||||||
NacosDiscoveryProperties nacosDiscoveryProperties,
|
NacosDiscoveryProperties nacosDiscoveryProperties,
|
||||||
ApplicationContext context) {
|
ApplicationContext context) {
|
||||||
return new NacosRegistration(nacosDiscoveryProperties, context);
|
return new NacosRegistration(registrationCustomizers.getIfAvailable(),
|
||||||
|
nacosDiscoveryProperties, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.registry;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationHandler;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration;
|
||||||
|
import com.alibaba.nacos.api.NacosFactory;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.powermock.api.mockito.PowerMockito;
|
||||||
|
import org.powermock.api.support.MethodProxy;
|
||||||
|
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
|
import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
|
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author L.cm
|
||||||
|
*/
|
||||||
|
@RunWith(PowerMockRunner.class)
|
||||||
|
@PowerMockIgnore("javax.management.*")
|
||||||
|
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||||
|
@PrepareForTest({ NacosFactory.class })
|
||||||
|
@SpringBootTest(classes = NacosRegistrationCustomizerTest.TestConfig.class,
|
||||||
|
properties = { "spring.application.name=myTestService1",
|
||||||
|
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848" },
|
||||||
|
webEnvironment = RANDOM_PORT)
|
||||||
|
public class NacosRegistrationCustomizerTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
|
||||||
|
Properties.class);
|
||||||
|
MethodProxy.proxy(method, new InvocationHandler() {
|
||||||
|
@Override
|
||||||
|
public Object invoke(Object proxy, Method method, Object[] args)
|
||||||
|
throws Throwable {
|
||||||
|
return new MockNamingService();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() throws Exception {
|
||||||
|
NacosRegistration registration = nacosAutoServiceRegistration.getRegistration();
|
||||||
|
Map<String, String> metadata = registration.getMetadata();
|
||||||
|
Assert.assertEquals("test1", metadata.get("test1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class,
|
||||||
|
NacosDiscoveryClientConfiguration.class,
|
||||||
|
NacosServiceRegistryAutoConfiguration.class })
|
||||||
|
public static class TestConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public NacosRegistrationCustomizer nacosRegistrationCustomizer() {
|
||||||
|
return registration -> {
|
||||||
|
Map<String, String> metadata = registration.getMetadata();
|
||||||
|
metadata.put("test1", "test1");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user