美文网首页
Dubbo用户使用总结(一)

Dubbo用户使用总结(一)

作者: 柏丁 | 来源:发表于2020-05-30 21:26 被阅读0次

    简单Demo的快速启动

    Dubbo采用全Spring的配置方式,对应用没有任何API的侵入,需要Spring加载Dubbo的配置(基于Schema扩展进行加载具体Schema的扩展)。
    dubbo的两种配置方式:

    Spring配置(主要采用这种模式)

    1. 服务提供者的接口:
    public interface DemoService {
        String sayHello(String name);
    }
    
    1. 服务提供者的接口实现:
    public class DemoServiceImpl implements DemoService {
        public String sayHello(String name) {
            System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
            return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
        }
    }
    

    其中返回值中的RpcContext类中的各种方法会在下文中的“上下文信息”中具体介绍。

    1. 服务提供者通过Spring XML配置暴露服务:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="hello-world-app"  />
        <!-- 使用multicast广播注册中心暴露服务地址 -->
        <dubbo:registry address="multicast://224.5.6.7:1234" />
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
        <!-- 和本地bean一样实现服务 -->
        <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
    </beans>
    
    1. 服务提供者的配置加载,启动:
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                   new String[] { "META-INF/spring/dubbo-demo-provider.xml" });
           context.start();
           System.in.read(); 
    

    该类是加载Spring配置,并启动provider。具体XML路径根据项目而定。

    1. 服务消费者的xml配置引用服务:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
        <dubbo:application name="consumer-of-helloworld-app"  />
        <!-- 使用multicast广播注册中心暴露发现服务地址 -->
        <dubbo:registry address="multicast://224.5.6.7:1234" />
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />
    </beans>
    
    1. 服务消费者的服务加载:
    public class Consumer {
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/consumer.xml"});
            context.start();
            DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
            String hello = demoService.sayHello("world"); // 执行远程方法
            System.out.println( hello ); // 显示调用结果
        }
    }
    
    1. 配置之间的优先加载顺序

    [图片上传失败...(image-847139-1590845170673)]

    API方式配置

    1. 一般场景
    • 服务提供者
    import com.alibaba.dubbo.rpc.config.ApplicationConfig;
    import com.alibaba.dubbo.rpc.config.RegistryConfig;
    import com.alibaba.dubbo.rpc.config.ProviderConfig;
    import com.alibaba.dubbo.rpc.config.ServiceConfig;
    import com.xxx.XxxService;
    import com.xxx.XxxServiceImpl;
    // 服务实现
    XxxService xxxService = new XxxServiceImpl();
    // 当前应用配置
    ApplicationConfig application = new ApplicationConfig();
    application.setName("xxx");
    // 连接注册中心配置
    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("10.20.130.230:9090");
    registry.setUsername("aaa");
    registry.setPassword("bbb");
    // 服务提供者协议配置
    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setName("dubbo");
    protocol.setPort(12345);
    protocol.setThreads(200);
    // 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
    // 服务提供者暴露服务配置
    ServiceConfig<XxxService> service = new ServiceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
    service.setApplication(application);
    service.setRegistry(registry); // 多个注册中心可以用setRegistries()
    service.setProtocol(protocol); // 多个协议可以用setProtocols()
    service.setInterface(XxxService.class);
    service.setRef(xxxService);
    service.setVersion("1.0.0");
    // 暴露及注册服务
    service.export();
    
    • 服务消费者
    import com.alibaba.dubbo.rpc.config.ApplicationConfig;
    import com.alibaba.dubbo.rpc.config.RegistryConfig;
    import com.alibaba.dubbo.rpc.config.ConsumerConfig;
    import com.alibaba.dubbo.rpc.config.ReferenceConfig;
    import com.xxx.XxxService;
    // 当前应用配置
    ApplicationConfig application = new ApplicationConfig();
    application.setName("yyy");
    // 连接注册中心配置
    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("10.20.130.230:9090");
    registry.setUsername("aaa");
    registry.setPassword("bbb");
    // 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
    // 引用远程服务
    ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
    reference.setApplication(application);
    reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
    reference.setInterface(XxxService.class);
    reference.setVersion("1.0.0");
    // 和本地bean一样使用xxxService
    XxxService xxxService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
    
    1. 方法级设置
    // 方法级配置
    List<MethodConfig> methods = new ArrayList<MethodConfig>();
    MethodConfig method = new MethodConfig();
    method.setName("createXxx");
    method.setTimeout(10000);
    method.setRetries(0);
    methods.add(method);
    // 引用远程服务
    reference.setMethods(methods); // 设置方法级配置
    
    1. 点对点直连
    ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
    // 如果点对点直连,可以用reference.setUrl()指定目标地址,设置url后将绕过注册中心,
    // 其中,协议对应provider.setProtocol()的值,端口对应provider.setPort()的值,
    // 路径对应service.setPath()的值,如果未设置path,缺省path为接口名
    reference.setUrl("dubbo://10.20.130.230:20880/com.xxx.XxxService"); 
    
    

    用maven引用com.alibaba.dubbo创建的项目地址如下:

    相关文章

      网友评论

          本文标题:Dubbo用户使用总结(一)

          本文链接:https://www.haomeiwen.com/subject/cazhzhtx.html