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

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

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

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.lyj.service"></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 id="dubboTestService" interface="com.dubbo.service.dubboTestService" loadbalance="consistenthash" timeout="30000" />

</beans>

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

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

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

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

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

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

3、泛化 

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

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class DubboServiceGeneric {

    public static final Logger logger = LoggerFactory.getLogger(DubboServiceGeneric.class);

    @Value("${dubbo.cloud.subscribed-services}")
    public String applicationName;

    @Value("${dubbo.registry.address}")
    public String dubboRegistryAddress;


    public Object invoke(String interfaceName, String version, String metName, String[] paramsClass, Object[] paramsValues) {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(applicationName);
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(dubboRegistryAddress);
        ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface(interfaceName);
        referenceConfig.setVersion(version);
        applicationConfig.setRegistry(registryConfig);
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setGeneric("true");
        referenceConfig.setAsync(true);
        referenceConfig.setTimeout(7000);

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

调用时只需要

@Autowired
DubboServiceGeneric dubboServiceGeneric;

String s = "";//s为method方法的参数
String getInfo = dubboServiceGeneric.invoke("com.dubbo.service.DubboTestService", "1.0.0", "getInfo", s);//1.0.0为版本号 

对应的服务提供者为

import com.dubbo.service.DubboTestService;
import org.apache.dubbo.config.annotation.Service;

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


    @Autowired
    GatewayComponentMapper gatewayComponentMapper;

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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值