dubbo 简单教程

留个记录,项目能跑起来就行。。。。要啥自行车,以后慢慢精进

一、启动 zookeeper

        zookeeper-3.7.0:官方下载页官方下载资源

  1.  conf/zoo_sample.cfg 复制为 zoo.cfg
  2. zoo.cfg文件中定义端口号:clientPort=2181
  3. 启动bin/zkServer.cmd  

二、启动 dubbo-admin

        github资源

  1. 下载dubbo-admin-master文件夹导入idea
  2. 配置maven      
    1. File——》Settings——》maven——》Maven home path选择Bundled (Maven 3)
  3. 进入dubbo-admin\src\main\resources打开application.properties
  4. 配置登录端口号:server.port=7001
  5. 配置zookeeper地址:dubbo.registry.address=zookeeper://127.0.0.1:2181
  6. 运行dubbo-admin\src\main\java\com\alibaba\dubboadmin\DubboAdminApplication.java
  7. 登录:http://localhost:7001/     默认账号密码:root,root

三。创建示例

        1.创建空项目

        2.创建maven项目 :

        api 项目:

                存放公用接口:   bean,  service,impl

                业务1项目:        

  1. pom.xml 引入api项目,dubbo,zookeeper包
    <properties>
        <dubbo.version>2.7.8</dubbo.version>
    </properties>
    
    <dependency>
        <groupId></groupId>
        <artifactId>xxApi</artifactId>
        <version>${project.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-dependencies-zookeeper</artifactId>
        <version>${dubbo.version}</version>
        <type>pom</type>
    </dependency>
  2. 新建:src\main\resources\业务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:context="http://www.springframework.org/schema/context"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.3.xsd
           http://dubbo.apache.org/schema/dubbo
           http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!--包扫描路径-->
        <context:component-scan base-package="cn.zwy"></context:component-scan>
        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
        <dubbo:application name="xx项目名"  />
        <!-- 使用multicast广播注册中心暴露发现服务地址 -->
        <dubbo:registry address="zookeeper://192.168.31.147:2181?client=curator" />
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="xxService" interface="service路径.xxRpcService" />
    </beans>
  3. 创建service
    public interface MeetingService {Meeting selectByCode(String code);}
    
  4. 创建impl
    @Service
    public class MeetingServiceImpl implements MeetingService {
        @Reference
        private UserRpcService userRpcService;
        public Meeting selectByCode(String code) {
            User user = userRpcService.selectById(id);
            return meeting;
        }
    }
  5. 配置启动文件
    public class App {
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
            context.start();
            System.in.read(); // 按任意键退出
        }
    }
         

  业务2项目:

  1.  pom.xml  引入api项目,dubbo,zookeeper包,同业务1
  2. 新建:src\main\resources\业务2.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://dubbo.apache.org/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="项目2名"  />
        <!-- 使用multicast广播注册中心暴露服务地址 -->
        <dubbo:registry address="zookeeper://192.168.31.147:2181?client=curator" />
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="service包路径.XXService" ref="XXService" />
        <!-- 和本地bean一样实现服务 -->
        <bean id="xxService" class="impl路径.XXImpl" />
    </beans>
  3. 创建impl
    public class xxServiceImpl implements xxService {/*service创建在api内*/
         public User selectById(String id) {return null}
    }
  4. 配置启动文件
    public class App {
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
            context.start();
            System.in.read(); // 按任意键退出
        }
    }

总结

        zookeeper 集群管理,dubbo调度(分配)资源(集群中的访问接口)。

        业务1项目,业务2项目分别将地址和端口号注册到dubbo,由dubbo安排生产者的请求,分配业务所在的服务器

        业务1项目,业务2项目之间service层之间的互动,或者公用API交由API项目进行管理

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤独白鲸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值