Dubbo+ZooKeeper+Spring快速开始

做一遍是了解一个事物最快的方法,此处我们就尝试一下dubbo。

什么是Dubbo?

Dubbo是一个高性能的,基于JAVA的,开源的分布式RPC框架。

Dubbo特点:

1.透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
2.软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
3.服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者

另外,Dubbo可以采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

单机模式安装zookeeper

下载地址: http://www.apache.org/dyn/closer.cgi/zookeeper/,本文下载版本zookeeper-3.4.10.tar.gz,然后解压,修改conf文件夹下的配置文件。
将zoo_sample.cfg 改名为 zoo.cfg,因为 Zookeeper在启动时会找这个文件作为默认配置文件。

配置文件如下,可能需要根据实际情况修改:

# 客户端与服务器之间维持心跳的时间间隔
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# Zookeeper保存数据和日志的目录,需修改成自己的!
dataDir=E:\zookeeper-3.4.10\data
# 客户端连接Zookeeper 服务器的端口
clientPort=2181

启动:
bin目录下运行 zkServer.cmd 即可

使用例子

代码地址:https://gitee.com/xuea/alltest.git 下的dubboproviderdubboconsumer,一个服务提供者,一个服务消费者。

服务提供者

主要配置文件spring-dubbo.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:util="http://www.springframework.org/schema/util"
    xmlns:dubbo="http://code.alibabatech.com/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/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

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

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry id="zk_registry" address="zookeeper://127.0.0.1:2181" />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20098" />

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

</beans>

服务接口及实现

package com.leo.service;

import com.leo.model.User;

public interface DemoService {

    public String sayHello(String name);

    public User findUserById(long id);
}
package com.leo.service;

import com.leo.model.User;
import org.springframework.stereotype.Service;

@Service("demoService")
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }

    @Override
    public User findUserById(long id) {

        User user = new User();
        user.setId(id);
        user.setName("Ricky");
        user.setAge(26);

        return user;
    }

}

服务启动

package com.leo.main;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Dubbo main
 *
 */
public class App {

    public static void main(String[] args) {

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

        System.out.println("Dubbo main start...");

        try {
            System.in.read();   // 按任意键退出
        } catch (IOException e) {
            e.printStackTrace();
        } 

    }
}

服务消费者

主要配置文件

<?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:util="http://www.springframework.org/schema/util"
    xmlns:dubbo="http://code.alibabatech.com/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/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo-consumer"  />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!--<dubbo:registry address="zookeeper://127.0.0.1:2181" />-->

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="com.leo.service.DemoService" />

</beans>

service接口同服务提供方一致

消费者启动

package com.leo.main;

import com.leo.model.User;
import com.leo.service.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Dubbo Consumer client
 *
 */
public class App {

    public static void main(String[] args) {

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

        DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理
        String hello = demoService.sayHello("ricky"); // 执行远程方法
        System.out.println(hello); // 显示调用结果

        User user = demoService.findUserById(15);
        System.out.println(user); // 显示调用结果

    }
}

先启动服务提供者,再启动消费者即可看到运行状况。

运行遇到的问题:

Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘demoService’: FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.leo.service.DemoService. No provider available for the service com.leo.service.DemoService from the url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?application=dubbo-consumer&dubbo=2.5.3&interface=com.leo.service.DemoService&methods=findUserById,sayHello&pid=928&side=consumer&timestamp=1508224149384 to the consumer 10.240.128.222 use dubbo version 2.5.3

后来发现provider的提供端口没有开启,参考下面这个页面开启下20098端口即可。
https://jingyan.baidu.com/article/67508eb4dc85e79cca1ce48f.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值