Dubbo入门


dubbo结构图:
这里写图片描述


dubbo管理控制台安装(ubuntu)

  • 必须使用jdk1.7(其他版本不行),否则报错

  • 安装zookeeper(单节点)之后

在zookeeper根目录新建两个文件夹
mkdir data
mkdir logs
cd zookeeper-3.4.6/conf
cp zoo_sample.cfg zoo.cfg
  • 修改zoo.cfg配置文件内容
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/home/xialonglei/zookeeper-3.4.6/data
dataLogDir=/home/xialonglei/zookeeper-3.4.6/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=xialonglei:2888:3888
  • 启动zookeeper
./zkServer.sh start

这里写图片描述
表明zkServer启动成功!

放到webapps下载
解压unzip duubo-admin.war -d ROOT
http://127.0.01:8080

这里写图片描述
表明dubbo管理控制台部署成功!


dubbo监控中心的安装

 - 解压
 - cd 监控中心的conf文件夹下
 - vim dubbo.properties(屏蔽掉广播地址,打开zookeeper注册中心地址,修改dubbo.jetty.port=9090)
 - cd 监控中心的bin目录下
 - ./start.sh
 - 最好不要将zookeeper服务与监控中心部署在同一台机器上,避免机器挂掉后,注册中心和监控中心都挂掉了

dubbo与spring整合

  • 所需要的依赖
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.5</version>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    </dependency>
</dependencies>
  • provider.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://code.alibabatech.com/schema/dubbo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <!-- 提供方应用信息,用于计算机依赖关系 -->
    <dubbo:application name="provider_1"/>

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> -->
    <!-- 使用zookeeper注册中心暴露服务地址 即zookeeper的所在服务器ip地址和端口号 -->
    <dubbo:registry address="zookeeper://192.168.2.121:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <!-- serializtion 协议序列方式,当协议支持多种序列化方式时使用,默认hessian2
        比如:dubbo协议的dubbo,hessian2,java,compactedjava,以及http协议的json等
    -->
    <dubbo:protocol name="dubbo" port="20880" serialization="json"/>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.xll.dubbo.DemoService" ref="demoService"/>

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.xll.dubbo.provider.DemoServiceImpl"/>

</beans>
  • consumer.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://code.alibabatech.com/schema/dubbo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <!-- 提供方应用信息,用于计算机依赖关系 -->
    <dubbo:application name="consumer_1"/>

    <!-- 使用zookeeper注册中心暴露服务地址 即zookeeper的所在服务器ip地址和端口号 -->
    <dubbo:registry address="zookeeper://192.168.2.121:2181" />

    <dubbo:reference id="demoService" interface="com.xll.dubbo.DemoService"/>

</beans>
  • 目录结构(intellij idea maven)

这里写图片描述

  • dubbo-provider代码
1. DemoService(接口)代码:
package com.xll.dubbo;

/**
 * Created by lonely.xia on 2017/9/18.
 */
public interface DemoService {
    String sayHello(String name);
}

2. DemoServiceImpl.java代码:
package com.xll.dubbo.provider;
import com.xll.dubbo.DemoService;

/**
 * Created by lonely.xia on 2017/9/18.
 */
public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        return "name->" + name;
    }
}

3. ProviderMain.java代码
package com.xll.dubbo.provider;
import com.xll.dubbo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;

/**
 * Created by lonely.xia on 2017/9/18.
 */
public class ProviderMain {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext classPathXmlApplicationContext
                = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
        classPathXmlApplicationContext.start();
        System.in.read();   // 按任意键退出

    }
}
  • dubbo-consumer代码
1. DemoService(接口)代码
package com.xll.dubbo;

/**
 * Created by lonely.xia on 2017/9/18.
 */
public interface DemoService {
    String sayHello(String name);
}

2. ConsumerMain.java代码
package com.xll.dubbo.consumer;
import com.xll.dubbo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;

/**
 * Created by lonely.xia on 2017/9/18.
 */
public class ConsumerMain {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext classPathXmlApplicationContext
                = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
        classPathXmlApplicationContext.start();
        DemoService demoService = (DemoService) classPathXmlApplicationContext.getBean("demoService");
        String hello = demoService.sayHello("xialonglei");
        System.out.println("RPC调用结果:" + hello);
        System.in.read();   // 按任意键退出
    }
}
  • 启动服务者和消费者

dubbo过滤器的实现

  • 在服务者中新建一个类
package com.xll.dubbo.filter;
import com.alibaba.dubbo.rpc.*;
/**
 * Created by lonely.xia on 2017/9/27.
*/
public class IpFilter implements Filter {
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String ip = RpcContext.getContext().getRemoteHost();
        //如果是192.168.148.1机器发起的请求,则可以执行服务调用
        if (ip.equals("192.168.148.1")) {
            return invoker.invoke(invocation);
        } else {
            return new RpcResult();
        }
    }
}
  • 在resources文件夹中新建文件META-INF/dubbo/com.alibaba.dubbo.rpc.Filter(文件名必需为这个),文件内容如下
#name=value(name随意起,但value为过滤类路径)
ipFilter=com.xll.dubbo.filter.IpFilter
  • 结束

dubbo的文件上传和下载

  • 新建file-facade服务接口模块(这样就可以使服务提供者和消费者共同依赖该模块)

这里写图片描述

  • 新建file-service服务模块,作为服务提供者

这里写图片描述

  • provider.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://code.alibabatech.com/schema/dubbo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <!-- 提供方应用信息,用于计算机依赖关系 -->
    <dubbo:application name="provider_2"/>

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> -->
    <!-- 使用zookeeper注册中心暴露服务地址 即zookeeper的所在服务器ip地址和端口号 -->
    <dubbo:registry address="zookeeper://192.168.36.8:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <!-- serializtion 协议序列方式,当协议支持多种序列化方式时使用,默认hessian2
        比如:dubbo协议的dubbo,hessian2,java,compactedjava,以及http协议的json等
    -->
    <dubbo:protocol name="dubbo" port="20880" serialization="json"/>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.xll.service.IFileService" ref="fileService" protocol="hessian"/>

    <!-- 和本地bean一样实现服务 -->
    <bean id="fileService" class="com.xll.service.impl.FileServieImpl"/>

    <!-- 默认是以jetty作为服务容器,因此要下载jetty和hessian依赖 -->
    <dubbo:protocol name="hessian" port="20887"/>
</beans>
<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.51</version>
</dependency>
<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty</artifactId>
    <version>6.1.26</version>
</dependency>
  • 编写服务启动类
public class ProviderMain {
    public static void main(String[] args) throws IOException {

        ClassPathXmlApplicationContext classPathXmlApplicationContext
                = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
        classPathXmlApplicationContext.start();
        System.in.read();  // 按任意键退出

    }
}
  • 新建file-consumer消费者模块,作为服务的消费者

这里写图片描述

  • 配置pom.xml文件
<dependency>
    <groupId>com.xll</groupId>
    <artifactId>file-facade</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
  • 配置consumer.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://code.alibabatech.com/schema/dubbo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <dubbo:application name="consumer_2"/>

    <!-- 使用zookeeper注册中心暴露服务地址 即zookeeper的所在服务器ip地址和端口号 -->
    <dubbo:registry address="zookeeper://192.168.36.8:2181" />

    <dubbo:reference id="fileService" interface="com.xll.service.IFileService" timeout="120000"/>

</beans>
  • 启动服务者和消费者

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值