1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

merge from 2.2.4.RELEASE

This commit is contained in:
theonefx
2021-01-18 17:30:10 +08:00
134 changed files with 1467 additions and 1214 deletions

View File

@@ -3,8 +3,7 @@
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<parent>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-starters</artifactId>
<version>${revision}</version>

View File

@@ -17,18 +17,25 @@
package com.alibaba.cloud.dubbo.autoconfigure;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.cloud.dubbo.autoconfigure.condition.MissingSpringCloudRegistryConfigPropertyCondition;
import com.alibaba.cloud.dubbo.bootstrap.DubboBootstrapStartCommandLineRunner;
import com.alibaba.cloud.dubbo.bootstrap.DubboBootstrapWrapper;
import com.alibaba.cloud.dubbo.bootstrap.event.DubboBootstrapStartedEvent;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.registry.DubboServiceRegistrationEventPublishingAspect;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreDeregisteredEvent;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
import com.ecwid.consul.v1.agent.model.NewService;
import com.netflix.appinfo.InstanceInfo;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.spring.ServiceBean;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -42,7 +49,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry;
@@ -63,9 +72,11 @@ import static org.springframework.util.ObjectUtils.isEmpty;
* Dubbo Service Registration Auto-{@link Configuration}.
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
@Configuration
@Import({ DubboServiceRegistrationEventPublishingAspect.class })
@Import({ DubboServiceRegistrationEventPublishingAspect.class,
DubboBootstrapStartCommandLineRunner.class })
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
@AutoConfigureAfter(name = { EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME,
CONSUL_AUTO_SERVICE_AUTO_CONFIGURATION_CLASS_NAME,
@@ -105,10 +116,42 @@ public class DubboServiceRegistrationAutoConfiguration {
return new RegistryConfig(ADDRESS, PROTOCOL);
}
private Map<ServiceRegistry<Registration>, Set<Registration>> registrations = new ConcurrentHashMap<>();
@EventListener(DubboBootstrapStartedEvent.class)
public void onDubboBootstrapStarted(DubboBootstrapStartedEvent event) {
if (!event.getSource().isReady()) {
return;
}
registrations.forEach(
(registry, registrations) -> registrations.forEach(registration -> {
attachDubboMetadataServiceMetadata(registration);
registry.register(registration);
}));
}
@EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(ServiceInstancePreRegisteredEvent event) {
Registration registration = event.getSource();
attachDubboMetadataServiceMetadata(registration);
if (!DubboBootstrap.getInstance().isReady()
|| !DubboBootstrap.getInstance().isStarted()) {
ServiceRegistry<Registration> registry = event.getRegistry();
synchronized (registry) {
registrations.putIfAbsent(registry, new HashSet<>());
registrations.get(registry).add(registration);
}
}
else {
attachDubboMetadataServiceMetadata(registration);
}
}
@EventListener(ServiceInstancePreDeregisteredEvent.class)
public void onServiceInstancePreDeregistered(
ServiceInstancePreDeregisteredEvent event) {
ServiceRegistry<Registration> registry = event.getRegistry();
registrations.remove(registry);
}
private void attachDubboMetadataServiceMetadata(Registration registration) {
@@ -131,21 +174,67 @@ public class DubboServiceRegistrationAutoConfiguration {
@Configuration
@ConditionalOnBean(name = EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME)
@Aspect
class EurekaConfiguration implements SmartInitializingSingleton {
@Autowired
private ObjectProvider<Collection<ServiceBean>> serviceBeans;
@EventListener(DubboBootstrapStartedEvent.class)
public void onDubboBootstrapStarted(DubboBootstrapStartedEvent event) {
DubboBootstrapWrapper wrapper = event.getSource();
if (!wrapper.isReady()) {
return;
}
registrations.forEach(
(registry, registrations) -> registrations.removeIf(registration -> {
if (!(registration instanceof EurekaRegistration)) {
return false;
}
EurekaRegistration eurekaRegistration = (EurekaRegistration) registration;
InstanceInfo instanceInfo = eurekaRegistration
.getApplicationInfoManager().getInfo();
EurekaInstanceConfigBean config = (EurekaInstanceConfigBean) eurekaRegistration
.getInstanceConfig();
config.setInitialStatus(InstanceInfo.InstanceStatus.UP);
attachDubboMetadataServiceMetadata(instanceInfo.getMetadata());
eurekaRegistration.getApplicationInfoManager()
.registerAppMetadata(instanceInfo.getMetadata());
eurekaRegistration.getApplicationInfoManager()
.setInstanceStatus(InstanceInfo.InstanceStatus.UP);
return true;
}));
}
@EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(
ServiceInstancePreRegisteredEvent event) {
Registration registration = event.getSource();
EurekaRegistration eurekaRegistration = EurekaRegistration.class
.cast(registration);
InstanceInfo instanceInfo = eurekaRegistration.getApplicationInfoManager()
.getInfo();
attachDubboMetadataServiceMetadata(instanceInfo.getMetadata());
if (!(registration instanceof EurekaRegistration)) {
return;
}
if (DubboBootstrap.getInstance().isReady()
&& DubboBootstrap.getInstance().isStarted()) {
EurekaRegistration eurekaRegistration = (EurekaRegistration) registration;
InstanceInfo instanceInfo = eurekaRegistration.getApplicationInfoManager()
.getInfo();
EurekaInstanceConfigBean config = (EurekaInstanceConfigBean) eurekaRegistration
.getInstanceConfig();
config.setInitialStatus(InstanceInfo.InstanceStatus.UP);
attachDubboMetadataServiceMetadata(instanceInfo.getMetadata());
eurekaRegistration.getApplicationInfoManager()
.registerAppMetadata(instanceInfo.getMetadata());
}
else {
EurekaRegistration eurekaRegistration = (EurekaRegistration) registration;
EurekaInstanceConfigBean config = (EurekaInstanceConfigBean) eurekaRegistration
.getInstanceConfig();
config.setInitialStatus(InstanceInfo.InstanceStatus.STARTING);
}
}
/**
@@ -170,21 +259,28 @@ public class DubboServiceRegistrationAutoConfiguration {
@AutoConfigureOrder
class ConsulConfiguration {
/**
* Handle the pre-registered event of {@link ServiceInstance} for Consul.
* @param event {@link ServiceInstancePreRegisteredEvent}
*/
@EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(
ServiceInstancePreRegisteredEvent event) {
Registration registration = event.getSource();
Class<?> registrationClass = AopUtils.getTargetClass(registration);
String registrationClassName = registrationClass.getName();
if (CONSUL_AUTO_SERVICE_AUTO_REGISTRATION_CLASS_NAME
.equalsIgnoreCase(registrationClassName)) {
ConsulRegistration consulRegistration = (ConsulRegistration) registration;
attachURLsIntoMetadata(consulRegistration);
@EventListener(DubboBootstrapStartedEvent.class)
public void attachURLsIntoMetadataBeforeReRegist(
DubboBootstrapStartedEvent event) {
if (!event.getSource().isReady()) {
return;
}
registrations.entrySet().removeIf(entry -> {
Set<Registration> registrations = entry.getValue();
registrations.removeIf(registration -> {
Class<?> registrationClass = AopUtils.getTargetClass(registration);
String registrationClassName = registrationClass.getName();
return !CONSUL_AUTO_SERVICE_AUTO_REGISTRATION_CLASS_NAME
.equalsIgnoreCase(registrationClassName);
});
return registrations.isEmpty();
});
registrations.forEach(
(registry, registrations) -> registrations.forEach(registration -> {
ConsulRegistration consulRegistration = (ConsulRegistration) registration;
attachURLsIntoMetadata(consulRegistration);
}));
}
private void attachURLsIntoMetadata(ConsulRegistration consulRegistration) {

View File

@@ -103,7 +103,7 @@ public class DubboServiceRegistrationNonWebApplicationAutoConfiguration {
*/
private void setServerPort() {
if (serverPort == null) {
synchronized (this) {
synchronized (DubboServiceRegistrationNonWebApplicationAutoConfiguration.class) {
if (serverPort == null) {
for (List<URL> urls : repository.getAllExportedUrls().values()) {
urls.stream().filter(

View File

@@ -0,0 +1,50 @@
/*
* 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.dubbo.bootstrap;
import com.alibaba.cloud.dubbo.bootstrap.event.DubboBootstrapStartedEvent;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
/**
* publish Dubbo microsystem startup finish event.
*
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
@Component
public class DubboBootstrapStartCommandLineRunner
implements CommandLineRunner, ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
@Override
public void setApplicationEventPublisher(
ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
@Override
public void run(String... args) {
applicationEventPublisher.publishEvent(
new DubboBootstrapStartedEvent(DubboBootstrapWrapper.getInstance()));
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.dubbo.bootstrap;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
/**
* Wrapper DubboBootstrap operation.
*
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
public final class DubboBootstrapWrapper {
private DubboBootstrapWrapper() {
}
private static final DubboBootstrapWrapper INSTANCE = new DubboBootstrapWrapper();
public static DubboBootstrapWrapper getInstance() {
return INSTANCE;
}
public boolean isReady() {
return DubboBootstrap.getInstance().isStarted()
&& DubboBootstrap.getInstance().isReady()
&& DubboBootstrap.getInstance().isInitialized();
}
public DubboBootstrap getDubboBootstrap() {
return DubboBootstrap.getInstance();
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.dubbo.bootstrap.event;
import com.alibaba.cloud.dubbo.bootstrap.DubboBootstrapWrapper;
import org.springframework.context.ApplicationEvent;
/**
* Dubbo microsytem start finish event, every thing is ready.
*
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
public class DubboBootstrapStartedEvent extends ApplicationEvent {
/**
* Create a new {@code DubboBootstrapStartedEvent}.
* @param source the object on which the event initially occurred or with which the
* event is associated (never {@code null})
*/
public DubboBootstrapStartedEvent(DubboBootstrapWrapper source) {
super(source);
}
@Override
public DubboBootstrapWrapper getSource() {
return (DubboBootstrapWrapper) super.getSource();
}
}

View File

@@ -40,6 +40,7 @@ public abstract class AbstractHttpRequestMatcher implements HttpRequestMatcher {
* <p>
* For example {@code " || "} for URL patterns or {@code " && "} for param
* expressions.
* @return str
*/
protected abstract String getToStringInfix();

View File

@@ -77,7 +77,7 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
/**
* Caches the IDs of {@link ApplicationListener}.
*/
private static final Set<String> registerListeners = new HashSet<>();
private static final Set<String> REGISTER_LISTENERS = new HashSet<>();
protected final Logger logger = LoggerFactory.getLogger(getClass());
@@ -193,7 +193,7 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
private void registerServiceInstancesChangedEventListener(URL url,
NotifyListener listener) {
String listenerId = generateId(url);
if (registerListeners.add(listenerId)) {
if (REGISTER_LISTENERS.add(listenerId)) {
applicationContext.addApplicationListener(
new ApplicationListener<ServiceInstancesChangedEvent>() {
@Override
@@ -261,7 +261,7 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
String listenerId = generateId(url);
// The metaservice will restart the new listener. It needs to be optimized
// to see whether the original listener can be reused.
this.registerListeners.remove(listenerId);
REGISTER_LISTENERS.remove(listenerId);
}
/**

View File

@@ -83,7 +83,7 @@ public class DubboCloudRegistry extends FailbackRegistry {
/**
* Caches the IDs of {@link ApplicationListener}.
*/
private static final Set<String> registerListeners = new HashSet<>();
private static final Set<String> REGISTER_LISTENERS = new HashSet<>();
protected final Logger logger = LoggerFactory.getLogger(getClass());
@@ -215,7 +215,7 @@ public class DubboCloudRegistry extends FailbackRegistry {
private void registerServiceInstancesChangedListener(URL url,
ApplicationListener<ServiceInstancesChangedEvent> listener) {
String listenerId = generateId(url);
if (registerListeners.add(listenerId)) {
if (REGISTER_LISTENERS.add(listenerId)) {
applicationContext.addApplicationListener(listener);
}
}
@@ -290,7 +290,7 @@ public class DubboCloudRegistry extends FailbackRegistry {
.map(templateURL -> templateURL.removeParameter(PID_KEY))
.map(templateURL -> {
String protocol = templateURL.getProtocol();
int port = repository.getDubboProtocolPort(serviceInstance,
Integer port = repository.getDubboProtocolPort(serviceInstance,
protocol);
if (Objects.equals(templateURL.getHost(), host)
&& Objects.equals(templateURL.getPort(), port)) { // use
@@ -300,15 +300,27 @@ public class DubboCloudRegistry extends FailbackRegistry {
return templateURL;
}
URLBuilder clonedURLBuilder = from(templateURL) // remove the
// parameters from
// the template
// URL
.setHost(host) // reset the host
.setPort(port); // reset the port
if (port == null) {
if (logger.isWarnEnabled()) {
logger.warn(
"The protocol[{}] port of Dubbo service instance[host : {}] "
+ "can't be resolved",
protocol, host);
}
return null;
}
else {
URLBuilder clonedURLBuilder = from(templateURL) // remove the
// parameters from
// the template
// URL
.setHost(host) // reset the host
.setPort(port); // reset the port
return clonedURLBuilder.build();
}).forEach(clonedExportedURLs::add);
return clonedURLBuilder.build();
}
}).filter(Objects::nonNull).forEach(clonedExportedURLs::add);
});
return clonedExportedURLs;
}

View File

@@ -16,6 +16,7 @@
package com.alibaba.cloud.dubbo.registry;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreDeregisteredEvent;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstanceRegisteredEvent;
import org.aspectj.lang.annotation.After;
@@ -33,6 +34,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @see ServiceInstancePreRegisteredEvent
* @see ServiceInstanceRegisteredEvent
* @see ServiceInstancePreDeregisteredEvent
*/
@Aspect
public class DubboServiceRegistrationEventPublishingAspect
@@ -41,18 +43,29 @@ public class DubboServiceRegistrationEventPublishingAspect
/**
* The pointcut expression for {@link ServiceRegistry#register(Registration)}.
*/
public static final String REGISTER_POINTCUT_EXPRESSION = "execution(* org.springframework.cloud.client.serviceregistry.ServiceRegistry.register(*)) && args(registration)";
public static final String REGISTER_POINTCUT_EXPRESSION = "execution(* org.springframework.cloud.client.serviceregistry.ServiceRegistry.register(*)) && target(registry) && args(registration)";
/**
* The pointcut expression for {@link ServiceRegistry#deregister(Registration)}.
*/
public static final String DEREGISTER_POINTCUT_EXPRESSION = "execution(* org.springframework.cloud.client.serviceregistry.ServiceRegistry.deregister(*)) && target(registry) && args(registration)";
private ApplicationEventPublisher applicationEventPublisher;
@Before(REGISTER_POINTCUT_EXPRESSION)
public void beforeRegister(Registration registration) {
applicationEventPublisher
.publishEvent(new ServiceInstancePreRegisteredEvent(registration));
@Before(value = REGISTER_POINTCUT_EXPRESSION, argNames = "registry, registration")
public void beforeRegister(ServiceRegistry registry, Registration registration) {
applicationEventPublisher.publishEvent(
new ServiceInstancePreRegisteredEvent(registry, registration));
}
@After(REGISTER_POINTCUT_EXPRESSION)
public void afterRegister(Registration registration) {
@Before(value = DEREGISTER_POINTCUT_EXPRESSION, argNames = "registry, registration")
public void beforeDeregister(ServiceRegistry registry, Registration registration) {
applicationEventPublisher.publishEvent(
new ServiceInstancePreDeregisteredEvent(registry, registration));
}
@After(value = REGISTER_POINTCUT_EXPRESSION, argNames = "registry, registration")
public void afterRegister(ServiceRegistry registry, Registration registration) {
applicationEventPublisher
.publishEvent(new ServiceInstanceRegisteredEvent(registration));
}

View File

@@ -0,0 +1,49 @@
/*
* 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.dubbo.registry.event;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationEvent;
/**
* The
* before-{@link org.springframework.cloud.client.serviceregistry.ServiceRegistry#register(org.springframework.cloud.client.serviceregistry.Registration)
* register} event for {@link org.springframework.cloud.client.ServiceInstance}.
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class ServiceInstancePreDeregisteredEvent extends ApplicationEvent {
private final ServiceRegistry<Registration> registry;
public ServiceInstancePreDeregisteredEvent(ServiceRegistry<Registration> registry,
Registration source) {
super(source);
this.registry = registry;
}
@Override
public Registration getSource() {
return (Registration) super.getSource();
}
public ServiceRegistry<Registration> getRegistry() {
return registry;
}
}

View File

@@ -29,8 +29,12 @@ import org.springframework.context.ApplicationEvent;
*/
public class ServiceInstancePreRegisteredEvent extends ApplicationEvent {
public ServiceInstancePreRegisteredEvent(Registration source) {
private final ServiceRegistry<Registration> registry;
public ServiceInstancePreRegisteredEvent(ServiceRegistry<Registration> registry,
Registration source) {
super(source);
this.registry = registry;
}
@Override
@@ -38,4 +42,8 @@ public class ServiceInstancePreRegisteredEvent extends ApplicationEvent {
return (Registration) super.getSource();
}
public ServiceRegistry<Registration> getRegistry() {
return registry;
}
}