dubbo入门学习 五 provider端的编写

1. 新建Maven Project, 里面只有接口(dubbo-service)

  1.1 为什么这么做?

  RPC框架,不希望Consumer知道具体实现.如果实现类和接口在同一个项目中,Consumer依赖这个项目时,就会知道实现类具体实现.

2. 新建Maven Project, 写接口的实现类(dubbo-service-impl)

3. 在duboo-service-impl中配置pom.xml

  3.1 依赖接口

  3.2 依赖dubbo,去掉老版本spring

  3.3 依赖新版本spring

  3.4 依赖zookeeper客户端工具zkClient

此次采用的是老的dubbo,配置的时候需要配置spring,且在配置dubbo时需要配置<exclusions>标签,去除掉依赖的spring2.5.3版本,从新配置新的spring版本。



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dubbo.bjsxt</groupId>
  <artifactId>dubbo-service-impl</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
      <dependency>
           <groupId>com.dubbo.demo</groupId>
          <artifactId>dubbo-service</artifactId>
          <version>0.0.1-SNAPSHOT</version>
      </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.3</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
<!-- <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
</dependency> -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <!-- zkclient  当provider向zookeeper注册中心注册时,使用的工具就在这个jar包中 -->
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    </dependency>
      
  </dependencies>
</project>

从2017年九月开始,dubbo的维护开始又apache接受,现在可以使用dubbo的2.6+以上的版面,这些版本不在是依赖spring的2.5.3

如下图

4. 新建实现类,并实现接口方法.

5. 新建配置文件applicationContext-dubbo.xml,并配置

  5.1 <dubbo:application/> 给provider起名,在monitor或管理工具中区别是哪个provider

  5.2 <dubbo:registry/> 配置注册中心

    5.2.1 address:注册中心的ip和端口

    5.2.2 protocol使用哪种注册中心

  5.3 <dubbo:protocol/> 配置协议

    5.3.1 name 使用什么协议

    5.3.2 port: consumer invoke provider时的端口号

  5.4 <dubbo:service/> 注册接口

    5.4.1 ref 引用接口实现类<bean>的id值

<?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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        <!-- 给当前的provider 起名字,因为provider都是需要被注册或者监控中心知道 -->
        <dubbo:application name="dubbo-service"/>
        
        <!-- 配置注册中心,2181是注册中心的端口 ,protocol代表使用的注册中心是zookeeper-->
        <dubbo:registry address="192.168.153.128:2181" protocol="zookeeper">
            
        </dubbo:registry>
        <!-- 配置端口,代表当前的provider占用当前计算机的端口 ,name的属性表示才采用的协议是dubbo协议-->
        <dubbo:protocol name="dubbo" port="2880">
        </dubbo:protocol>
        <!-- 注册功能 -->
        <dubbo:service interface="com.bjsxt.service.DemoService" ref="demoServiceImpl"></dubbo:service>
        <!-- 注册实现类 -->
        <bean id="demoServiceImpl" class="com.bjsxt.service.impl.DemoServiceImpl"></bean>
        
        <!-- <dubbo:annotation package="com.bjsxt.service.imple"/> -->
        
        <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->

        
</beans>

6. 启动容器

  6.1 通过spring方式启动

    6.1.1 applicationContext-dubbo.xml位置没有要求

ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-dubbo.xml");
        ac.start();
        System.out.println("启动成功");
        System.in.read();

  6.2 使用dubbo提供的方式启动(推荐使用这种方式)

    6.2.1 要求applicationContext-dubbo.xml必须放入类路径下/META-INF/spring/*.xml(在做实验时,返现dubbo配置文件只能放在src/main/resources下)

package com.bjsxt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.container.Main;

public class Demo {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext-dubbo.xml");
        /*ac.start();
        System.out.println("启动成功");*/
        
        //官方推荐
        //配置文件放在src/main/resources/META-INF/spring/目录下
        Main.main(args);
    }
}
原文地址:https://www.cnblogs.com/cplinux/p/9904083.html