Dubbo实战案例介绍一

Dubbo实战案例

在Dubbo中所有的的服务调用都是基于接口去进行双方交互的。双方协定好Dubbo调用中的接口,提供者来提供实现类并且注册到注册中心上。调用方则只需要引入该接口,并且同样注册到相同的注册中心上(消费者)。即可利用注册中心来实现集群感知功能,之后消费者即可对提供者进行调用。
我们所有的项目都是基于Maven去进行创建,这样相互在引用的时候只需要以依赖的形式进行展现就可
以了。
并且这里我们会通过maven的父工程来统一依赖的版本
程序实现分为以下几步骤:

  1. 建立maven工程 并且 创建API模块: 用于规范双方接口协定
  2. 提供provider模块,引入API模块,并且对其中的服务进行实现。将其注册到注册中心上,对外来
    统一提供服务。
  3. 提供consumer模块,引入API模块,并且引入与提供者相同的注册中心。再进行服务调用。

开发过程

接口协定

  1. 定义maven。
<groupId>com.lwl</groupId> 
<artifactId>service-api</artifactId> 
<version>1.0-SNAPSHOT</version>
  1. 定义接口,这里为了方便,只是写一个基本的方法。
public interface HelloService { 
	String sayHello(String name); 
}

创建接口提供者

  1. 引入API模块。
<dependency>
 <groupId>com.lwl</groupId>
  <artifactId>service-api</artifactId> 
  <version>${project.version}</version> 
</dependency>
  1. 引入Dubbo相关依赖,这里为了方便,使用注解方式。
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-rpc-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-remoting-netty4</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-serialization-hessian2</artifactId>
        </dependency>
  1. 编写实现类。注意这里也使用了Dubbo中的 @Service 注解来声明他是一个服务的提供者。
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name){
       
        return "hello:"+name;
    }
}
  1. 编写配置文件,用于配置dubbo。比如这里我就叫 dubbo-provider.properties ,放入到
    resources 目录下。
dubbo.application.name=dubbo-demo-annotation-provider 
dubbo.protocol.name=dubbo 
dubbo.protocol.port=20880

dubbo.application.name: 当前提供者的名称
dubbo.protocol.name: 对外提供的时候使用的协议
dubbo.protocol.port: 该服务对外暴露的端口是什么,在消费者使用时,则会使用这个端口
并且使用指定的协议与提供者建立连接。

  1. 编写启动的 main 函数。这里面做的比较简单,主要要注意注解方式中的注册中心这里是使用的本机2181端口来作为注册中心。
public class DubboPureMain {
    public static void main(String[] args) throws  Exception{
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
        context.start();
        System.in.read();
    }
    @Configuration
    @EnableDubbo(scanBasePackages = "com.lwl.service.impl")
    @PropertySource("classpath:/dubbo-provider.properties")
    static  class  ProviderConfiguration{
        @Bean
        public RegistryConfig   registryConfig(){
            RegistryConfig  registryConfig  = new RegistryConfig();
            registryConfig.setAddress("zookeeper://127.0.0.1:2181?timeout=10000");
            //registryConfig.setTimeout(10000);
            return   registryConfig;
        }
    }

}

创建消费者

  1. 引入API模块。
  		<dependency>
            <groupId>com.lagou</groupId>
            <artifactId>service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  1. 引入Dubbo依赖 ,同服务提供者。
  2. 编写服务,用于真实的引用dubbo接口并使用。因为这里是示例,所以比较简单一些。这里面
    @Reference 中所指向的就是真实的第三方服务接口。
@Component
public class ComsumerComponet {
    @Reference
    private HelloService  helloService;
    public String  sayHello(String name){
        return  helloService.sayHello(name);
    }

}
  1. 编写消费者的配置文件。这里比较简单,主要就是指定了当前消费者的名称和注册中心的位置。通
    过这个注册中心地址,消费者就会注册到这里并且也可以根据这个注册中心找到真正的提供者列
dubbo.application.name=service-consumer 
dubbo.registry.address=zookeeper://127.0.0.1:2181
  1. 编写启动类,这其中就会当用户在控制台输入了一次换行后,则会发起一次请求。

public class AnnotationConsumerMain  {
    public static void main(String[] args) throws  Exception {
        System.out.println("-------------");
        AnnotationConfigApplicationContext   context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        // 获取消费者组件
        ComsumerComponet  service = context.getBean(ComsumerComponet.class);
        while(true){
             System.in.read();
             String  hello = service.sayHello("world");
             System.out.println("result:"+hello);
        }
    }
    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.lwl.bean")
    @EnableDubbo
    static  class  ConsumerConfiguration{

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时小浅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值