Search in sources :

Example 1 with ProtocolConfig

use of com.weibo.api.motan.config.ProtocolConfig in project motan by weibocom.

the class AnnotationBean method refer.

/**
     * refer proxy
     *
     * @param reference
     * @param referenceClass
     * @param <T>
     * @return
     */
private <T> Object refer(MotanReferer reference, Class<?> referenceClass) {
    String interfaceName;
    if (!void.class.equals(reference.interfaceClass())) {
        interfaceName = reference.interfaceClass().getName();
    } else if (referenceClass.isInterface()) {
        interfaceName = referenceClass.getName();
    } else {
        throw new IllegalStateException("The @Reference undefined interfaceClass or interfaceName, and the property type " + referenceClass.getName() + " is not a interface.");
    }
    String key = reference.group() + "/" + interfaceName + ":" + reference.version();
    RefererConfigBean<T> referenceConfig = referenceConfigs.get(key);
    if (referenceConfig == null) {
        referenceConfig = new RefererConfigBean<T>();
        referenceConfig.setBeanFactory(beanFactory);
        if (void.class.equals(reference.interfaceClass()) && referenceClass.isInterface()) {
            referenceConfig.setInterface((Class<T>) referenceClass);
        } else if (!void.class.equals(reference.interfaceClass())) {
            referenceConfig.setInterface((Class<T>) reference.interfaceClass());
        }
        if (beanFactory != null) {
            if (reference.protocol() != null && reference.protocol().length() > 0) {
                //多个PROTOCOL
                List<ProtocolConfig> protocolConfigs = SpringBeanUtil.getMultiBeans(beanFactory, reference.protocol(), SpringBeanUtil.COMMA_SPLIT_PATTERN, ProtocolConfig.class);
                referenceConfig.setProtocols(protocolConfigs);
            }
            if (reference.directUrl() != null && reference.directUrl().length() > 0) {
                referenceConfig.setDirectUrl(reference.directUrl());
            }
            if (reference.basicReferer() != null && reference.basicReferer().length() > 0) {
                BasicRefererInterfaceConfig biConfig = beanFactory.getBean(reference.basicReferer(), BasicRefererInterfaceConfig.class);
                if (biConfig != null) {
                    referenceConfig.setBasicReferer(biConfig);
                }
            }
            if (reference.client() != null && reference.client().length() > 0) {
            //TODO?
            //                    referenceConfig.setC(reference.client());
            }
            if (reference.registry() != null && reference.registry().length() > 0) {
                List<RegistryConfig> registryConfigs = SpringBeanUtil.getMultiBeans(beanFactory, reference.registry(), SpringBeanUtil.COMMA_SPLIT_PATTERN, RegistryConfig.class);
                referenceConfig.setRegistries(registryConfigs);
            }
            if (reference.extConfig() != null && reference.extConfig().length() > 0) {
                referenceConfig.setExtConfig(beanFactory.getBean(reference.extConfig(), ExtConfig.class));
            }
            if (reference.application() != null && reference.application().length() > 0) {
                referenceConfig.setApplication(reference.application());
            }
            if (reference.module() != null && reference.module().length() > 0) {
                referenceConfig.setModule(reference.module());
            }
            if (reference.group() != null && reference.group().length() > 0) {
                referenceConfig.setGroup(reference.group());
            }
            if (reference.version() != null && reference.version().length() > 0) {
                referenceConfig.setVersion(reference.version());
            }
            if (reference.proxy() != null && reference.proxy().length() > 0) {
                referenceConfig.setProxy(reference.proxy());
            }
            if (reference.filter() != null && reference.filter().length() > 0) {
                referenceConfig.setFilter(reference.filter());
            }
            if (reference.actives() > 0) {
                referenceConfig.setActives(reference.actives());
            }
            if (reference.async()) {
                referenceConfig.setAsync(reference.async());
            }
            if (reference.mock() != null && reference.mock().length() > 0) {
                referenceConfig.setMock(reference.mock());
            }
            if (reference.shareChannel()) {
                referenceConfig.setShareChannel(reference.shareChannel());
            }
            // if throw exception when call failure,the default value is ture
            if (reference.throwException()) {
                referenceConfig.setThrowException(reference.throwException());
            }
            if (reference.requestTimeout() > 0) {
                referenceConfig.setRequestTimeout(reference.requestTimeout());
            }
            if (reference.register()) {
                referenceConfig.setRegister(reference.register());
            }
            if (reference.accessLog()) {
                referenceConfig.setAccessLog("true");
            }
            if (reference.check()) {
                referenceConfig.setCheck("true");
            }
            if (reference.retries() > 0) {
                referenceConfig.setRetries(reference.retries());
            }
            if (reference.usegz()) {
                referenceConfig.setUsegz(reference.usegz());
            }
            if (reference.mingzSize() > 0) {
                referenceConfig.setMingzSize(reference.mingzSize());
            }
            if (reference.codec() != null && reference.codec().length() > 0) {
                referenceConfig.setCodec(reference.codec());
            }
            if (reference.mean() != null && reference.mean().length() > 0) {
                referenceConfig.setMean(reference.mean());
            }
            if (reference.p90() != null && reference.p90().length() > 0) {
                referenceConfig.setP90(reference.p90());
            }
            if (reference.p99() != null && reference.p99().length() > 0) {
                referenceConfig.setP99(reference.p99());
            }
            if (reference.p999() != null && reference.p999().length() > 0) {
                referenceConfig.setP999(reference.p999());
            }
            if (reference.errorRate() != null && reference.errorRate().length() > 0) {
                referenceConfig.setErrorRate(reference.errorRate());
            }
            try {
                referenceConfig.afterPropertiesSet();
            } catch (RuntimeException e) {
                throw (RuntimeException) e;
            } catch (Exception e) {
                throw new IllegalStateException(e.getMessage(), e);
            }
        }
        referenceConfigs.putIfAbsent(key, referenceConfig);
        referenceConfig = referenceConfigs.get(key);
    }
    return referenceConfig.getRef();
}
Also used : RegistryConfig(com.weibo.api.motan.config.RegistryConfig) BasicRefererInterfaceConfig(com.weibo.api.motan.config.BasicRefererInterfaceConfig) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) BeansException(org.springframework.beans.BeansException) ExtConfig(com.weibo.api.motan.config.ExtConfig) ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig)

