Search in sources :

Example 1 with RegistryConfig

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

the class MotanApiExportDemo method main.

public static void main(String[] args) throws InterruptedException {
    ServiceConfig<MotanDemoService> motanDemoService = new ServiceConfig<MotanDemoService>();
    // 设置接口及实现类
    motanDemoService.setInterface(MotanDemoService.class);
    motanDemoService.setRef(new MotanDemoServiceImpl());
    // 配置服务的group以及版本号
    motanDemoService.setGroup("motan-demo-rpc");
    motanDemoService.setVersion("1.0");
    // 配置注册中心直连调用
    // RegistryConfig directRegistry = new RegistryConfig();
    // directRegistry.setRegProtocol("local");
    // directRegistry.setCheck("false"); //不检查是否注册成功
    // motanDemoService.setRegistry(directRegistry);
    // 配置ZooKeeper注册中心
    RegistryConfig zookeeperRegistry = new RegistryConfig();
    zookeeperRegistry.setRegProtocol("zookeeper");
    zookeeperRegistry.setAddress("127.0.0.1:2181");
    motanDemoService.setRegistry(zookeeperRegistry);
    // 配置RPC协议
    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setId("motan");
    protocol.setName("motan");
    motanDemoService.setProtocol(protocol);
    motanDemoService.setExport("motan:8002");
    motanDemoService.export();
    MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
    System.out.println("server start...");
}
Also used : RegistryConfig(com.weibo.api.motan.config.RegistryConfig) MotanDemoService(com.weibo.motan.demo.service.MotanDemoService) ServiceConfig(com.weibo.api.motan.config.ServiceConfig) ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig)

Example 2 with RegistryConfig

use of com.weibo.api.motan.config.RegistryConfig 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 3 with RegistryConfig

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

the class SpringSupportTest method testRegistryConfig.

@Test
public void testRegistryConfig() {
    Map<String, RegistryConfig> map = cp.getBeansOfType(RegistryConfig.class);
    assertEquals(2, map.size());
    RegistryConfig local = map.get("myLocal");
    RegistryConfig mockRegistry = map.get("mockRegistry");
    assertTrue(local != null && mockRegistry != null);
}
Also used : RegistryConfig(com.weibo.api.motan.config.RegistryConfig) Test(org.junit.Test)

Example 4 with RegistryConfig

use of com.weibo.api.motan.config.RegistryConfig 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 5 with RegistryConfig

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

the class MotanFrameworkUtil method getDefaultRegistryConfig.

/**
     * 默认本地注册中心
     * @return local registry
     */
public static RegistryConfig getDefaultRegistryConfig() {
    RegistryConfig local = new RegistryConfig();
    local.setRegProtocol("local");
    return local;
}
Also used : RegistryConfig(com.weibo.api.motan.config.RegistryConfig)

Aggregations

RegistryConfig (com.weibo.api.motan.config.RegistryConfig)9 ProtocolConfig (com.weibo.api.motan.config.ProtocolConfig)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 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1