Dubbo详解(二)配置和使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/muyi_amen/article/details/79818544

架构演变过程

dubbo-17071-20160628000111859-650529644.jpg

什么是RPC?

RPC(Remote Procedure Call Protocol)远程过程调用协议。服务器A调用服务器B上的方法的一种技术。Dubbo就是一个RPC框架,实现了远程过程调用。

Dubbo的原理图

image

dubbo主要的三个要素:
1、接口的远程调用
2、负载均衡。
3、服务自动注册和发现

Dubbo的使用

1、说明

Dubbo框架需要有注册中心,本案例中使用Redis作为Dubbo的注册中心。除了Redis外,Zookeeper等也可以作为Dubbo的注册中心。

2、环境要求

JDK 1.6以上。

3、添加依赖

<dependencies>
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.5</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
    </dependencies>

4、定义Dubbo服务接口

其实就是创建一个独立的module(并且在pom.xml导入以上依赖),在module中创建一些接口和方法(也叫服务)。
比如在dubbo_service_interface中创建一个接口IDubboService,代码如下:

package com.javen.dubbo.service;

import java.util.List;

/**
 * Dubbo的服务
 * @author yangjw 
 */
public interface IDubboService {

    List<String> getData(String data);

}

5、定义服务提供者(接口具体实现者)

创建另外一个module,命名dubbo_provider1(并且在pom.xml导入以上依赖)。将dubbo_service_interface作为依赖添加进来。

1、在其中创建类DubboService,并且继承IDubboService接口。代码如下:

package com.javen.dubbo.provider;

import com.javen.dubbo.service.IDubboService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务提供者
 * @author yangjw 
 */
@Component("dubboService")
public class DubboService implements IDubboService{


    public List<String> getData(String data) {

        ArrayList<String> list = new ArrayList<String>();
        list.add("这是Dubbo中Provider返回的数据:" + data);
        return list;
    }
}

2、配置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: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">

    <context:annotation-config/>
    <context:component-scan base-package="com.javen.dubbo.provider"/>
    <!--配置dubbo服务的唯一名称 -->
    <dubbo:application name="dubbo_provider1"/>
    <!--将服务注册到redis中,并且配置协议和端口为20880-->
    <dubbo:registry address="redis://192.168.72.188:6379"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--配置服务接口,ref关联到服务实现类-->
    <dubbo:service interface="com.javen.dubbo.service.IDubboService" ref="dubboService"/>

</beans>

3、启动Provider,代码如下:

package com.javen.dubbo.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class StartProvider {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"spring-dubbo.xml"});
        System.out.println("服务1启动~~~");
        context.start();
        //线程阻塞:保证服务一直存在,如果线程结束,服务终止
        System.in.read(); // press any key to exit
    }
}

6、定义服务消费者(接口的具体调用者)

再创建一个module,命名dubbo_consumer(并且在pom.xml导入以上依赖)。将dubbo_service_interface作为依赖添加进来。

1、配置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: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="demo-consumer"/>
    <dubbo:registry address="redis://192.168.72.188:6379"/>
    <dubbo:reference id="demoService" interface="com.javen.dubbo.service.IDubboService"/>
</beans>

2、测试

package com.test;

import com.javen.dubbo.service.IDubboService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-dubbo.xml")
public class DubboTest {

    @Resource(name = "demoService")
    private IDubboService dubboService;
    @Test
    public void testDubbo(){
        List<String> haha = dubboService.getData("haha");
        System.out.println(haha.get(0));
    }
}

猜你喜欢

转载自blog.csdn.net/muyi_amen/article/details/79818544
今日推荐