Dubbo(5)什么是服务暴露和服务引用?

在Dubbo中,服务暴露和服务引用是两个核心概念,分别对应服务提供者和服务消费者的操作。

1. 服务暴露(Service Exposure)

服务暴露是指服务提供者将其提供的服务注册到注册中心,使其能够被服务消费者发现和调用。

服务暴露的步骤:
  1. 定义服务接口:服务提供者和消费者都需要共享的接口。
  2. 实现服务接口:服务提供者提供具体的服务实现。
  3. 配置服务暴露:将服务实现通过Dubbo暴露出去。
代码示例
  1. 定义服务接口
package com.example;

public interface DemoService {
    String sayHello(String name);
}
  1. 实现服务接口
package com.example;

import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}
  1. 配置服务暴露

在Spring配置文件中:

<dubbo:application name="dubbo-demo-provider" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.example.DemoService" ref="demoServiceImpl" />

或者在Spring Boot配置文件中:

dubbo:
  application:
    name: dubbo-demo-provider
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
    port: 20880
  scan:
    base-packages: com.example

2. 服务引用(Service Reference)

服务引用是指服务消费者从注册中心获取服务提供者的地址信息,并通过Dubbo框架调用远程服务。

服务引用的步骤:
  1. 定义服务接口:服务提供者和消费者共享的接口。
  2. 引用远程服务:在消费者端配置服务引用。
  3. 调用远程服务:通过Dubbo框架调用远程服务。
代码示例
  1. 定义服务接口

(同上,服务接口由提供者和消费者共享)

  1. 引用远程服务

在Spring配置文件中:

<dubbo:application name="dubbo-demo-consumer" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:reference id="demoService" interface="com.example.DemoService" />

或者在Spring Boot配置文件中:

dubbo:
  application:
    name: dubbo-demo-consumer
  registry:
    address: zookeeper://127.0.0.1:2181
  scan:
    base-packages: com.example
  1. 调用远程服务
package com.example;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

@Component
public class DemoServiceConsumer {
    
    @DubboReference
    private DemoService demoService;

    public void execute() {
        String message = demoService.sayHello("World");
        System.out.println(message);
    }
}

服务暴露和服务引用的工作原理

  1. 服务暴露:服务提供者在启动时,将其提供的服务注册到注册中心(如ZooKeeper)。注册中心保存了所有服务提供者的地址信息。

  2. 服务引用:服务消费者在启动时,从注册中心订阅所需的服务列表。注册中心返回所有可用的服务提供者信息。消费者通过Dubbo框架,利用负载均衡策略选择一个服务提供者进行调用。

综合示例

下面是一个完整的示例,展示了服务暴露和服务引用的过程。

服务提供者
  1. 服务接口
package com.example;

public interface DemoService {
    String sayHello(String name);
}
  1. 服务实现
package com.example;

import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}
  1. Spring Boot配置文件
dubbo:
  application:
    name: dubbo-demo-provider
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
    port: 20880
  scan:
    base-packages: com.example
服务消费者
  1. 服务接口

(同上,服务接口由提供者和消费者共享)

  1. 引用远程服务
package com.example;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

@Component
public class DemoServiceConsumer {
    
    @DubboReference
    private DemoService demoService;

    public void execute() {
        String message = demoService.sayHello("World");
        System.out.println(message);
    }
}
  1. Spring Boot配置文件
dubbo:
  application:
    name: dubbo-demo-consumer
  registry:
    address: zookeeper://127.0.0.1:2181
  scan:
    base-packages: com.example

总结

服务暴露和服务引用是Dubbo的核心概念。服务暴露使得服务提供者可以将其服务注册到注册中心,供消费者发现和调用;服务引用使得服务消费者可以从注册中心获取服务提供者的信息,并通过Dubbo框架调用远程服务。通过上述代码示例,可以清晰地看到服务暴露和服务引用的具体操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

辞暮尔尔-烟火年年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值