dubbo 服务调用者的三种调用方式

时间:2025-02-14 08:37:17

1、使用注解:@Referenc 或 @DubboReference(2.2.7版本以后)

	@Reference
    DubboTestService dubboTestService;
	
/*  @DubboReference
    DubboTestService dubboTestService;*/
	
    @GetMapping(value = "/dubbo/getInfo")
    public Map<String, Object> cachePermissions(String id) {
       return (id);
    }
	

2、将服务实例在 文件中注册 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
       xmlns:xsi="http:///2001/XMLSchema-instance"
       xmlns:dubbo="/schema/dubbo" xmlns:context="/schema/context"
       xsi:schemaLocation="/schema/beans
       /schema/beans/spring-beans-4.
       /schema/dubbo
       /schema/dubbo/ /schema/context /schema/context/">

    <context:component-scan base-package=""></context:component-scan>

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,可自定义 -->
    <dubbo:application name="spring-consumer"  />

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 监控中心监控设置,直连监控中心服务器地址或者从注册中心发现 -->
    <dubbo:monitor protocol="registry"></dubbo:monitor>
    <!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference  interface="" loadbalance="consistenthash" timeout="30000" />

</beans>

服务调用时可通过继承ApplicationContextAware,获取spring容器内的实例

import ;
import ;
import ;
import ;

/**
 * @description: ApplicationContextProvider 通过context获取目标原型
 **/
@Component
public class ApplicationContextProvider implements ApplicationContextAware {

    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

         = applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {

        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {

        return getApplicationContext().getBean(clazz);
    }

}

调用服务时只需要

Object bean = (dubboTestService)//值取自<dubbo:reference里注册的 id
String method = "getInfo";//需要调用的方法名
Class<?> parameterTypes = ("");//method的参数类型的列表(参数顺序需按声明method时的参数列表排列),可含有多个,取决于该方法
Method met= ().getMethod(method, parameterTypes);
Object resp= (bean, id); //resp返回值 id为method方法的参数

3、泛化 

        创建服务调用类   借鉴 dubbo-samples

import ;
import ;
import ;
import ;
import org.;
import org.;
import ;
import ;

@Component
public class DubboServiceGeneric {

    public static final Logger logger = ();

    @Value("${-services}")
    public String applicationName;

    @Value("${}")
    public String dubboRegistryAddress;


    public Object invoke(String interfaceName, String version, String metName, String[] paramsClass, Object[] paramsValues) {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        (applicationName);
        RegistryConfig registryConfig = new RegistryConfig();
        (dubboRegistryAddress);
        ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<>();
        (interfaceName);
        (version);
        (registryConfig);
        (applicationConfig);
        ("true");
        (true);
        (7000);

        GenericService genericService = ();
        Object o = genericService.$invoke(metName, paramsClass, paramsValues);
        return o;
    }
}

调用时只需要

@Autowired
DubboServiceGeneric dubboServiceGeneric;

String s = "";//s为method方法的参数
String getInfo = ("", "1.0.0", "getInfo", s);//1.0.0为版本号 

对应的服务提供者为

import ;
import ;

@Service(version = "1.0.0")
public class DubboTestServiceImpl implements DubboTestService {


    @Autowired
    GatewayComponentMapper gatewayComponentMapper;

    @Override
    public Map<String, String> getInfo(Object json) {
        List<GatewayComponentDTO> dtos = (new QueryWrapper<>());
        HashMap<String, String> map = new HashMap<>();
        ("info", (dtos));
        return map;
    }
}