Dubbo入门示例

需求:a作为服务提供方,被b服务消费者调用。

项目规划

  • c-common:将公共的实体类、接口放入该模块下。
  • a-provider:服务提供者。
  • b-consumer:服务消费者。

引入依赖

c-common下的pom.xml中,加入如下依赖:

<dependencies>

        <!-- 引入dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>

        <!-- 使用zookeeper作为注册中心
        dubbo 2.6以前的版本引入zkclient操作zookeeper
        dubbo 2.6及以后的版本引入curator操作zookeeper
        下面两个zk客户端根据dubbo版本2选1即可
        -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
       
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>

同时,a-providerb-consumer两个模块引入c-common

<dependencies>
	<!--引入公共模块-->
	<dependency>
	    <groupId>org.yun</groupId>
	    <artifactId>c-common</artifactId>
	    <version>1.0-SNAPSHOT</version>
	</dependency>
</dependencies>

开发c-common

很简单,就是定义服务提供者与服务消费者的接口。

/**
 * 服务提供接口
 */
public interface AService {
    void provider();
}
/**
 * 服务消费接口
 */
public interface BService {
    void consumer();
}

开发a-provider

a作为服务提供者要实现AService接口。

import com.alibaba.dubbo.config.annotation.Service;
import org.yun.common.service.AService;
/**
 * 注意:这个是dubbo包下的@Service
 * 表示对外暴露的服务
 */
@Service
public class AServiceImpl implements AService {
    @Override
    public void provider() {
        System.out.println("a-provider");
        System.out.println("我被b-consumer调用了...");
    }
}

provider.xml中,配置具体的服务。

<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:application name="a-provider"/>
    <!--指定服务的注册地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--指定服务注册的通信协议与端口号-->
    <dubbo:protocol name="dubbo" port="20890"/>

    <!-- 要扫描dubbo服务提供者实现类的包 -->
    <dubbo:annotation package="org.yun.provider.service.impl"/>

</beans>

编写服务提供者启动类

public class ProviderMain {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("provider.xml");
        ctx.start();
        System.out.println("服务启动了...");
        System.in.read();
    }
}

启动服务,看到..

注意:因为使用Zookeeper作为注册中心,所以要先启动Zookeeper服务,然后再启动 a-provider 。

至此,服务提供者开发完毕。接下来,开发服务消费者。

开发b-consumer

b作为服务消费者要实现BService接口。

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
import org.yun.common.service.AService;
import org.yun.common.service.BService;

//也可以是@Service(是Spring下的)。表示被Spring容器管理的组件。
@Component  
public class BServiceImpl implements BService {

    //要引用的远程服务	
    @Reference  //该注解是Dubbo下的,表示要调用的远程服务
    private AService aService;

    @Override
    public void consumer() {
        aService.provider();//调用远程服务
        System.out.println("b-consumer");
        System.out.println("我消费了a-provider服务...");
    }
}

consumer.xml中,配置具体的服务。

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       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://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

     <!--指定服务应用的名称-->
    <dubbo:application name="b-consumer"/>
    <!--指定服务的注册地址。要与服务提供者的地址一致。-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 要扫描dubbo服务消费者实现类的包 -->
    <dubbo:annotation package="org.yun.consumer.service.impl"/>

    <!-- 指定spring要扫描的包 -->
    <context:component-scan base-package="org.yun.consumer.service.impl"/>

</beans>

编写服务消费者启动类

public class ConsumerMain {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("consumer.xml");
        ctx.start();//启动服务
        BService bService = ctx.getBean(BService.class);
        bService.consumer(); //服务消费者,消费服务
        System.in.read();
    }
}

启动服务,看到..

转载于:https://my.oschina.net/mondayer/blog/3028984

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值