Search in sources :

Example 66 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class ApacheDubboMetaDataSubscriber method onSubscribe.

@Override
public void onSubscribe(final MetaData metaData) {
    if (RpcTypeEnum.DUBBO.getName().equals(metaData.getRpcType())) {
        MetaData exist = META_DATA.get(metaData.getPath());
        if (Objects.isNull(exist) || Objects.isNull(ApacheDubboConfigCache.getInstance().get(metaData.getPath()))) {
            // The first initialization
            ApacheDubboConfigCache.getInstance().initRef(metaData);
        } else {
            // because these four properties will affect the call of Dubbo;
            if (!Objects.equals(metaData.getServiceName(), exist.getServiceName()) || !Objects.equals(metaData.getRpcExt(), exist.getRpcExt()) || !Objects.equals(metaData.getParameterTypes(), exist.getParameterTypes()) || !Objects.equals(metaData.getMethodName(), exist.getMethodName())) {
                ApacheDubboConfigCache.getInstance().build(metaData);
            }
        }
        META_DATA.put(metaData.getPath(), metaData);
    }
}
Also used : MetaData(org.apache.shenyu.common.dto.MetaData)

Example 67 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class ApacheDubboPluginTest method setUp.

@BeforeEach
public void setUp() {
    exchange = MockServerWebExchange.from(MockServerHttpRequest.get("localhost").build());
    metaData = new MetaData();
    metaData.setId("1332017966661636096");
    metaData.setAppName("dubbo");
    metaData.setPath("/dubbo/findAll");
    metaData.setServiceName("org.apache.shenyu.test.dubbo.api.service.DubboTestService");
    metaData.setMethodName("findAll");
    metaData.setRpcType(RpcTypeEnum.DUBBO.getName());
    ApacheDubboProxyService apacheDubboProxyService = mock(ApacheDubboProxyService.class);
    apacheDubboPlugin = new ApacheDubboPlugin(apacheDubboProxyService);
}
Also used : MetaData(org.apache.shenyu.common.dto.MetaData) ApacheDubboProxyService(org.apache.shenyu.plugin.apache.dubbo.proxy.ApacheDubboProxyService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 68 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class ApacheDubboPluginTest method testMethodIsNullExecute.

@Test
public void testMethodIsNullExecute() {
    ShenyuContext context = mock(ShenyuContext.class);
    exchange.getAttributes().put(Constants.CONTEXT, context);
    exchange.getAttributes().put(Constants.PARAM_TRANSFORM, "{key:value}");
    MetaData metaData = MetaData.builder().id("1332017966661636096").appName("dubbo").path("/dubbo/findAll").serviceName("org.apache.shenyu.test.dubbo.api.service.DubboTestService").rpcType(RpcTypeEnum.DUBBO.getName()).build();
    exchange.getAttributes().put(Constants.META_DATA, metaData);
    SelectorData selectorData = mock(SelectorData.class);
    RuleData data = mock(RuleData.class);
    assertThrows(NullPointerException.class, () -> {
        StepVerifier.create(apacheDubboPlugin.doExecute(exchange, chain, selectorData, data)).expectSubscription().verifyComplete();
    });
}
Also used : RuleData(org.apache.shenyu.common.dto.RuleData) ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) MetaData(org.apache.shenyu.common.dto.MetaData) SelectorData(org.apache.shenyu.common.dto.SelectorData) Test(org.junit.jupiter.api.Test)

Example 69 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class ApacheDubboConfigCacheTest method testBuild.

@Test
public void testBuild() {
    DubboParam dubboParamExtInfo = new DubboParam();
    dubboParamExtInfo.setVersion("2.7.5");
    dubboParamExtInfo.setGroup("Group");
    dubboParamExtInfo.setLoadbalance("Balance");
    dubboParamExtInfo.setUrl("http://192.168.55.113/dubbo");
    MetaData metaData = new MetaData();
    metaData.setRpcExt(GsonUtils.getInstance().toJson(dubboParamExtInfo));
    ApacheDubboConfigCache apacheDubboConfigCacheMock = mock(ApacheDubboConfigCache.class);
    when(apacheDubboConfigCacheMock.build(metaData)).thenReturn(new ReferenceConfig<>());
    assertNotNull(apacheDubboConfigCacheMock.build(metaData));
}
Also used : DubboParam(org.apache.shenyu.plugin.dubbo.common.cache.DubboParam) MetaData(org.apache.shenyu.common.dto.MetaData) Test(org.junit.jupiter.api.Test)

Example 70 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class MotanProxyService method genericInvoker.

/**
 * Generic invoker object.
 *
 * @param body the body
 * @param metaData the meta data
 * @param exchange the exchange
 * @return the object
 * @throws ShenyuException the shenyu exception
 */
