简易入门的Dubbo实例-自己建立的

 

使用maven工程创建

先创建接口端api,再创建服务提供端provider,最后创建服务消费端consumer

 

一、创建接口工程API

1、创建maven工程,选择quickstart选项,之后groupid取名为com.excel,代表公司组织名称,artifact取名为dubboapi,代表工程的名字:

 

2、建立接口和实体类

接口类定义如下:

package com.excel.dubboapi.intf;

import java.util.List;

import com.excel.dubboapi.model.Product;

public interface IProductService {

    public List<Product> listAllProducts();

    public Product getProductById(int id);

}

 

实体类定义如下:

package com.excel.dubboapi.model;

import java.io.Serializable;

public class Product implements Serializable{

    private static final long serialVersionUID = -4177152068720868250L;

    private int id;

    private String code;

    private String name;

    private String remark;

}

自行补齐gettter、setter方法。

到此完成了接口端的创建,接口类不需要依赖任何jar包。

 

 

二、创建服务提供端Provider

1、创建maven工程,选择quickstart选项,之后groupid取名为com.excel,代表公司组织名称,artifact取名为dubboprovider,代表工程的名字:

截图省略。。。。仿照前面的创建过程即可。

 

2、修改pom.xml文件

设定需要的jar包,pom.xml文件内容如下:

<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.excel</groupId>

    <artifactId>dubboprovider</artifactId>

    <version>1.0.0</version>

    <packaging>jar</packaging>

    <name>dubboprovider</name>

    <url>http://maven.apache.org</url>

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

    <dependencies>

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>dubbo</artifactId>

            <version>2.6.2</version>

        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->

        <dependency>

            <groupId>org.apache.zookeeper</groupId>

            <artifactId>zookeeper</artifactId>

            <version>3.4.13</version>

        </dependency>

        <!-- need curator,if not,it will case exception: java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy -->

        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->

        <dependency>

            <groupId>org.apache.curator</groupId>

            <artifactId>curator-recipes</artifactId>

            <version>4.0.1</version>

        </dependency>

        <dependency>

            <groupId>com.excel</groupId>

            <artifactId>dubboapi</artifactId>

            <version>1.0.0</version>

        </dependency>

    </dependencies>

</project>

在provider端,必须要有dubbo、zookeeper、curator-recipes这3个jar包。

由于要实现api,则需要加入dubboapi的包,也就是前面的接口端的工程。

 

3、创建接口实现类

3.1src/main/java目录下,创建com.excel.dubboprovider.main包,用于存放启动dubbo容器的Main函数,具体如下:

package com.excel.dubboprovider.main;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderMain {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:prodiver-zookeeper.xml");

        app.start();

        System.out.println("Provider Server Started!");

        try {

            System.out.println("Input Anything will stop the Provider Server");

            System.in.read();

            app.close();

            System.out.println("Provider Server Stoped!");

        } catch (IOException e) {

            e.printStackTrace();

        }

       

    }

}

 

 

3.2src/main/java目录下,创建com.excel.dubboprovider.service.impl包,用于存放实现接口方法的实现类,具体如下:

package com.excel.dubboprovider.service.impl;

 

import java.util.ArrayList;

import java.util.List;

 

import com.excel.dubboapi.intf.IProductService;

import com.excel.dubboapi.model.Product;

 

public class ProductServiceImpl implements IProductService{

 

    public List<Product> listAllProducts() {

       

        List<Product> list = new ArrayList<>();

        for (int i = 0; i < 100; i++) {

            Product product = new Product();

            product.setId(100+i);

            product.setCode("P"+i);

            product.setName("Product"+i);

            product.setRemark("Remark Product"+i);

            list.add(product);

        }

        return list;

    }

 

    public Product getProductById(int id) {

        Product product = new Product();

        product.setId(100+id);

        product.setCode("P"+id);

        product.setName("Product"+id);

        product.setRemark("Remark Product"+id);

        return product;

    }

}

 

 

 

 

4、设定dubbo的配置

4.1src/main/resources目录下,创建prodiver-zookeeper.xml,用于设定dubbo的服务提供端的配置信息,具体如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"

       xmlns="http://www.springframework.org/schema/beans"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

 

    <!-- provider's application name, used for tracing dependency relationship -->

    <dubbo:application name="dubboprovider" />

 

    <!-- use zookeeper registry center to export service -->

    <dubbo:registry protocol="zookeeper" address="10.4.2.206:2181"/>

 

    <!-- service implementation, as same as regular local bean -->

    <bean id="demoService" class="com.excel.dubboprovider.service.impl.ProductServiceImpl"/>

 

    <!-- declare the service interface to be exported -->

    <dubbo:service interface="com.excel.dubboapi.intf.IProductService" ref="demoService"/>

 

