當前位置: 首頁>>代碼示例>>Java>>正文


Java RpcContext類代碼示例

本文整理匯總了Java中com.alibaba.dubbo.rpc.RpcContext的典型用法代碼示例。如果您正苦於以下問題:Java RpcContext類的具體用法?Java RpcContext怎麽用?Java RpcContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


RpcContext類屬於com.alibaba.dubbo.rpc包,在下文中一共展示了RpcContext類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: subscribe

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
public void subscribe(URL url, NotifyListener listener) {
    if (getUrl().getPort() == 0) {
        URL registryUrl = RpcContext.getContext().getUrl();
        if (registryUrl != null && registryUrl.getPort() > 0
        		&& RegistryService.class.getName().equals(registryUrl.getPath())) {
            super.setUrl(registryUrl);
            super.register(registryUrl);
        }
    }
    String client = RpcContext.getContext().getRemoteAddressString();
    ConcurrentMap<URL, Set<NotifyListener>> clientListeners = remoteSubscribed.get(client);
    if (clientListeners == null) {
        remoteSubscribed.putIfAbsent(client, new ConcurrentHashMap<URL, Set<NotifyListener>>());
        clientListeners = remoteSubscribed.get(client);
    }
    Set<NotifyListener> listeners = clientListeners.get(url);
    if (listeners == null) {
        clientListeners.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
        listeners = clientListeners.get(url);
    }
    listeners.add(listener);
    super.subscribe(url, listener);
    subscribed(url, listener);
}
 
開發者ID:dachengxi,項目名稱:EatDubbo,代碼行數:25,代碼來源:SimpleRegistryService.java

示例2: buildParentSpan

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
private Span buildParentSpan(RpcContext carrier, boolean skip,
                             long traceId, long spanId) {
    Map<String, String> attachments = carrier.getAttachments();
    Span.SpanBuilder span = Span.builder().traceId(traceId).spanId(spanId);
    String processId = attachments.get(Span.PROCESS_ID_NAME);
    String parentName = attachments.get(Span.SPAN_NAME_NAME);
    if (StringUtils.hasText(parentName)) {
        span.name(parentName);
    }
    if (StringUtils.hasText(processId)) {
        span.processId(processId);
    }
    if (attachments.get(Span.PARENT_ID_NAME) != null) {
        span.parent(Span
                .hexToId(attachments.get(Span.PARENT_ID_NAME)));
    }
    span.remote(true);
    if (skip) {
        span.exportable(false);
    }
    return span.build();
}
 
開發者ID:threeq,項目名稱:dubbo-zipkin,代碼行數:23,代碼來源:DubboSpanExtractor.java

示例3: testSetContext

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Test
public void testSetContext() {
    invocation = EasyMock.createMock(Invocation.class);
    EasyMock.expect(invocation.getMethodName()).andReturn("$enumlength").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] { Enum.class }).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] { "hello" }).anyTimes();
    EasyMock.expect(invocation.getAttachments()).andReturn(null).anyTimes();
    EasyMock.replay(invocation);
    invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class).anyTimes();
    RpcResult result = new RpcResult();
    result.setValue("High");
    EasyMock.expect(invoker.invoke(invocation)).andReturn(result).anyTimes();
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    EasyMock.expect(invoker.getUrl()).andReturn(url).anyTimes();
    EasyMock.replay(invoker);
    contextFilter.invoke(invoker, invocation);
    assertNull(RpcContext.getContext().getInvoker());
}
 
開發者ID:dachengxi,項目名稱:EatDubbo,代碼行數:22,代碼來源:ContextFilterTest.java

示例4: afterPreCap

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
@Override
public void afterPreCap(InvokeChainContext context, Object[] args) {

    String url = (String) context.get(InvokeChainConstants.CLIENT_SPAN_THREADLOCAL_STOREKEY);

    Span span = this.spanFactory.getSpanFromContext(url);

    String spanMeta = this.spanFactory.getSpanMeta(span);
    RpcContext.getContext().setAttachment(InvokeChainConstants.PARAM_RPCHEAD_SPANINFO, spanMeta);
    if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
        SlowOperContext slowOperContext = new SlowOperContext();
        // dubbo雖屬於rpc,但從其使用方式上屬於方法級
        Invocation invocation = (Invocation) args[1];
        slowOperContext.put(SlowOperConstants.PROTOCOL_METHOD_PARAMS, parseParams(invocation.getArguments()));

        Object params[] = { span, slowOperContext };
        UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap",
                span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.PRECAP, context, params);
    }
}
 
開發者ID:uavorg,項目名稱:uavstack,代碼行數:21,代碼來源:DubboConsumerAdapter.java

