pull/514/head
mercyblitz 6 years ago
parent ba569af8bc
commit 8218c9d38b

@ -14,7 +14,7 @@
<name>Spring Cloud Alibaba Dubbo</name>
<properties>
<dubbo.version>2.7.0</dubbo.version>
<dubbo.version>2.7.1</dubbo.version>
<spring-cloud-zookeeper.version>2.1.0.RELEASE</spring-cloud-zookeeper.version>
<spring-cloud-consul.version>2.1.0.RELEASE</spring-cloud-consul.version>
<curator.version>4.0.1</curator.version>

@ -73,7 +73,7 @@ public class DubboMetadataAutoConfiguration {
}
@Bean
public Supplier<ProtocolConfig> dubboProtocolConfigSupplier(Collection<ProtocolConfig> protocols) {
public Supplier<ProtocolConfig> dubboProtocolConfigSupplier(ObjectProvider<Collection<ProtocolConfig>> protocols) {
return new DubboProtocolConfigSupplier(protocols);
}

@ -17,16 +17,20 @@
package org.springframework.cloud.alibaba.dubbo.autoconfigure;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.spring.context.event.ServiceBeanExportedEvent;
import com.ecwid.consul.v1.agent.model.NewService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.cloud.alibaba.dubbo.metadata.repository.DubboServiceMetadataRepository;
import org.springframework.cloud.alibaba.dubbo.registry.DubboServiceRegistrationEventPublishingAspect;
import org.springframework.cloud.alibaba.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
@ -35,7 +39,6 @@ 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.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.EventListener;
@ -49,6 +52,7 @@ import java.util.stream.Collectors;
import static org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.CONSUL_AUTO_CONFIGURATION_CLASS_NAME;
import static org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.EUREKA_AUTO_CONFIGURATION_CLASS_NAME;
import static org.springframework.cloud.alibaba.dubbo.registry.DubboServiceRegistrationEventPublishingAspect.REGISTER_POINTCUT_EXPRESSION;
import static org.springframework.cloud.alibaba.dubbo.registry.SpringCloudRegistry.DUBBO_URLS_METADATA_PROPERTY_NAME;
/**
@ -74,7 +78,7 @@ public class DubboServiceRegistrationAutoConfiguration {
public static final String CONSUL_AUTO_CONFIGURATION_CLASS_NAME =
"org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistrationAutoConfiguration";
private static final String CONSUL_AUTO_REGISTRATION_CLASS_NAME =
public static final String CONSUL_AUTO_REGISTRATION_CLASS_NAME =
"org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration";
private static final Logger logger = LoggerFactory.getLogger(DubboServiceRegistrationAutoConfiguration.class);
@ -95,32 +99,29 @@ public class DubboServiceRegistrationAutoConfiguration {
@Configuration
@ConditionalOnBean(name = EUREKA_AUTO_CONFIGURATION_CLASS_NAME)
@AutoConfigureOrder
static class EurekaConfiguration {
@Autowired
private ApplicationContext applicationContext;
@Aspect
class EurekaConfiguration {
@Autowired
private ServiceRegistry serviceRegistry;
private volatile Registration registration;
private Registration registration;
private boolean deferredRegistration = true;
@EventListener(ServiceBeanExportedEvent.class)
public void onServiceBeanExported() {
reRegister();
}
private void reRegister() {
if (registration == null) {
return;
@Around(REGISTER_POINTCUT_EXPRESSION)
public Object doRegister(ProceedingJoinPoint pjp, Registration registration) throws Throwable {
this.registration = registration;
if (deferredRegistration) {
return null;
}
serviceRegistry.register(registration);
return pjp.proceed(pjp.getArgs());
}
@EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(ServiceInstancePreRegisteredEvent event) {
registration = event.getSource();
@EventListener(ApplicationStartedEvent.class)
public void onApplicationStarted() {
deferredRegistration = false;
serviceRegistry.register(registration);
}
}
@ -138,9 +139,16 @@ public class DubboServiceRegistrationAutoConfiguration {
@EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(ServiceInstancePreRegisteredEvent event) {
Registration registration = event.getSource();
String registrationClassName = registration.getClass().getName();
Class<?> registrationClass = AopUtils.getTargetClass(registration);
String registrationClassName = registrationClass.getName();
if (CONSUL_AUTO_REGISTRATION_CLASS_NAME.equalsIgnoreCase(registrationClassName)) {
NewService newService = ((ConsulRegistration) registration).getService();
ConsulRegistration consulRegistration = (ConsulRegistration) registration;
attachURLsIntoMetadata(consulRegistration);
}
}
private void attachURLsIntoMetadata(ConsulRegistration consulRegistration) {
NewService newService = consulRegistration.getService();
String dubboURLsJson = getDubboURLsJSON();
if (StringUtils.hasText(dubboURLsJson)) {
List<String> tags = newService.getTags();
@ -149,8 +157,6 @@ public class DubboServiceRegistrationAutoConfiguration {
}
}
}
private void attachURLsIntoMetadata(Registration registration) {
if (registration == null) {
return;

@ -0,0 +1,156 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.springframework.cloud.alibaba.dubbo.autoconfigure;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.dubbo.config.spring.context.event.ServiceBeanExportedEvent;
import com.ecwid.consul.v1.agent.model.NewService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.alibaba.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
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.ConsulAutoRegistration;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.cloud.netflix.eureka.CloudEurekaInstanceConfig;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import java.util.List;
import static org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.CONSUL_AUTO_CONFIGURATION_CLASS_NAME;
import static org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.EUREKA_AUTO_CONFIGURATION_CLASS_NAME;
/**
* Dubbo Service Registration Auto-{@link Configuration} for Non-Web application
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
@Configuration
@ConditionalOnNotWebApplication
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
@AutoConfigureAfter(DubboServiceRegistrationAutoConfiguration.class)
@Aspect
public class DubboServiceRegistrationNonWebApplicationAutoConfiguration {
@Autowired
private ServiceRegistry serviceRegistry;
@Autowired
private Registration registration;
private volatile Integer webPort = null;
private volatile boolean registered = false;
@Around("execution(* org.springframework.cloud.client.serviceregistry.Registration.getPort())")
public Object getPort(ProceedingJoinPoint pjp) throws Throwable {
return webPort != null ? webPort : pjp.proceed();
}
@EventListener(ServiceBeanExportedEvent.class)
public void onServiceBeanExported(ServiceBeanExportedEvent event) {
setWebPort(event.getServiceBean());
register();
}
private void register() {
if (registered) {
return;
}
serviceRegistry.register(registration);
registered = true;
}
/**
* Set web port from {@link ServiceBean#getExportedUrls() exported URLs} if "rest" protocol is present.
*
* @param serviceBean {@link ServiceBean}
*/
private void setWebPort(ServiceBean serviceBean) {
if (webPort == null) {
List<URL> urls = serviceBean.getExportedUrls();
urls.stream()
.filter(url -> "rest".equalsIgnoreCase(url.getProtocol()))
.findFirst()
.ifPresent(url -> {
webPort = url.getPort();
});
}
}
@Configuration
@ConditionalOnBean(name = EUREKA_AUTO_CONFIGURATION_CLASS_NAME)
@AutoConfigureOrder
static class EurekaConfiguration {
@EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(ServiceInstancePreRegisteredEvent event) {
setPort(event.getSource());
}
private void setPort(Registration registration) {
EurekaRegistration eurekaRegistration = (EurekaRegistration) registration;
CloudEurekaInstanceConfig cloudEurekaInstanceConfig = eurekaRegistration.getInstanceConfig();
cloudEurekaInstanceConfig.setNonSecurePort(registration.getPort());
}
}
@Configuration
@ConditionalOnBean(name = CONSUL_AUTO_CONFIGURATION_CLASS_NAME)
@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();
ConsulAutoRegistration consulRegistration = (ConsulAutoRegistration) registration;
setPort(consulRegistration);
}
/**
* Set port on Non-Web Application
*
* @param consulRegistration {@link ConsulRegistration}
*/
private void setPort(ConsulAutoRegistration consulRegistration) {
int port = consulRegistration.getPort();
NewService newService = consulRegistration.getService();
if (newService.getPort() == null) {
newService.setPort(port);
}
}
}
}

