Search in sources :

Example 1 with TripleServerInterceptor

use of com.alipay.sofa.rpc.interceptor.TripleServerInterceptor in project sofa-rpc by sofastack.

the class TripleServer method registerProcessor.

@Override
public void registerProcessor(ProviderConfig providerConfig, Invoker instance) {
    Object ref = providerConfig.getRef();
    this.lock.lock();
    try {
        // wrap invoker to support unique id
        UniqueIdInvoker oldInvoker = this.invokerMap.putIfAbsent(providerConfig.getInterfaceId(), new UniqueIdInvoker());
        if (null != oldInvoker) {
            // we only need register given invoker into unique id invoker.
            if (!oldInvoker.registerInvoker(providerConfig, instance)) {
                throw new IllegalStateException("Can not expose service with interface:" + providerConfig.getInterfaceId() + " and unique id: " + providerConfig.getUniqueId());
            }
            return;
        }
        UniqueIdInvoker uniqueIdInvoker = this.invokerMap.get(providerConfig.getInterfaceId());
        if (!uniqueIdInvoker.registerInvoker(providerConfig, instance)) {
            throw new IllegalStateException("Can not expose service with interface:" + providerConfig.getInterfaceId() + " and unique id: " + providerConfig.getUniqueId());
        }
        // create service definition
        ServerServiceDefinition serviceDef;
        if (SofaProtoUtils.isProtoClass(ref)) {
            // refer is BindableService
            this.setBindableProxiedImpl(providerConfig, uniqueIdInvoker);
            BindableService bindableService = (BindableService) providerConfig.getRef();
            serviceDef = bindableService.bindService();
        } else {
            GenericServiceImpl genericService = new GenericServiceImpl(uniqueIdInvoker, providerConfig.getProxyClass());
            genericService.setProxiedImpl(genericService);
            serviceDef = buildSofaServiceDef(genericService, providerConfig);
        }
        List<TripleServerInterceptor> interceptorList = buildInterceptorChain(serviceDef);
        ServerServiceDefinition serviceDefinition = ServerInterceptors.intercept(serviceDef, interceptorList);
        this.serviceInfo.put(providerConfig, serviceDefinition);
        ServerServiceDefinition ssd = this.handlerRegistry.addService(serviceDefinition);
        if (ssd != null) {
            throw new IllegalStateException("Can not expose service with same name:" + serviceDefinition.getServiceDescriptor().getName());
        }
        this.invokerCnt.incrementAndGet();
    } catch (Exception e) {
        String msg = "Register triple service error";
        LOGGER.error(msg, e);
        this.serviceInfo.remove(providerConfig);
        throw new SofaRpcRuntimeException(msg, e);
    } finally {
        this.lock.unlock();
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) TripleServerInterceptor(com.alipay.sofa.rpc.interceptor.TripleServerInterceptor) ServerServiceDefinition(io.grpc.ServerServiceDefinition) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) BindableService(io.grpc.BindableService)

Example 2 with TripleServerInterceptor

use of com.alipay.sofa.rpc.interceptor.TripleServerInterceptor in project sofa-rpc by sofastack.

the class TripleServer method buildInterceptorChain.

/**
 * build chain
 *
 * @param serviceDef
 * @return
 */
protected List<TripleServerInterceptor> buildInterceptorChain(ServerServiceDefinition serviceDef) {
    List<TripleServerInterceptor> interceptorList = new ArrayList<>();
    interceptorList.add(new ServerReqHeaderInterceptor(serviceDef));
    return interceptorList;
}
Also used : TripleServerInterceptor(com.alipay.sofa.rpc.interceptor.TripleServerInterceptor) ArrayList(java.util.ArrayList) ServerReqHeaderInterceptor(com.alipay.sofa.rpc.interceptor.ServerReqHeaderInterceptor)

Aggregations

TripleServerInterceptor (com.alipay.sofa.rpc.interceptor.TripleServerInterceptor)2 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)1 ServerReqHeaderInterceptor (com.alipay.sofa.rpc.interceptor.ServerReqHeaderInterceptor)1 BindableService (io.grpc.BindableService)1 ServerServiceDefinition (io.grpc.ServerServiceDefinition)1 ArrayList (java.util.ArrayList)1