示例5: filter

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
public void filter(ClientRequestContext requestContext) throws IOException {
    int size = 0;
    for (Map.Entry<String, String> entry : RpcContext.getContext().getAttachments().entrySet()) {
        if (entry.getValue().contains(",") || entry.getValue().contains("=")
                || entry.getKey().contains(",") || entry.getKey().contains("=")) {
            throw new IllegalArgumentException("The attachments of " + RpcContext.class.getSimpleName() + " must not contain ',' or '=' when using rest protocol");
        }

        // TODO for now we don't consider the differences of encoding and server limit
        size += entry.getValue().getBytes("UTF-8").length;
        if (size > MAX_HEADER_SIZE) {
            throw new IllegalArgumentException("The attachments of " + RpcContext.class.getSimpleName() + " is too big");
        }

        StringBuilder attachments = new StringBuilder();
        attachments.append(entry.getKey());
        attachments.append("=");
        attachments.append(entry.getValue());
        requestContext.getHeaders().add(DUBBO_ATTACHMENT_HEADER, attachments.toString());
    }
}
 
開發者ID:yunhaibin,項目名稱:dubbox-hystrix,代碼行數:22,代碼來源:RpcContextFilter.java

示例6: testGenericFilter

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
@Test
public void testGenericFilter() throws Exception {
    MonitorFilter monitorFilter = new MonitorFilter();
    monitorFilter.setMonitorFactory(monitorFactory);
    Invocation invocation = new RpcInvocation("$invoke", new Class<?>[] { String.class, String[].class, Object[].class }, new Object[] { "xxx", new String[] {}, new Object[] {} } );
    RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
    monitorFilter.invoke(serviceInvoker, invocation);
    while (lastStatistics == null) {
        Thread.sleep(10);
    }
    Assert.assertEquals("abc", lastStatistics.getParameter(MonitorService.APPLICATION));
    Assert.assertEquals(MonitorService.class.getName(), lastStatistics.getParameter(MonitorService.INTERFACE));
    Assert.assertEquals("xxx", lastStatistics.getParameter(MonitorService.METHOD));
    Assert.assertEquals(NetUtils.getLocalHost() + ":20880", lastStatistics.getParameter(MonitorService.PROVIDER));
    Assert.assertEquals(NetUtils.getLocalHost(), lastStatistics.getAddress());
    Assert.assertEquals(null, lastStatistics.getParameter(MonitorService.CONSUMER));
    Assert.assertEquals(1, lastStatistics.getParameter(MonitorService.SUCCESS, 0));
    Assert.assertEquals(0, lastStatistics.getParameter(MonitorService.FAILURE, 0));
    Assert.assertEquals(1, lastStatistics.getParameter(MonitorService.CONCURRENT, 0));
    Assert.assertEquals(invocation, lastInvocation);
}
 
開發者ID:dachengxi,項目名稱:EatDubbo,代碼行數:22,代碼來源:MonitorFilterTest.java

示例7: test_Async_Future_Multi

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
@Test
public void test_Async_Future_Multi() throws Exception {
    initOrResetUrl(true);
    destroyService();
    exportService();
    referService();
    
    int requestId1 = 1;
    Person ret = demoProxy.get(requestId1);
    Assert.assertEquals(null, ret);
    Future<Person> p1Future = RpcContext.getContext().getFuture();
    
    int requestId2 = 1;
    Person ret2 = demoProxy.get(requestId2);
    Assert.assertEquals(null, ret2);
    Future<Person> p2Future = RpcContext.getContext().getFuture();
    
    ret = p1Future.get(1000, TimeUnit.MICROSECONDS);
    ret2 = p2Future.get(1000, TimeUnit.MICROSECONDS);
    Assert.assertEquals(requestId1, ret.getId());
    Assert.assertEquals(requestId2, ret.getId());
    destroyService();
}
 
開發者ID:yunhaibin,項目名稱:dubbox-hystrix,代碼行數:24,代碼來源:ImplicitCallBackTest.java

示例8: invoke

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (invoker.getUrl().hasParameter(Constants.MONITOR_KEY)) {
        RpcContext context = RpcContext.getContext(); // 提供方必須在invoke()之前獲取context信息
        long start = System.currentTimeMillis(); // 記錄起始時間戮
        getConcurrent(invoker, invocation).incrementAndGet(); // 並發計數
        try {
            Result result = invoker.invoke(invocation); // 讓調用鏈往下執行
            collect(invoker, invocation, result, context, start, false);
            return result;
        } catch (RpcException e) {
            collect(invoker, invocation, null, context, start, true);
            throw e;
        } finally {
            getConcurrent(invoker, invocation).decrementAndGet(); // 並發計數
        }
    } else {
        return invoker.invoke(invocation);
    }
}
 
開發者ID:flychao88,項目名稱:dubbocloud,代碼行數:20,代碼來源:MonitorFilter.java

