Dubbo(17)如何实现Dubbo的泛化调用?

Dubbo的泛化调用(Generic Invocation)是指在不知道具体服务接口的情况下,通过通用的方式调用服务。泛化调用主要用于服务测试、网关等场景,可以极大地提高系统的灵活性和可扩展性。

Dubbo泛化调用的实现

Dubbo提供了GenericService接口,用于实现泛化调用。具体步骤如下:

  1. 定义服务接口和实现:定义一个普通的服务接口和其实现。
  2. 暴露服务:通过Dubbo配置将服务暴露出去。
  3. 使用泛化调用:在消费者端使用GenericService进行服务调用。

1. 服务定义和实现

定义一个普通的服务接口DemoService

package com.example;

public interface DemoService {
    String sayHello(String name);
}

实现服务接口,并通过Dubbo注解将其暴露为远程服务:

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;
    }
}

Spring Boot配置文件(application.yml):

server:
  port: 8081

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. 服务消费者使用泛化调用

在消费者端,通过GenericService进行泛化调用。

服务消费者:

package com.example;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.stereotype.Component;

@Component
public class DemoServiceConsumer {

    public void execute() {
        // 创建应用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("dubbo-demo-consumer");

        // 创建注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");

        // 创建引用配置
        ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
        reference.setApplication(application);
        reference.setRegistry(registry);
        reference.setInterface("com.example.DemoService");
        reference.setGeneric("true");

        // 获取引用配置缓存
        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        GenericService genericService = cache.get(reference);

        // 泛化调用
        Object result = genericService.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"World"});
        System.out.println(result);
    }
}

Spring Boot配置文件(application.yml):

server:
  port: 8080

dubbo:
  application:
    name: dubbo-demo-consumer
  registry:
    address: zookeeper://127.0.0.1:2181
  consumer:
    check: false
  scan:
    base-packages: com.example

服务消费者启动类:

package com.example;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }

    @Bean
    public CommandLineRunner demo(DemoServiceConsumer consumer) {
        return args -> consumer.execute();
    }
}

运行示例

  1. 启动ZooKeeper。
  2. 启动服务提供者。
  3. 启动服务消费者。

在消费者的控制台中,你会看到泛化调用的结果:

Hello, World

总结

Dubbo的泛化调用通过GenericService接口实现,使得在不知道具体服务接口的情况下也能进行服务调用。

Dubbo泛化调用是一种动态调用服务方法的方式,通过这种方式,调用方无需依赖具体的接口类,只需传递服务名、方法名和参数即可。而Dubbo无参的泛化调用,指的是调用方不需要传递具体的参数值,只需指定参数的类型即可。 在Dubbo中,无参的泛化调用可以使用GenericService接口来实现调用方需要首先获取到GenericService实例,然后使用该实例进行远程调用。具体实现方式如下: 1. 获取GenericService实例 ```java ReferenceConfig<GenericService> reference = new ReferenceConfig<>(); reference.setInterface("com.xxx.service.XxxService"); reference.setGeneric(true); GenericService genericService = reference.get(); ``` 2. 构建请求参数 ```java List<String> parameterTypes = new ArrayList<>(); // 添加方法参数类型 parameterTypes.add("java.lang.String"); parameterTypes.add("com.xxx.model.User"); Object[] arguments = new Object; // 构建参数值 arguments = "test"; User user = new User(); user.setName("test"); arguments = user; // 构建请求对象 GenericServiceRequest request = new GenericServiceRequest(); request.setMethodName("testMethod"); request.setParameterTypes(parameterTypes.toArray(new String)); request.setArguments(arguments); ``` 3. 发起远程调用 ```java Object result = genericService.$invoke("testMethod", parameterTypes.toArray(new String), arguments); ``` 其中,$invoke()方法的第一个参数是方法名,第二个参数是方法参数类型数组,第三个参数是方法参数值数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

辞暮尔尔-烟火年年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值