@ -18,6 +18,8 @@ package org.springframework.cloud.alibaba.dubbo.metadata;
import org.apache.dubbo.config.ProtocolConfig;
import org.springframework.beans.factory.ObjectProvider;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Supplier;
@ -31,15 +33,16 @@ import static org.apache.dubbo.common.Constants.DEFAULT_PROTOCOL;
*/
public class DubboProtocolConfigSupplier implements Supplier<ProtocolConfig> {
private final Collection<ProtocolConfig> protocols;
private final ObjectProvider<Collection<ProtocolConfig>> protocols;
public DubboProtocolConfigSupplier(Collection<ProtocolConfig> protocols) {
public DubboProtocolConfigSupplier(ObjectProvider<Collection<ProtocolConfig>> protocols) {
this.protocols = protocols;
}
@Override
public ProtocolConfig get() {
ProtocolConfig protocolConfig = null;
Collection<ProtocolConfig> protocols = this.protocols.getIfAvailable();
for (ProtocolConfig protocol : protocols) {
String protocolName = protocol.getName();
if (DEFAULT_PROTOCOL.equals(protocolName)) {

@ -39,7 +39,7 @@ public class DubboServiceRegistrationEventPublishingAspect implements Applicatio
/**
* The pointcut expression for {@link ServiceRegistry#register(Registration)}
*/
private static final String REGISTER_POINTCUT_EXPRESSION =
public static final String REGISTER_POINTCUT_EXPRESSION =
"execution(* org.springframework.cloud.client.serviceregistry.ServiceRegistry.register(*)) && args(registration)";
private ApplicationEventPublisher applicationEventPublisher;

@ -2,6 +2,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboMetadataAutoConfiguration,\
org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboOpenFeignAutoConfiguration,\
org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration,\
org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboServiceRegistrationNonWebApplicationAutoConfiguration,\
org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboLoadBalancedRestTemplateAutoConfiguration,\
org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboServiceAutoConfiguration

@ -17,6 +17,7 @@
package org.springframework.cloud.alibaba.dubbo.bootstrap;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
@ -52,7 +53,7 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
@EnableFeignClients
public class DubboSpringCloudConsumerBootstrap {
@Reference(version = "1.0.0")
@Reference(version = "1.0.0", protocol = "dubbo")
private RestService restService;
@Autowired
@ -94,7 +95,7 @@ public class DubboSpringCloudConsumerBootstrap {
}
@FeignClient("${provider.application.name}")
@DubboTransported()
@DubboTransported(protocol = "dubbo")
public interface DubboFeignRestService {
@GetMapping(value = "/param")

@ -9,4 +9,4 @@ server:
provider:
application:
name: spring-cloud-alibaba-dubbo-web-provider
name: spring-cloud-alibaba-dubbo-provider

@ -17,6 +17,13 @@
<dependencies>
<!-- Resolve the Dubbo REST RPC issue -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- Resolve the Spring Cloud registration issue -->
<dependency>
<groupId>org.springframework</groupId>

@ -16,6 +16,7 @@
*/
package org.springframework.cloud.alibaba.dubbo.bootstrap;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@ -30,6 +31,7 @@ public class DubboSpringCloudProviderBootstrap {
public static void main(String[] args) {
new SpringApplicationBuilder(DubboSpringCloudProviderBootstrap.class)
.properties("spring.profiles.active=nacos")
.web(WebApplicationType.NONE)
.run(args);
}
}

Loading…
Cancel
Save