</beans>

 

期间可能需要调整一下JDK的编译版本,这里使用了jdk8.

到这里,就完成了服务提供端Provider的开发。

 

在启动服务提供端Provider之前,需要先启动zookeeper服务,即在D:\zookeeper-3.4.13\bin目录下,双击zkServer.cmd文件运行。如果还没有zookeeper,请先自行下载并解压。

 

 

在ProviderMain类,点击右键开始运行即可,得出控制台的信息如下:

不要关闭,让它保持启动状态。

 

此时再到zookeeper的客户端看服务是否注册上去。

到D:\zookeeper-3.4.13\bin目录下,双击zkCli.cmd文件运行,然后输入ls /,得出界面如下:

 

再输入ls /dubbo,得出如下界面:

可以看到我们的接口已经存在了,说明启动成功了。

 

 

 

三、创建消费服务端Consumer

1、创建maven工程,选择quickstart选项,之后groupid取名为com.excel,代表公司组织名称,artifact取名为dubboconsumer,代表工程的名字:

截图省略。。。。仿照前面的创建过程即可。

 

2、修改pom.xml文件

设定需要的jar包,pom.xml文件内容如下:

<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.excel</groupId>

    <artifactId>consumer</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

 

    <name>consumer</name>

    <url>http://maven.apache.org</url>

 

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

 

    <dependencies>

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>dubbo</artifactId>

            <version>2.6.2</version>

        </dependency>

 

        <!-- need curator,if not,it will case exception: java.lang.NoClassDefFoundError:

            org/apache/curator/RetryPolicy -->

        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->

        <dependency>

            <groupId>org.apache.curator</groupId>

            <artifactId>curator-recipes</artifactId>

            <version>4.0.1</version>

        </dependency>

 

        <dependency>

            <groupId>com.excel</groupId>

            <artifactId>dubboapi</artifactId>

            <version>1.0.0</version>

        </dependency>

    </dependencies>

</project>

 

 

3、创建接口实现类

3.1src/main/java目录下,创建com.excel.dubboconsumer.main包,用于存放启动dubbo容器的Main函数,具体如下:

package com.excel.consumer;

 

import java.util.List;

 

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.excel.dubboapi.intf.IProductService;

import com.excel.dubboapi.model.Product;

 

public class ConsumerMain {

   

    public static void main(String[] args) {

        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:consumer_zookeeper.xml"); 

        context.start(); 

        System.out.println("dubbo consumer start here"); 

       

       

        IProductService customerService =  (IProductService) context.getBean("consumerService"); 

        List<Product> list = customerService.listAllProducts();

       

        System.out.println("Dubbo consumer//Dubbo服务消费端运行"); 

        System.out.println("=======================================================");

        if (list.isEmpty()) {

        System.out.println("customer list empty ");

        } else {

            int idx = 1;

            for (Product customer : list) {

                System.out.println("customer: {}"+idx);

                System.out.println("customer name: {}"+ customer.getName());

                System.out.println("customer code: {}"+ customer.getCode());

                System.out.println("customer Remark: {}"+ customer.getRemark());

                System.out.println("customer Id: {}"+ customer.getId());

                System.out.println("");

                idx++;

            }

        }

        System.out.println("=======================================================");

        context.close();

    }

}

 

 

 

4、设定dubbo的配置

4.1src/main/resources目录下,创建consumer-zookeeper.xml,用于设定dubbo的服务消费端的配置信息,具体如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"

       xmlns="http://www.springframework.org/schema/beans"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

        

    <!--定义消费方应用信息,用于计算依赖关系;在 dubbo-admin dubbo-monitor 会显示该名字,方便识别--> 

    <dubbo:application name="dubboConsumer" owner="programmer" organization="dubbox"/> 

   

    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper,配置见conf文件夹下,默认zoo.cfg--> 

    <dubbo:registry address="zookeeper://localhost:2181"/>

   

    <!-- generate proxy for the remote service, then demoService can be used in the same way as the

    local regular interface -->

    <dubbo:reference id="consumerService" check="false" interface="com.excel.dubboapi.intf.IProductService"/>

   

</beans>

 

到此,消费端的代码也开发完毕。

 

ConsumerMain类里,点击右键,运行,可以看到运行结果,如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值