@SuppressWarnings("all")
public Mono<Object> genericInvoker(final String body, final MetaData metaData, final ServerWebExchange exchange) throws ShenyuException {
    Map<String, Map<String, String>> rpcContext = exchange.getAttribute(Constants.GENERAL_CONTEXT);
    Optional.ofNullable(rpcContext).map(context -> context.get(PluginEnum.MOTAN.getName())).ifPresent(context -> {
        context.forEach((k, v) -> RpcContext.getContext().setRpcAttachment(k, v));
    });
    RefererConfig<CommonHandler> reference = ApplicationConfigCache.getInstance().get(metaData.getPath());
    if (Objects.isNull(reference) || StringUtils.isEmpty(reference.getServiceInterface())) {
        ApplicationConfigCache.getInstance().invalidate(metaData.getPath());
        reference = ApplicationConfigCache.getInstance().initRef(metaData);
    }
    CommonHandler commonHandler = reference.getRef();
    ApplicationConfigCache.MotanParamInfo motanParamInfo = ApplicationConfigCache.PARAM_MAP.get(metaData.getMethodName());
    Object[] params;
    if (Objects.isNull(motanParamInfo)) {
        params = new Object[0];
    } else {
        int num = motanParamInfo.getParamTypes().length;
        params = new Object[num];
        Map<String, Object> bodyMap = GsonUtils.getInstance().convertToMap(body);
        for (int i = 0; i < num; i++) {
            params[i] = bodyMap.get(motanParamInfo.getParamNames()[i]).toString();
        }
    }
    ResponseFuture responseFuture;
    // CHECKSTYLE:OFF IllegalCatch
    try {
        responseFuture = (ResponseFuture) commonHandler.asyncCall(metaData.getMethodName(), params, Object.class);
    } catch (Throwable e) {
        LOG.error("Exception caught in MotanProxyService#genericInvoker.", e);
        return null;
    }
    // CHECKSTYLE:ON IllegalCatch
    ResponseFuture finalResponseFuture = responseFuture;
    CompletableFuture<Object> future = CompletableFuture.supplyAsync(finalResponseFuture::getValue);
    return Mono.fromFuture(future.thenApply(ret -> {
        if (Objects.isNull(ret)) {
            ret = Constants.MOTAN_RPC_RESULT_EMPTY;
        }
        exchange.getAttributes().put(Constants.RPC_RESULT, ret);
        exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName());
        return ret;
    })).onErrorMap(ShenyuException::new);
}
Also used : Constants(org.apache.shenyu.common.constant.Constants) PluginEnum(org.apache.shenyu.common.enums.PluginEnum) Logger(org.slf4j.Logger) RefererConfig(com.weibo.api.motan.config.RefererConfig) CommonHandler(com.weibo.api.motan.proxy.CommonHandler) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) CompletableFuture(java.util.concurrent.CompletableFuture) ApplicationConfigCache(org.apache.shenyu.plugin.motan.cache.ApplicationConfigCache) StringUtils(org.apache.commons.lang3.StringUtils) ResultEnum(org.apache.shenyu.common.enums.ResultEnum) GsonUtils(org.apache.shenyu.common.utils.GsonUtils) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Objects(java.util.Objects) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) RpcContext(com.weibo.api.motan.rpc.RpcContext) Map(java.util.Map) Optional(java.util.Optional) MetaData(org.apache.shenyu.common.dto.MetaData) ResponseFuture(com.weibo.api.motan.rpc.ResponseFuture) ResponseFuture(com.weibo.api.motan.rpc.ResponseFuture) ApplicationConfigCache(org.apache.shenyu.plugin.motan.cache.ApplicationConfigCache) CommonHandler(com.weibo.api.motan.proxy.CommonHandler) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) Map(java.util.Map)

Aggregations

MetaData (org.apache.shenyu.common.dto.MetaData)70 Test (org.junit.jupiter.api.Test)31 BeforeEach (org.junit.jupiter.api.BeforeEach)14 RuleData (org.apache.shenyu.common.dto.RuleData)11 SelectorData (org.apache.shenyu.common.dto.SelectorData)10 ShenyuContext (org.apache.shenyu.plugin.api.context.ShenyuContext)9 ArrayList (java.util.ArrayList)8 MetaDataSubscriber (org.apache.shenyu.sync.data.api.MetaDataSubscriber)8 ServerWebExchange (org.springframework.web.server.ServerWebExchange)7 Objects (java.util.Objects)6 ShenyuException (org.apache.shenyu.common.exception.ShenyuException)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Optional (java.util.Optional)5 Constants (org.apache.shenyu.common.constant.Constants)5 ShenyuPluginChain (org.apache.shenyu.plugin.api.ShenyuPluginChain)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ResultEnum (org.apache.shenyu.common.enums.ResultEnum)4 RpcTypeEnum (org.apache.shenyu.common.enums.RpcTypeEnum)4 GsonUtils (org.apache.shenyu.common.utils.GsonUtils)4