commit
8ef205f2ca
@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* 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.registry;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||||
|
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.core.ResolvableType;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.springframework.beans.BeanUtils.instantiateClass;
|
||||||
|
import static org.springframework.core.ResolvableType.forInstance;
|
||||||
|
import static org.springframework.core.ResolvableType.forType;
|
||||||
|
import static org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames;
|
||||||
|
import static org.springframework.util.ClassUtils.isPresent;
|
||||||
|
import static org.springframework.util.ClassUtils.resolveClassName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link RegistrationFactory} Provider
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
|
*/
|
||||||
|
public class RegistrationFactoryProvider implements FactoryBean<RegistrationFactory>, ApplicationContextAware {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
private RegistrationFactory registrationFactory;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RegistrationFactory getObject() throws BeansException {
|
||||||
|
return registrationFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getObjectType() {
|
||||||
|
return RegistrationFactory.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSingleton() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
ServiceRegistry<Registration> serviceRegistry = applicationContext.getBean(ServiceRegistry.class);
|
||||||
|
ClassLoader classLoader = applicationContext.getClassLoader();
|
||||||
|
this.registrationFactory = buildRegistrationFactory(serviceRegistry, classLoader);
|
||||||
|
}
|
||||||
|
|
||||||
|
private RegistrationFactory buildRegistrationFactory(ServiceRegistry<Registration> serviceRegistry,
|
||||||
|
ClassLoader classLoader) {
|
||||||
|
RegistrationFactory registrationFactory = null;
|
||||||
|
List<String> factoryClassNames = loadFactoryNames(RegistrationFactory.class, classLoader);
|
||||||
|
|
||||||
|
ResolvableType serviceRegistryType = forInstance(serviceRegistry);
|
||||||
|
// Get first generic Class
|
||||||
|
Class<?> registrationClass = resolveGenericClass(serviceRegistryType, ServiceRegistry.class, 0);
|
||||||
|
|
||||||
|
for (String factoryClassName : factoryClassNames) {
|
||||||
|
if (isPresent(factoryClassName, classLoader)) { // ignore compilation issue
|
||||||
|
Class<?> factoryClass = resolveClassName(factoryClassName, classLoader);
|
||||||
|
ResolvableType registrationFactoryType = forType(factoryClass);
|
||||||
|
Class<?> actualRegistrationClass = resolveGenericClass(registrationFactoryType, RegistrationFactory.class, 0);
|
||||||
|
if (registrationClass.equals(actualRegistrationClass)) {
|
||||||
|
registrationFactory = (RegistrationFactory) instantiateClass(registrationFactoryType.getRawClass());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (registrationFactory == null) {
|
||||||
|
|
||||||
|
if (logger.isWarnEnabled()) {
|
||||||
|
logger.warn("{} implementation can't be resolved by ServiceRegistry[{}]",
|
||||||
|
registrationClass.getSimpleName(), serviceRegistry.getClass().getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
registrationFactory = new DefaultRegistrationFactory();
|
||||||
|
} else {
|
||||||
|
if (logger.isInfoEnabled()) {
|
||||||
|
logger.info("{} has been resolved by ServiceRegistry[{}]",
|
||||||
|
registrationFactory.getClass().getName(), serviceRegistry.getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return registrationFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Class<?> resolveGenericClass(ResolvableType implementedType, Class<?> interfaceClass, int index) {
|
||||||
|
|
||||||
|
ResolvableType resolvableType = implementedType;
|
||||||
|
|
||||||
|
try {
|
||||||
|
OUTER:
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
ResolvableType[] interfaceTypes = resolvableType.getInterfaces();
|
||||||
|
|
||||||
|
for (ResolvableType interfaceType : interfaceTypes) {
|
||||||
|
if (interfaceType.resolve().equals(interfaceClass)) {
|
||||||
|
resolvableType = interfaceType;
|
||||||
|
break OUTER;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ResolvableType superType = resolvableType.getSuperType();
|
||||||
|
|
||||||
|
Class<?> superClass = superType.resolve();
|
||||||
|
|
||||||
|
if (Object.class.equals(superClass)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolvableType = superType;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Throwable e) {
|
||||||
|
resolvableType = ResolvableType.forType(void.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolvableType.resolveGeneric(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,8 +1,12 @@
|
|||||||
dubbo:
|
dubbo:
|
||||||
registry:
|
registry:
|
||||||
# The Spring Cloud Dubbo's registry extension
|
# The Spring Cloud Dubbo's registry extension
|
||||||
address: spring-cloud://localhost
|
address: spring-cloud://localhost
|
||||||
# The traditional Dubbo's registry
|
# The traditional Dubbo's registry
|
||||||
# address: zookeeper://127.0.0.1:2181
|
# address: zookeeper://127.0.0.1:2181
|
||||||
server:
|
server:
|
||||||
port: 7070
|
port: 0
|
||||||
|
|
||||||
|
provider:
|
||||||
|
application:
|
||||||
|
name: spring-cloud-alibaba-dubbo-web-provider
|
@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<version>0.2.2.BUILD-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dubbo-provider-web-sample</artifactId>
|
||||||
|
<name>Spring Cloud Dubbo Provider Web Sample</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Sample API -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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.bootstrap;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dubbo Spring Cloud Provider Bootstrap
|
||||||
|
*/
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
public class DubboSpringCloudWebProviderBootstrap {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new SpringApplicationBuilder(DubboSpringCloudWebProviderBootstrap.class)
|
||||||
|
.properties("spring.profiles.active=nacos")
|
||||||
|
.run(args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,109 @@
|
|||||||
|
/*
|
||||||
|
* 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.service;
|
||||||
|
|
||||||
|
import org.apache.dubbo.config.annotation.Service;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.springframework.cloud.alibaba.dubbo.util.LoggerUtils.log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring MVC {@link RestService}
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
|
*/
|
||||||
|
@Service(version = "1.0.0")
|
||||||
|
@RestController
|
||||||
|
public class SpringRestService implements RestService {
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@GetMapping(value = "/param")
|
||||||
|
public String param(@RequestParam String param) {
|
||||||
|
log("/param", param);
|
||||||
|
return param;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@PostMapping("/params")
|
||||||
|
public String params(@RequestParam int a, @RequestParam String b) {
|
||||||
|
log("/params", a + b);
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@GetMapping("/headers")
|
||||||
|
public String headers(@RequestHeader("h") String header,
|
||||||
|
@RequestHeader("h2") String header2,
|
||||||
|
@RequestParam("v") Integer param) {
|
||||||
|
String result = header + " , " + header2 + " , " + param;
|
||||||
|
log("/headers", result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@GetMapping("/path-variables/{p1}/{p2}")
|
||||||
|
public String pathVariables(@PathVariable("p1") String path1,
|
||||||
|
@PathVariable("p2") String path2,
|
||||||
|
@RequestParam("v") String param) {
|
||||||
|
String result = path1 + " , " + path2 + " , " + param;
|
||||||
|
log("/path-variables", result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@PostMapping("/form")
|
||||||
|
public String form(@RequestParam("f") String form) {
|
||||||
|
return String.valueOf(form);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@PostMapping(value = "/request/body/map", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
public User requestBodyMap(@RequestBody Map<String, Object> data, @RequestParam("param") String param) {
|
||||||
|
User user = new User();
|
||||||
|
user.setId(((Integer) data.get("id")).longValue());
|
||||||
|
user.setName((String) data.get("name"));
|
||||||
|
user.setAge((Integer) data.get("age"));
|
||||||
|
log("/request/body/map", param);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = "/request/body/user", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> requestBodyUser(@RequestBody User user) {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("id", user.getId());
|
||||||
|
map.put("name", user.getName());
|
||||||
|
map.put("age", user.getAge());
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
|||||||
|
dubbo:
|
||||||
|
scan:
|
||||||
|
base-packages: org.springframework.cloud.alibaba.dubbo.service
|
||||||
|
protocols:
|
||||||
|
dubbo:
|
||||||
|
name: dubbo
|
||||||
|
port: -1
|
||||||
|
registry:
|
||||||
|
# The Spring Cloud Dubbo's registry extension
|
||||||
|
address: spring-cloud://localhost
|
||||||
|
# The traditional Dubbo's registry
|
||||||
|
# address: zookeeper://127.0.0.1:2181
|
||||||
|
|
||||||
|
feign:
|
||||||
|
hystrix:
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
server:
|
||||||
|
port: 8080
|
@ -0,0 +1,64 @@
|
|||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: spring-cloud-alibaba-dubbo-web-provider
|
||||||
|
main:
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
|
||||||
|
|
||||||
|
# default disable all
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
enabled: false
|
||||||
|
register-enabled: false
|
||||||
|
zookeeper:
|
||||||
|
enabled: false
|
||||||
|
consul:
|
||||||
|
enabled: false
|
||||||
|
|
||||||
|
eureka:
|
||||||
|
client:
|
||||||
|
enabled: false
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
spring:
|
||||||
|
profiles: nacos
|
||||||
|
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
enabled: true
|
||||||
|
register-enabled: true
|
||||||
|
server-addr: 127.0.0.1:8848
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
spring:
|
||||||
|
profiles: eureka
|
||||||
|
|
||||||
|
eureka:
|
||||||
|
client:
|
||||||
|
enabled: true
|
||||||
|
service-url:
|
||||||
|
defaultZone: http://127.0.0.1:8761/eureka/
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
spring:
|
||||||
|
profiles: zookeeper
|
||||||
|
cloud:
|
||||||
|
zookeeper:
|
||||||
|
enabled: true
|
||||||
|
connect-string: 127.0.0.1:2181
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
spring:
|
||||||
|
profiles: consul
|
||||||
|
|
||||||
|
cloud:
|
||||||
|
consul:
|
||||||
|
enabled: true
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 8500
|
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* 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.util;
|
||||||
|
|
||||||
|
import org.apache.dubbo.rpc.RpcContext;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger Utilities
|
||||||
|
*/
|
||||||
|
public abstract class LoggerUtils {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(LoggerUtils.class);
|
||||||
|
|
||||||
|
public static void log(String url, Object result) {
|
||||||
|
String message = String.format("The client[%s] uses '%s' protocol to call %s : %s",
|
||||||
|
RpcContext.getContext().getRemoteHostName(),
|
||||||
|
RpcContext.getContext().getUrl() == null ? "N/A" : RpcContext.getContext().getUrl().getProtocol(),
|
||||||
|
url,
|
||||||
|
result
|
||||||
|
);
|
||||||
|
if (logger.isInfoEnabled()) {
|
||||||
|
logger.info(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue