Search in sources :

Example 6 with ProtocolConfig

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

the class AnnotationBean method postProcessAfterInitialization.

/**
     * init service config and export servcice
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (!isMatchPackage(bean)) {
        return bean;
    }
    Class<?> clazz = bean.getClass();
    if (isProxyBean(bean)) {
        clazz = AopUtils.getTargetClass(bean);
    }
    MotanService service = clazz.getAnnotation(MotanService.class);
    if (service != null) {
        ServiceConfigBean<Object> serviceConfig = new ServiceConfigBean<Object>();
        if (void.class.equals(service.interfaceClass())) {
            if (clazz.getInterfaces().length > 0) {
                Class<Object> clz = (Class<Object>) clazz.getInterfaces()[0];
                serviceConfig.setInterface(clz);
            } else {
                throw new IllegalStateException("Failed to export remote service class " + clazz.getName() + ", cause: The @Service undefined interfaceClass or interfaceName, and the service class unimplemented any interfaces.");
            }
        } else {
            serviceConfig.setInterface((Class<Object>) service.interfaceClass());
        }
        if (beanFactory != null) {
            serviceConfig.setBeanFactory(beanFactory);
            if (service.basicService() != null && service.basicService().length() > 0) {
                serviceConfig.setBasicServiceConfig(beanFactory.getBean(service.basicService(), BasicServiceInterfaceConfig.class));
            }
            if (service.export() != null && service.export().length() > 0) {
                serviceConfig.setExport(service.export());
            }
            if (service.host() != null && service.host().length() > 0) {
                serviceConfig.setHost(service.host());
            }
            String protocolValue = null;
            if (service.protocol() != null && service.protocol().length() > 0) {
                protocolValue = service.protocol();
            } else if (service.export() != null && service.export().length() > 0) {
                protocolValue = ConfigUtil.extractProtocols(service.export());
            }
            if (!StringUtils.isBlank(protocolValue)) {
                List<ProtocolConfig> protocolConfigs = SpringBeanUtil.getMultiBeans(beanFactory, protocolValue, SpringBeanUtil.COMMA_SPLIT_PATTERN, ProtocolConfig.class);
                serviceConfig.setProtocols(protocolConfigs);
            }
            if (service.registry() != null && service.registry().length() > 0) {
                List<RegistryConfig> registryConfigs = SpringBeanUtil.getMultiBeans(beanFactory, service.registry(), SpringBeanUtil.COMMA_SPLIT_PATTERN, RegistryConfig.class);
                serviceConfig.setRegistries(registryConfigs);
            }
            if (service.extConfig() != null && service.extConfig().length() > 0) {
                serviceConfig.setExtConfig(beanFactory.getBean(service.extConfig(), ExtConfig.class));
            }
            if (service.application() != null && service.application().length() > 0) {
                serviceConfig.setApplication(service.application());
            }
            if (service.module() != null && service.module().length() > 0) {
                serviceConfig.setModule(service.module());
            }
            if (service.group() != null && service.group().length() > 0) {
                serviceConfig.setGroup(service.group());
            }
            if (service.version() != null && service.version().length() > 0) {
                serviceConfig.setVersion(service.version());
            }
            if (service.proxy() != null && service.proxy().length() > 0) {
                serviceConfig.setProxy(service.proxy());
            }
            if (service.filter() != null && service.filter().length() > 0) {
                serviceConfig.setFilter(service.filter());
            }
            if (service.actives() > 0) {
                serviceConfig.setActives(service.actives());
            }
            if (service.async()) {
                serviceConfig.setAsync(service.async());
            }
            if (service.mock() != null && service.mock().length() > 0) {
                serviceConfig.setMock(service.mock());
            }
            // 是否共享 channel
            if (service.shareChannel()) {
                serviceConfig.setShareChannel(service.shareChannel());
            }
            // if throw exception when call failure,the default value is ture
            if (service.throwException()) {
                serviceConfig.setThrowException(service.throwException());
            }
            if (service.requestTimeout() > 0) {
                serviceConfig.setRequestTimeout(service.requestTimeout());
            }
            if (service.register()) {
                serviceConfig.setRegister(service.register());
            }
            if (service.accessLog()) {
                serviceConfig.setAccessLog("true");
            }
            if (service.check()) {
                serviceConfig.setCheck("true");
            }
            if (service.usegz()) {
                serviceConfig.setUsegz(service.usegz());
            }
            if (service.retries() > 0) {
                serviceConfig.setRetries(service.retries());
            }
            if (service.mingzSize() > 0) {
                serviceConfig.setMingzSize(service.mingzSize());
            }
            if (service.codec() != null && service.codec().length() > 0) {
                serviceConfig.setCodec(service.codec());
            }
            try {
                serviceConfig.afterPropertiesSet();
            } catch (RuntimeException e) {
                throw (RuntimeException) e;
            } catch (Exception e) {
                throw new IllegalStateException(e.getMessage(), e);
            }
        }
        serviceConfig.setRef(bean);
        serviceConfigs.add(serviceConfig);
        serviceConfig.export();
    }
    return bean;
}
Also used : RegistryConfig(com.weibo.api.motan.config.RegistryConfig) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) BeansException(org.springframework.beans.BeansException) ExtConfig(com.weibo.api.motan.config.ExtConfig) BasicServiceInterfaceConfig(com.weibo.api.motan.config.BasicServiceInterfaceConfig) ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig) MotanService(com.weibo.api.motan.config.springsupport.annotation.MotanService)

Example 7 with ProtocolConfig

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

the class SpringSupportTest method testProtocoConfigTest.

@Test
public void testProtocoConfigTest() {
    Map<String, ProtocolConfig> map = cp.getBeansOfType(ProtocolConfig.class);
    assertEquals(2, map.size());
    ProtocolConfig injvm = map.get("injvm");
    ProtocolConfig motan_rpc = map.get("motan");
    assertTrue(motan_rpc != null && injvm != null);
    assertEquals(injvm.isDefault().booleanValue(), true);
}
Also used : ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig) Test(org.junit.Test)

Example 8 with ProtocolConfig

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

the class MockServiceConfig method mockProtocolConfig.

protected static ProtocolConfig mockProtocolConfig(String protocolName) {
    ProtocolConfig pc = createProtocol(protocolName);
    pc.setEndpointFactory("mockEndpoint");
    return pc;
}
Also used : ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig)

Example 9 with ProtocolConfig

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

the class MotanApiClientDemo method main.

public static void main(String[] args) {
    RefererConfig<MotanDemoService> motanDemoServiceReferer = new RefererConfig<MotanDemoService>();
    // 设置接口及实现类
    motanDemoServiceReferer.setInterface(MotanDemoService.class);
    // 配置服务的group以及版本号
    motanDemoServiceReferer.setGroup("motan-demo-rpc");
    motanDemoServiceReferer.setVersion("1.0");
    motanDemoServiceReferer.setRequestTimeout(300);
    // 配置注册中心直连调用
    // RegistryConfig directRegistry = new RegistryConfig();
    // directRegistry.setRegProtocol("local");
    // motanDemoServiceReferer.setRegistry(directRegistry);
    // 配置ZooKeeper注册中心
    RegistryConfig zookeeperRegistry = new RegistryConfig();
    zookeeperRegistry.setRegProtocol("zookeeper");
    zookeeperRegistry.setAddress("127.0.0.1:2181");
    motanDemoServiceReferer.setRegistry(zookeeperRegistry);
    // 配置RPC协议
    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setId("motan");
    protocol.setName("motan");
    motanDemoServiceReferer.setProtocol(protocol);
    // motanDemoServiceReferer.setDirectUrl("localhost:8002");  // 注册中心直连调用需添加此配置
    // 使用服务
    MotanDemoService service = motanDemoServiceReferer.getRef();
    System.out.println(service.hello("motan"));
    System.exit(0);
}
Also used : RegistryConfig(com.weibo.api.motan.config.RegistryConfig) MotanDemoService(com.weibo.motan.demo.service.MotanDemoService) ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig) RefererConfig(com.weibo.api.motan.config.RefererConfig)

Aggregations

ProtocolConfig (com.weibo.api.motan.config.ProtocolConfig)9 RegistryConfig (com.weibo.api.motan.config.RegistryConfig)4 ExtConfig (com.weibo.api.motan.config.ExtConfig)2 MotanDemoService (com.weibo.motan.demo.service.MotanDemoService)2 BeansException (org.springframework.beans.BeansException)2 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)2 BasicRefererInterfaceConfig (com.weibo.api.motan.config.BasicRefererInterfaceConfig)1 BasicServiceInterfaceConfig (com.weibo.api.motan.config.BasicServiceInterfaceConfig)1 RefererConfig (com.weibo.api.motan.config.RefererConfig)1 ServiceConfig (com.weibo.api.motan.config.ServiceConfig)1 MotanService (com.weibo.api.motan.config.springsupport.annotation.MotanService)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1