示例9: invoke

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    RpcContext.getContext()
            .setInvoker(invoker)
            .setInvocation(invocation)
            .setLocalAddress(NetUtils.getLocalHost(), 0)
            .setRemoteAddress(invoker.getUrl().getHost(), 
                              invoker.getUrl().getPort());
    if (invocation instanceof RpcInvocation) {
        ((RpcInvocation)invocation).setInvoker(invoker);
    }
    try {
        return invoker.invoke(invocation);
    } finally {
        RpcContext.getContext().clearAttachments();
    }
}
 
開發者ID:zhuxiaolei,項目名稱:dubbo2,代碼行數:17,代碼來源:ConsumerContextFilter.java

示例10: testRetryFailed

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
@Test()
public void testRetryFailed() {

    resetInvokerToException();

    FailbackClusterInvoker<FailbackClusterInvokerTest> invoker = new FailbackClusterInvoker<FailbackClusterInvokerTest>(
                                                                                                                        dic);
    invoker.invoke(invocation);
    Assert.assertNull(RpcContext.getContext().getInvoker());
    invoker.retryFailed();// when retry the invoker which get from failed map already is not the mocked invoker,so
                          // it can be invoke successfully
}
 
開發者ID:zhuxiaolei,項目名稱:dubbo2,代碼行數:13,代碼來源:FailbackClusterInvokerTest.java

示例11: invoke

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (RpcContext.getContext().isConsumerSide()) {
        RpcContext.getContext().setAttachment(CommonConstant.TX_TRANSACTION_GROUP,
                TxTransactionLocal.getInstance().getTxGroupId());
    }
    return invoker.invoke(invocation);
}
 
開發者ID:yu199195,項目名稱:happylifeplat-transaction,代碼行數:9,代碼來源:DubboTxTransactionFilter.java

示例12: doInvoke

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
public Result doInvoke(Invocation invocation) throws Throwable {
    Exporter<?> exporter = InjvmProtocol.getExporter(exporterMap, getUrl());
    if (exporter == null)  {
        throw new RpcException("Service [" + key + "] not found.");
    }
    RpcContext.getContext().setRemoteAddress(NetUtils.LOCALHOST, 0);
    return exporter.getInvoker().invoke(invocation);
}
 
開發者ID:yunhaibin,項目名稱:dubbox-hystrix,代碼行數:9,代碼來源:InjvmInvoker.java

示例13: unregister

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
public void unregister(URL url) {
    String client = RpcContext.getContext().getRemoteAddressString();
    Set<URL> urls = remoteRegistered.get(client);
    if (urls != null && urls.size() > 0) {
        urls.remove(url);
    }
    super.unregister(url);
    unregistered(url);
}
 
開發者ID:dachengxi,項目名稱:EatDubbo,代碼行數:10,代碼來源:SimpleRegistryService.java

示例14: reply

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
@Override
public Object reply( ExchangeChannel channel, Object msg ) throws RemotingException {

    if ( msg instanceof Invocation ) {
        Invocation inv = ( Invocation ) msg;
        String serviceName = inv.getAttachments().get(Constants.INTERFACE_KEY);
        String serviceKey = serviceKey( channel.getLocalAddress().getPort(),
                                        serviceName, null, null );
        DubboExporter<?> exporter = (DubboExporter<?>) exporterMap.get( serviceKey );
        if (exporter == null) {
            throw new RemotingException(channel,
                                        "Not found exported service: "
                                                + serviceKey
                                                + " in "
                                                + exporterMap.keySet()
                                                + ", may be version or group mismatch "
                                                + ", channel: consumer: "
                                                + channel.getRemoteAddress()
                                                + " --> provider: "
                                                + channel.getLocalAddress()
                                                + ", message:"+ msg);
        }

        RpcContext.getContext().setRemoteAddress(channel.getRemoteAddress());
        return exporter.getInvoker().invoke( inv );

    }

    throw new RemotingException(channel,
                                "Unsupported request: "
                                        + (msg.getClass().getName() + ": " + msg)
                                        + ", channel: consumer: "
                                        + channel.getRemoteAddress()
                                        + " --> provider: "
                                        + channel.getLocalAddress());
}
 
開發者ID:yunhaibin,項目名稱:dubbox-hystrix,代碼行數:37,代碼來源:ThriftProtocol.java

示例15: handle

import com.alibaba.dubbo.rpc.RpcContext; //導入依賴的package包/類
public void handle(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    String uri = request.getRequestURI();
    HttpInvokerServiceExporter skeleton = skeletonMap.get(uri);
    if (! request.getMethod().equalsIgnoreCase("POST")) {
        response.setStatus(500);
    } else {
        RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort());
        try {
            skeleton.handleRequest(request, response);
        } catch (Throwable e) {
            throw new ServletException(e);
        }
    }
}
 
開發者ID:yunhaibin,項目名稱:dubbox-hystrix,代碼行數:16,代碼來源:HttpProtocol.java


注:本文中的com.alibaba.dubbo.rpc.RpcContext類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。