zoukankan      html  css  js  c++  java
  • 第一章 第一个dubbo项目

    为了安全:服务启动的ip全部使用10.10.10.10

    版本:

    • dubbo:2.5.5

    重要的网址:

    首先从https://github.com/alibaba/dubbo下载dubbo源码到本地,我们的第一个dubbo项目就是dubbo源码中的dubbo-demo子模块。

    代码结构如下:

    其中:

    • dubbo-demo-api是提供服务接口的模块
      • 在生产中,该模块会单独打成jar包,分别被provider和consumer依赖,provider实现该接口,consumer通过该接口引用provider实现的服务
    • dubbo-demo-provider是服务提供者
    • dubbo-demo-consumer是服务消费者

    一 dubbo-demo-api

    1 package com.alibaba.dubbo.demo;
    2 
    3 public interface DemoService {
    4     String sayHello(String name);
    5 }

    只提供了一个接口。

    二 dubbo-demo-provider

    1 配置文件

    src/main/resources/META-INF/spring/dubbo-demo-provider.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
     4        xmlns="http://www.springframework.org/schema/beans"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     6        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
     7 
     8     <!-- 提供方应用信息,用于计算依赖关系 -->
     9     <dubbo:application name="demo-provider"/>
    10 
    11     <!-- 使用zookeeper注册中心,并使用curator客户端 -->
    12     <dubbo:registry protocol="zookeeper" address="10.211.55.5:2181" client="curator"/>
    13 
    14     <!-- 使用dubbo协议在20880端口暴露服务 -->
    15     <dubbo:protocol name="dubbo" port="20880"/>
    16 
    17     <!-- 和本地bean一样实现服务 -->
    18     <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
    19 
    20     <!-- 声明需要暴露的服务接口 -->
    21     <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
    22 </beans>

    配置项:

    • dubbo:application:指定应用名字,在生产中通常与artifactId相同
    • dubbo:registry:指定注册中心
      • protocol:使用的注册协议
      • address:注册中心地址
      • client:默认情况下,操作zookeeper的java客户端使用的是zkClient,这里使用curator
    • dubbo:protocol:指定服务暴露的协议和端口
    • dubbo:service:生命暴露的服务接口及其实现类

    2 provider提供服务接口实现

     1 package com.alibaba.dubbo.demo.provider;
     2 
     3 import com.alibaba.dubbo.demo.DemoService;
     4 import com.alibaba.dubbo.rpc.RpcContext;
     5 
     6 import java.text.SimpleDateFormat;
     7 import java.util.Date;
     8 
     9 public class DemoServiceImpl implements DemoService {
    10     public String sayHello(String name) {
    11         System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
    12         return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    13     }
    14 }

    3 provider启动类

     1 package com.alibaba.dubbo.demo.provider;
     2 
     3 import org.springframework.context.support.ClassPathXmlApplicationContext;
     4 
     5 public class Provider {
     6     public static void main(String[] args) throws Exception {
     7         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
     8         context.start();
     9 
    10         System.in.read(); // 按任意键退出
    11     }
    12 }

    三 dubbo-demo-consumer

    1 配置文件

    src/main/resources/META-INF/spring/dubbo-demo-consumer.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
     4        xmlns="http://www.springframework.org/schema/beans"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     6        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
     7 
     8     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
     9     <dubbo:application name="demo-consumer"/>
    10 
    11     <!-- 使用zookeeper注册中心,并使用curator客户端 -->
    12     <dubbo:registry protocol="zookeeper" address="10.211.55.5:2181" client="curator"/>
    13 
    14     <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    15     <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>
    16 </beans>

    配置项:

    • dubbo:reference:指定引用的服务
      • check:检查注册中心是否有可用的 provider

    2 consumer启动类

     1 package com.alibaba.dubbo.demo.consumer;
     2 
     3 import com.alibaba.dubbo.demo.DemoService;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Consumer {
     7     public static void main(String[] args) {
     8         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
     9         context.start();
    10 
    11         DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理
    12         String hello = demoService.sayHello("world"); // 执行远程方法
    13 
    14         System.out.println(hello); // 显示调用结果
    15     }
    16 }

     启动服务并且调用远程服务。

    四 启动服务

    1 启动provider

    启动成功后,会发现在zookeeper上创建了节点:

    /dubbo

    --/com.alibaba.dubbo.demo.DemoService

    ----/providers

    ------/dubbo://10.10.10.10:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=1393&side=provider&timestamp=1506831679007

    注意:

    • 所有dubbo相关的都会注册在“/dubbo”根节点下
    • 与当前暴露的接口服务相关的,都注册在“/dubbo/接口”下

    2 启动consumer

    启动成功后,会发现在zookeeper上创建了节点:

    /dubbo

    --/com.alibaba.dubbo.demo.DemoService

    ----/consumers

    ------/consumer://10.10.10.10/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=1434&side=consumer&timestamp=1506832498078

    之后,看到provider和consumer双方的互动输出,则表示rpc成功!

    第一个dubbo项目就结束了,dubbo-demo项目也可以查看http://dubbo.io/的例子。

  • 相关阅读:
    VisualSVN-Server windows 版安装时报错 "Service 'VisualSVN Server' failed to start. Please check VisualSVN Server log in Event Viewer for more details."
    Pytest 单元测试框架之初始化和清除环境
    Pytest 单元测试框架入门
    Python(email 邮件收发)
    Python(minidom 模块)
    Python(csv 模块)
    禅道简介
    2020年最好的WooCommerce主题
    Shopify网上开店教程(2020版)
    WooCommerce VS Magento 2020:哪个跨境电商自建站软件更好?
  • 原文地址:https://www.cnblogs.com/java-zhao/p/7616962.html
Copyright © 2011-2022 走看看