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

Merge remote-tracking branch 'upstream/master'

# Conflicts:
#	pom.xml
#	spring-cloud-alibaba-dependencies/pom.xml
#	spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml
This commit is contained in:
mercyblitz 2019-01-22 15:16:21 +08:00
parent 59a969049d
commit 744e33229a
8 changed files with 42 additions and 253 deletions

47
pom.xml
View File

@ -119,6 +119,14 @@
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons-dependencies</artifactId>
@ -157,7 +165,6 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.cobertura</groupId>
<artifactId>cobertura-runtime</artifactId>
@ -165,15 +172,6 @@
<scope>provided</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
@ -211,7 +209,14 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<version>${cobertura-maven-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>5.0.3</version>
</dependency>
</dependencies>
<configuration>
<quiet>true</quiet>
<formats>
@ -220,26 +225,6 @@
</formats>
<check/>
<check />
<instrumentation>
<excludes>
<exclude>org/springframework/cloud/alibaba/cloud/dependencies/**.*.class</exclude>
<exclude>org/springframework/cloud/alibaba/cloud/docs/**.*.class</exclude>
<exclude>org/springframework/cloud/alibaba/cloud/examples/**.*.class</exclude>
<exclude>org/springframework/cloud/alibaba/cloud/test/**.*.class</exclude>
<exclude>org/springframework/cloud/start/alibaba/**.*.class</exclude>
<exclude>org/springframework/cloud/start/alicloud/**.*.class</exclude>
<exclude>org/springframework/cloud/alibaba/nacos/src/test/**.*.class</exclude>
<exclude>org/springframework/cloud/alibaba/nacos/config/server/src/test/**.*.class</exclude>
<exclude>org/springframework/cloud/alibaba/sentinel/src/test/**.*.class</exclude>
<exclude>org/springframework/cloud/alibaba/sentinel/datasource/src/test/**.*.class</exclude>
<exclude>org/springframework/cloud/alicloud/acm/src/test/**.*.class</exclude>
<exclude>org/springframework/cloud/alicloud/ans/src/test/**.*.class</exclude>
<exclude>org/springframework/cloud/alicloud/context/src/test/**.*.class</exclude>
<exclude>org/springframework/cloud/alicloud/oss/src/test/**.*.class</exclude>
<exclude>org/springframework/cloud/alicloud/scx/src/test/**.*.class</exclude>
<exclude>org/springframework/cloud/stream/binder/rocketmq/src/test/**.*.class</exclude>
</excludes>
</instrumentation>
</configuration>
<executions>
<execution>

View File

@ -17,14 +17,14 @@
<description>Spring Cloud Alibaba Dependencies</description>
<properties>
<sentinel.version>1.4.0</sentinel.version>
<sentinel.version>1.4.1</sentinel.version>
<oss.version>3.1.0</oss.version>
<nacos.client.version>0.6.2</nacos.client.version>
<nacos.config.version>0.6.1</nacos.config.version>
<acm.version>1.0.8</acm.version>
<ans.version>1.0.1</ans.version>
<aliyun.sdk.version>4.0.1</aliyun.sdk.version>
<alicloud.context.version>1.0.4</alicloud.context.version>
<alicloud.context.version>1.0.5</alicloud.context.version>
<aliyun.sdk.edas.version>2.16.0</aliyun.sdk.edas.version>
<rocketmq.version>4.3.1</rocketmq.version>
<schedulerX.client.version>2.1.6</schedulerX.client.version>

View File

@ -260,8 +260,8 @@ public class NacosConsumerApp {
@GetMapping("/echo/app-name")
public String echoAppName(){
//使用 LoadBalanceClient 和 RestTemolate 结合的方式来访问
ServiceInstance delegate = loadBalancerClient.choose("nacos-provider");
String url = String.format("http://%s:%s/echo/%s",delegate.getHost(),delegate.getPort(),appName);
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
String url = String.format("http://%s:%s/echo/%s",serviceInstance.getHost(),serviceInstance.getPort(),appName);
System.out.println("request url:"+url);
return restTemplate.getForObject(url,String.class);
}

View File

@ -260,8 +260,8 @@ public class NacosConsumerApp {
@GetMapping("/echo/app-name")
public String echoAppName(){
//Access through the combination of LoadBalanceClient and RestTemolate
ServiceInstance delegate = loadBalancerClient.choose("nacos-provider");
String url = String.format("http://%s:%s/echo/%s",delegate.getHost(),delegate.getPort(),appName);
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
String url = String.format("http://%s:%s/echo/%s",serviceInstance.getHost(),serviceInstance.getPort(),appName);
System.out.println("request url:" +url);
return restTemplate.getForObject(url,String.class);
}

View File

@ -1,40 +0,0 @@
/*
* 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.cloud.examples;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Service(version = "1.0.0")
public class EchoController implements EchoService {
@Override
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "hello Nacos Discovery " + string;
}
@Override
@RequestMapping(value = "/divide", method = RequestMethod.GET)
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return String.valueOf(a / b);
}
}

View File

@ -1,33 +0,0 @@
/*
* 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.cloud.examples;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* TODO
*/
public interface EchoService {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
String echo(@PathVariable String string);
@RequestMapping(value = "/divide", method = RequestMethod.GET)
String divide(@RequestParam Integer a, @RequestParam Integer b);
}

View File

@ -1,30 +1,13 @@
package org.springframework.cloud.alibaba.cloud.examples;
import com.alibaba.dubbo.config.spring.ServiceBean;
import com.alibaba.dubbo.config.spring.context.event.ServiceBeanExportedEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.context.event.EventListener;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.springframework.util.ClassUtils.isPrimitiveOrWrapper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author xiaojing
@ -33,118 +16,20 @@ import static org.springframework.util.ClassUtils.isPrimitiveOrWrapper;
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
@Autowired
private ConfigurableListableBeanFactory beanFactory;
@RestController
class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "hello Nacos Discovery " + string;
}
@Autowired
private ObjectMapper objectMapper;
@EventListener(ServiceBeanExportedEvent.class)
public void onServiceBeanExportedEvent(ServiceBeanExportedEvent event) {
ServiceBean serviceBean = event.getServiceBean();
}
@EventListener(InstancePreRegisteredEvent.class)
public void onInstancePreRegisteredEvent(InstancePreRegisteredEvent event) throws JsonProcessingException {
Registration registration = event.getRegistration();
Map<String, ServiceBean> serviceBeansMap = beanFactory.getBeansOfType(ServiceBean.class);
Map<String, String> metaData = registration.getMetadata();
String serviceBeansJson = objectMapper.writeValueAsString(services(serviceBeansMap));
metaData.put("serviceBeans", serviceBeansJson);
}
public Map<String, Map<String, Object>> services(Map<String, ServiceBean> serviceBeansMap) {
Map<String, Map<String, Object>> servicesMetadata = new LinkedHashMap<>(serviceBeansMap.size());
for (Map.Entry<String, ServiceBean> entry : serviceBeansMap.entrySet()) {
String serviceBeanName = entry.getKey();
ServiceBean serviceBean = entry.getValue();
Map<String, Object> serviceBeanMetadata = resolveBeanMetadata(serviceBean);
Object service = resolveServiceBean(serviceBeanName, serviceBean);
if (service != null) {
// Add Service implementation class
serviceBeanMetadata.put("serviceClass", service.getClass().getName());
}
servicesMetadata.put(serviceBeanName, serviceBeanMetadata);
}
return servicesMetadata;
}
protected Map<String, Object> resolveBeanMetadata(final Object bean) {
final Map<String, Object> beanMetadata = new LinkedHashMap<>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Method readMethod = propertyDescriptor.getReadMethod();
if (readMethod != null && isSimpleType(propertyDescriptor.getPropertyType())) {
String name = Introspector.decapitalize(propertyDescriptor.getName());
Object value = readMethod.invoke(bean);
if (value != null) {
beanMetadata.put(name, value);
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return beanMetadata;
}
private Object resolveServiceBean(String serviceBeanName, ServiceBean serviceBean) {
int index = serviceBeanName.indexOf("#");
if (index > -1) {
Class<?> interfaceClass = serviceBean.getInterfaceClass();
String serviceName = serviceBeanName.substring(index + 1);
if (beanFactory.containsBean(serviceName)) {
return beanFactory.getBean(serviceName, interfaceClass);
}
}
return null;
}
private static boolean isSimpleType(Class<?> type) {
return isPrimitiveOrWrapper(type)
|| type == String.class
|| type == BigDecimal.class
|| type == BigInteger.class
|| type == Date.class
|| type == URL.class
|| type == Class.class
;
}
@RequestMapping(value = "/divide", method = RequestMethod.GET)
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return String.valueOf(a / b);
}
}
}

View File

@ -1,12 +1,4 @@
server.port=18082
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
dubbo.scan.base-packages=org.springframework.cloud.alibaba.cloud.examples
dubbo.registry.address=nacos://127.0.0.1:8848
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
management.endpoints.web.exposure.include=*