Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,14 @@
package org.apache.shenyu.client.apache.dubbo;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.MethodConfig;
import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.shenyu.client.core.client.AbstractContextRefreshedEventListener;
import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.apache.shenyu.client.dubbo.common.dto.DubboRpcExt;
import org.apache.shenyu.client.dubbo.common.dto.DubboRpcMethodExt;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.client.dubbo.common.dto.DubboRpcExtBuilders;
import org.apache.shenyu.common.enums.ApiHttpMethodEnum;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.register.client.api.ShenyuClientRegisterRepository;
import org.apache.shenyu.register.common.config.ShenyuClientConfig;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
Expand All @@ -46,16 +41,13 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.apache.dubbo.remoting.Constants.DEFAULT_CONNECT_TIMEOUT;

/**
* The Apache Dubbo ServiceBean Listener.
*/
Expand Down Expand Up @@ -223,31 +215,6 @@ public String getPort() {
}

private String buildRpcExt(final ServiceBean<?> serviceBean, final String methodName) {
DubboRpcExt build = DubboRpcExt.builder()
.protocol(StringUtils.isNotEmpty(serviceBean.getProtocol().getName()) ? serviceBean.getProtocol().getName() : "")
.group(StringUtils.isNotEmpty(serviceBean.getGroup()) ? serviceBean.getGroup() : "")
.version(StringUtils.isNotEmpty(serviceBean.getVersion()) ? serviceBean.getVersion() : "")
.loadbalance(StringUtils.isNotEmpty(serviceBean.getLoadbalance()) ? serviceBean.getLoadbalance() : CommonConstants.DEFAULT_LOADBALANCE)
.retries(Optional.ofNullable(serviceBean.getRetries()).orElse(CommonConstants.DEFAULT_RETRIES))
.timeout(Optional.ofNullable(serviceBean.getTimeout()).orElse(DEFAULT_CONNECT_TIMEOUT))
.sent(Optional.ofNullable(serviceBean.getSent()).orElse(Boolean.FALSE))
.cluster(StringUtils.isNotEmpty(serviceBean.getCluster()) ? serviceBean.getCluster() : Constants.DEFAULT_CLUSTER)
.url("")
.serialization(serviceBean.getSerialization())
.build();
// method config: loadbalance,retries,timeout,sent
if (Objects.nonNull(serviceBean.getMethods())) {
build.setMethods(new ArrayList<>());
for (MethodConfig methodConfig : serviceBean.getMethods()) {
DubboRpcMethodExt methodExt = new DubboRpcMethodExt();
methodExt.setName(methodConfig.getName());
methodExt.setLoadbalance(methodConfig.getLoadbalance());
methodExt.setRetries(methodConfig.getRetries());
methodExt.setTimeout(methodConfig.getTimeout());
methodExt.setSent(methodConfig.getSent());
build.getMethods().add(methodExt);
}
}
return GsonUtils.getInstance().toJson(build);
return DubboRpcExtBuilders.buildRpcExt(serviceBean);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@

package org.apache.shenyu.client.apache.dubbo.processor.extractor;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.shenyu.client.core.register.ApiBean;
import org.apache.shenyu.client.core.register.matcher.ApiAnnotationProcessor;
import org.apache.shenyu.client.core.register.matcher.ExtractorProcessor;
import org.apache.shenyu.client.dubbo.common.dto.DubboRpcExt;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.client.dubbo.common.dto.DubboRpcExtBuilders;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.common.utils.ListUtil;

import java.util.List;
import java.util.Optional;

import static org.apache.dubbo.remoting.Constants.DEFAULT_CONNECT_TIMEOUT;

/**
* DubboServiceProcessor.
Expand Down Expand Up @@ -70,31 +63,16 @@ public Class<DubboService> matchAnnotation() {
private String getRpcExt(final ApiBean apiBean) {
final Object beanInstance = apiBean.getBeanInstance();
if (beanInstance instanceof ServiceBean) {
return getRpcExt((ServiceBean<?>) beanInstance);
return DubboRpcExtBuilders.buildRpcExt((ServiceBean<?>) beanInstance);
}
return "{}";
}

private String getRpcExt(final ApiBean.ApiDefinition definition) {
final Object beanInstance = definition.getApiBean().getBeanInstance();
if (beanInstance instanceof ServiceBean) {
return getRpcExt((ServiceBean<?>) beanInstance);
return DubboRpcExtBuilders.buildRpcExt((ServiceBean<?>) beanInstance);
}
return "{}";
}

private static String getRpcExt(final ServiceBean<?> serviceBean) {
DubboRpcExt build = DubboRpcExt.builder()
.protocol(StringUtils.isNotEmpty(serviceBean.getProtocol().getName()) ? serviceBean.getProtocol().getName() : "")
.group(StringUtils.isNotEmpty(serviceBean.getGroup()) ? serviceBean.getGroup() : "")
.version(StringUtils.isNotEmpty(serviceBean.getVersion()) ? serviceBean.getVersion() : "")
.loadbalance(StringUtils.isNotEmpty(serviceBean.getLoadbalance()) ? serviceBean.getLoadbalance() : CommonConstants.DEFAULT_LOADBALANCE)
.retries(Optional.ofNullable(serviceBean.getRetries()).orElse(CommonConstants.DEFAULT_RETRIES))
.timeout(Optional.ofNullable(serviceBean.getTimeout()).orElse(DEFAULT_CONNECT_TIMEOUT))
.sent(Optional.ofNullable(serviceBean.getSent()).orElse(Boolean.FALSE))
.cluster(StringUtils.isNotEmpty(serviceBean.getCluster()) ? serviceBean.getCluster() : Constants.DEFAULT_CLUSTER)
.url("")
.build();
return GsonUtils.getInstance().toJson(build);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package org.apache.shenyu.client.apache.dubbo.processor.extractor;

import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.shenyu.client.core.register.ApiBean;
import org.apache.shenyu.client.core.register.matcher.ApiAnnotationProcessor;
import org.apache.shenyu.client.core.register.matcher.ExtractorProcessor;
import org.apache.shenyu.client.dubbo.common.dto.DubboRpcExtBuilders;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.utils.ListUtil;
import org.springframework.stereotype.Service;

import java.util.List;

Expand All @@ -38,7 +40,14 @@ public List<String> supportedClient() {

@Override
public void process(final ApiBean apiBean, final Service annotation) {
apiBean.setBeanPath(annotation.value());
apiBean.setBeanPath(annotation.path());

apiBean.addProperties("rpcExt", getRpcExt(apiBean));
}

@Override
public void process(final ApiBean.ApiDefinition definition) {
definition.addProperties("rpcExt", getRpcExt(definition));
}

@Override
Expand All @@ -50,4 +59,20 @@ public void process(final ApiBean.ApiDefinition definition, final Service annota
public Class<Service> matchAnnotation() {
return Service.class;
}

private String getRpcExt(final ApiBean apiBean) {
final Object beanInstance = apiBean.getBeanInstance();
if (beanInstance instanceof ServiceBean) {
return DubboRpcExtBuilders.buildRpcExt((ServiceBean<?>) beanInstance);
}
return "{}";
}

private String getRpcExt(final ApiBean.ApiDefinition definition) {
final Object beanInstance = definition.getApiBean().getBeanInstance();
if (beanInstance instanceof ServiceBean) {
return DubboRpcExtBuilders.buildRpcExt((ServiceBean<?>) beanInstance);
}
return "{}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* 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.apache.shenyu.client.apache.dubbo.processor.extractor;

import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.shenyu.client.core.register.ApiBean;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.lang.reflect.Method;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class DubboServiceProcessorTest {

private DubboServiceProcessor dubboServiceProcessor;

@Mock
private ServiceBean<?> serviceBean;

@Mock
private ProtocolConfig protocolConfig;

@BeforeEach
void setUp() {
dubboServiceProcessor = new DubboServiceProcessor();
}

@Test
void testSupportedClient() {
List<String> clients = dubboServiceProcessor.supportedClient();
assertEquals(1, clients.size());
assertEquals(RpcTypeEnum.DUBBO.getName(), clients.get(0));
}

@Test
void testMatchAnnotation() {
assertEquals(DubboService.class, dubboServiceProcessor.matchAnnotation());
}

@Test
void testProcessWithServiceBeanSetsBeanPathAndRpcExt() throws Exception {
when(serviceBean.getProtocol()).thenReturn(protocolConfig);
when(protocolConfig.getName()).thenReturn("dubbo");
when(serviceBean.getGroup()).thenReturn("testGroup");
when(serviceBean.getVersion()).thenReturn("1.0.0");
when(serviceBean.getLoadbalance()).thenReturn("random");
when(serviceBean.getRetries()).thenReturn(3);
when(serviceBean.getTimeout()).thenReturn(1000);
when(serviceBean.getSent()).thenReturn(true);
when(serviceBean.getCluster()).thenReturn("failover");
when(serviceBean.getSerialization()).thenReturn("fastjson");
when(serviceBean.getMethods()).thenReturn(null);

ApiBean apiBean = new ApiBean(RpcTypeEnum.DUBBO.getName(), "testBean", serviceBean);

DubboService dubboServiceAnnotation = TestDubboServiceBean.class.getAnnotation(DubboService.class);
dubboServiceProcessor.process(apiBean, dubboServiceAnnotation);

assertEquals(dubboServiceAnnotation.path(), apiBean.getBeanPath());
String rpcExt = apiBean.getPropertiesValue("rpcExt");
assertNotNull(rpcExt);
assertTrue(rpcExt.contains("\"serialization\":\"fastjson\""));
assertTrue(rpcExt.contains("\"protocol\":\"dubbo\""));
assertTrue(rpcExt.contains("\"group\":\"testGroup\""));
}

@Test
void testProcessWithNonServiceBeanReturnsEmptyRpcExt() {
Object plainBean = new Object();
ApiBean apiBean = new ApiBean(RpcTypeEnum.DUBBO.getName(), "testBean", plainBean);

DubboService dubboServiceAnnotation = TestDubboServiceBean.class.getAnnotation(DubboService.class);
dubboServiceProcessor.process(apiBean, dubboServiceAnnotation);

String rpcExt = apiBean.getPropertiesValue("rpcExt");
assertEquals("{}", rpcExt);
}

@Test
void testProcessApiDefinitionWithServiceBean() throws Exception {
when(serviceBean.getProtocol()).thenReturn(protocolConfig);
when(protocolConfig.getName()).thenReturn("dubbo");
when(serviceBean.getGroup()).thenReturn("myGroup");
when(serviceBean.getVersion()).thenReturn("2.0.0");
when(serviceBean.getLoadbalance()).thenReturn(null);
when(serviceBean.getRetries()).thenReturn(null);
when(serviceBean.getTimeout()).thenReturn(null);
when(serviceBean.getSent()).thenReturn(null);
when(serviceBean.getCluster()).thenReturn(null);
when(serviceBean.getSerialization()).thenReturn("fastjson");
when(serviceBean.getMethods()).thenReturn(null);

ApiBean apiBean = new ApiBean(RpcTypeEnum.DUBBO.getName(), "testBean", serviceBean);
Method method = TestDubboServiceBean.class.getMethod("testMethod");
ApiBean.ApiDefinition definition = new ApiBean.ApiDefinition(apiBean, method);

dubboServiceProcessor.process(definition);

String rpcExt = definition.getPropertiesValue("rpcExt");
assertNotNull(rpcExt);
assertTrue(rpcExt.contains("\"serialization\":\"fastjson\""));
assertTrue(rpcExt.contains("\"protocol\":\"dubbo\""));
assertTrue(rpcExt.contains("\"group\":\"myGroup\""));
assertTrue(rpcExt.contains("\"version\":\"2.0.0\""));
}

@Test
void testProcessApiDefinitionWithNonServiceBean() throws Exception {
Object plainBean = new Object();
ApiBean apiBean = new ApiBean(RpcTypeEnum.DUBBO.getName(), "testBean", plainBean);
Method method = TestDubboServiceBean.class.getMethod("testMethod");
ApiBean.ApiDefinition definition = new ApiBean.ApiDefinition(apiBean, method);

dubboServiceProcessor.process(definition);

String rpcExt = definition.getPropertiesValue("rpcExt");
assertEquals("{}", rpcExt);
}

@Test
void testProcessWithNullProtocol() {
when(serviceBean.getProtocol()).thenReturn(null);
when(serviceBean.getGroup()).thenReturn(null);
when(serviceBean.getVersion()).thenReturn(null);
when(serviceBean.getLoadbalance()).thenReturn(null);
when(serviceBean.getRetries()).thenReturn(null);
when(serviceBean.getTimeout()).thenReturn(null);
when(serviceBean.getSent()).thenReturn(null);
when(serviceBean.getCluster()).thenReturn(null);
when(serviceBean.getSerialization()).thenReturn(null);
when(serviceBean.getMethods()).thenReturn(null);

ApiBean apiBean = new ApiBean(RpcTypeEnum.DUBBO.getName(), "testBean", serviceBean);

DubboService dubboServiceAnnotation = TestDubboServiceBean.class.getAnnotation(DubboService.class);
dubboServiceProcessor.process(apiBean, dubboServiceAnnotation);

String rpcExt = apiBean.getPropertiesValue("rpcExt");
assertNotNull(rpcExt);
assertTrue(rpcExt.contains("\"protocol\":\"\""));
}

@DubboService(path = "/test-dubbo-path")
private static class TestDubboServiceBean {
public void testMethod() {
}
}
}
Loading
Loading