首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >搭建简单的Dubbo生产者与消费者

搭建简单的Dubbo生产者与消费者

作者头像
Melody132
发布2020-03-10 09:09:30
9110
发布2020-03-10 09:09:30
举报
文章被收录于专栏:时光笔记时光笔记

前言

Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

搭建演示

一、安装Zookeeper

安装环境为Centos系统 IP:192.168.40.129,与Windows操作基本一样

  1. 安装Java环境
yum install java-1.8.0-openjdk.x86_64 -y
  1. 安装Zookeeper
#下载zookeeper
cd /usr/local/
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz

#解压
tar -zxvf zookeeper-3.4.14.tar.gz 

#配置
cd ./zookeeper-3.4.14
mkdir data
vim conf/zoo_sample.cfg 
#修改 dataDir=/usr/local/zookeeper-3.4.14/data 
mv conf/zoo_sample.cfg conf/zoo.cfg

#启动
/usr/local/zookeeper-3.4.14/bin/zkServer.sh start
二、创建项目

开发环境为IDEA ,注意修改成正确的Maven配置

dubbo-demo

请输入图片描述
请输入图片描述

主体结构,其中producer和consumer也可以分开写,但在本例为方便而中写到了一起。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>cn.ttext.demo.dubbo</groupId>
    <artifactId>dubbo-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0</version>
    <modules>
        <module>dubbo-demo-api</module>
        <module>duboo-demo-producer</module>
        <module>duboo-demo-consumer</module>
    </modules>

    <properties>
        <spring.version>4.3.3.RELEASE</spring.version>
        <dubbo.version>2.6.7</dubbo.version>
        <netty.version>4.1.43.Final</netty.version>
        <curator.version>4.2.0</curator.version>
        <zookeeper.version>3.4.14</zookeeper.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>${netty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${curator.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>zookeeper</artifactId>
                    <groupId>org.apache.zookeeper</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <artifactId>zookeeper</artifactId>
            <groupId>org.apache.zookeeper</groupId>
            <version>${zookeeper.version}</version>
        </dependency>

        <!-- spring依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
    </dependencies>

</project>

dubbo-demo-api

请输入图片描述
请输入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>cn.ttext.demo.dubbo</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <version>1.0.0</version>
    <artifactId>dubbo-demo-api</artifactId>
</project>

IDemoService

package cn.ttext.demo.dubbo.api;

public interface IDemoService {
    String say(String val);
}

dubbo-demo-producer

请输入图片描述
请输入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>cn.ttext.demo.dubbo</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <version>1.0.0</version>
    <artifactId>dubbo-demo-producer</artifactId>

    <dependencies>
        <dependency>
            <groupId>cn.ttext.demo.dubbo</groupId>
            <artifactId>dubbo-demo-api</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

</project>

spring-dubbo-producer.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/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-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-demo-producer"/>
    <!-- 注册中心地址 -->
    <dubbo:registry address="zookeeper://192.168.40.129:2181"/>
    <!-- 服务端口 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <bean id="demoService" class="cn.ttext.demo.dubbo.api.impl.DemoServiceImpl"/>

    <dubbo:service interface="cn.ttext.demo.dubbo.api.IDemoService" ref="demoService"/>

</beans>

DemoServiceImpl

package cn.ttext.demo.dubbo.api.impl;

import cn.ttext.demo.dubbo.api.IDemoService;
import com.alibaba.dubbo.rpc.RpcContext;

public class DemoServiceImpl implements IDemoService {
    public String say(String val) {
        return RpcContext.getContext().getLocalAddress() + ":" + val;
    }
}

Main

package cn.ttext.demo.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        new ClassPathXmlApplicationContext(new String[]{"spring-dubbo-producer.xml"}).start();
        System.out.println("服务已启动");
        System.in.read();
    }
}

dubbo-demo-consumer

请输入图片描述
请输入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>cn.ttext.demo.dubbo</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <version>1.0.0</version>
    <artifactId>duboo-demo-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>cn.ttext.demo.dubbo</groupId>
            <artifactId>dubbo-demo-api</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

spring-dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/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-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-demo-producer"/>

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

    <dubbo:reference id="demoService" check="false" interface="cn.ttext.demo.dubbo.api.IDemoService"/>

</beans>

Main

package cn.ttext.demo.dubbo;

import cn.ttext.demo.dubbo.api.IDemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext(new String[]{"spring-dubbo-consumer.xml"});
        classPathXmlApplicationContext.start();

        IDemoService iDemoService = (IDemoService) classPathXmlApplicationContext.getBean("demoService");

        while (true) {
            Thread.sleep(1000);
            System.out.println(iDemoService.say("Hello!!!"));
        }
    }
}
请输入图片描述
请输入图片描述

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-11-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 搭建演示
    • 一、安装Zookeeper
      • 二、创建项目
      相关产品与服务
      负载均衡
      负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档