Example 2 with ProtocolConfig

use of com.weibo.api.motan.config.ProtocolConfig in project motan by weibocom.

the class ServiceConfigBean method checkAndConfigExport.

/**
     * 检查是否已经装配export,如果没有则到basicConfig查找
     */
private void checkAndConfigExport() {
    if (StringUtils.isBlank(getExport()) && getBasicServiceConfig() != null && !StringUtils.isBlank(getBasicServiceConfig().getExport())) {
        setExport(getBasicServiceConfig().getExport());
        if (getBasicServiceConfig().getProtocols() != null) {
            setProtocols(new ArrayList<ProtocolConfig>(getBasicServiceConfig().getProtocols()));
        }
    }
    if (CollectionUtil.isEmpty(getProtocols()) && StringUtils.isNotEmpty(getExport())) {
        Map<String, Integer> exportMap = ConfigUtil.parseExport(export);
        if (!exportMap.isEmpty()) {
            List<ProtocolConfig> protos = new ArrayList<ProtocolConfig>();
            for (String p : exportMap.keySet()) {
                ProtocolConfig proto = null;
                try {
                    proto = beanFactory.getBean(p, ProtocolConfig.class);
                } catch (NoSuchBeanDefinitionException e) {
                }
                if (proto == null) {
                    if (MotanConstants.PROTOCOL_MOTAN.equals(p)) {
                        proto = MotanFrameworkUtil.getDefaultProtocolConfig();
                    } else {
                        throw new MotanFrameworkException(String.format("cann't find %s ProtocolConfig bean! export:%s", p, export), MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
                    }
                }
                protos.add(proto);
            }
            setProtocols(protos);
        }
    }
    if (StringUtils.isEmpty(getExport()) || CollectionUtil.isEmpty(getProtocols())) {
        throw new MotanFrameworkException(String.format("%s ServiceConfig must config right export value!", getInterface().getName()), MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ArrayList(java.util.ArrayList) ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 3 with ProtocolConfig

use of com.weibo.api.motan.config.ProtocolConfig in project motan by weibocom.

the class MockServiceConfig method createProtocol.

protected static ProtocolConfig createProtocol(String protocolName) {
    ProtocolConfig pc = new ProtocolConfig();
    pc.setName(protocolName);
    pc.setId(pc.getName());
    return pc;
}
Also used : ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig)

Example 4 with ProtocolConfig

use of com.weibo.api.motan.config.ProtocolConfig in project motan by weibocom.

the class MotanFrameworkUtil method getDefaultProtocolConfig.

/**
 * 获取默认motan协议配置
 *
 * @return motan协议配置
 */
public static ProtocolConfig getDefaultProtocolConfig() {
    ProtocolConfig pc = new ProtocolConfig();
    pc.setId("motan");
    pc.setName("motan");
    return pc;
}
Also used : ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig)

Example 5 with ProtocolConfig

use of com.weibo.api.motan.config.ProtocolConfig in project motan by weibocom.

the class Motan2RpcClient method motan2ApiCommonClientDemo.

public static void motan2ApiCommonClientDemo() throws Throwable {
    RefererConfig<CommonHandler> referer = new RefererConfig<>();
    // 设置服务端接口
    referer.setInterface(CommonHandler.class);
    referer.setServiceInterface("com.weibo.motan.demo.service.MotanDemoService");
    // 配置服务的group以及版本号
    referer.setGroup("motan-demo-rpc");
    referer.setVersion("1.0");
    referer.setRequestTimeout(1000);
    // 配置注册中心直连调用
    RegistryConfig registry = new RegistryConfig();
    registry.setRegProtocol("direct");
    registry.setAddress("127.0.0.1:8001");
    referer.setRegistry(registry);
    // 配置RPC协议
    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setId("motan2");
    protocol.setName("motan2");
    referer.setProtocol(protocol);
    // 使用服务
    CommonHandler client = referer.getRef();
    System.out.println(client.call("hello", new Object[] { "a" }, String.class));
}
Also used : RegistryConfig(com.weibo.api.motan.config.RegistryConfig) CommonHandler(com.weibo.api.motan.proxy.CommonHandler) ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig) RefererConfig(com.weibo.api.motan.config.RefererConfig)

Aggregations

ProtocolConfig (com.weibo.api.motan.config.ProtocolConfig)12 RegistryConfig (com.weibo.api.motan.config.RegistryConfig)7 Before (org.junit.Before)3 RefererConfig (com.weibo.api.motan.config.RefererConfig)2 MotanDemoService (com.weibo.motan.demo.service.MotanDemoService)2 BasicRefererInterfaceConfig (com.weibo.api.motan.config.BasicRefererInterfaceConfig)1 ExtConfig (com.weibo.api.motan.config.ExtConfig)1 ServiceConfig (com.weibo.api.motan.config.ServiceConfig)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 RestfulServletContainerListener (com.weibo.api.motan.protocol.restful.support.servlet.RestfulServletContainerListener)1 CommonHandler (com.weibo.api.motan.proxy.CommonHandler)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Context (org.apache.catalina.Context)1 Wrapper (org.apache.catalina.Wrapper)1 Tomcat (org.apache.catalina.startup.Tomcat)1 HttpServletDispatcher (org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher)1 Test (org.junit.Test)1 BeansException (org.springframework.beans.BeansException)1