Dubbo service provider unit testing

Preface

Due to the many Dubbo interfaces used in work, there will often be call timeouts or data confusion issues during joint debugging between products. At this time, when you want to test and call the provider interface separately, you often need to start the entire project. When the project is large, it is often compared. It was time-consuming and troublesome. Later, I discovered that it was possible to simulate a consumer directly through the API configuration method, and call the interface directly. It was simple and rude. It was very useful as a unit test. The unit test was posted directly below:

public class DubboProviderTest {

    /**
     * 当前应用注册信息
     */
    private static ApplicationConfig applicationConfig = new ApplicationConfig();
    /**
     * 注册中心信息缓存
     */
    private static Map<String, RegistryConfig> registryConfigMap = new ConcurrentHashMap<>();
    /**
     * key值
     */
    private static String key = null;

    static {
        applicationConfig.setName("xxx.consumer");
    }

    @Test
    public void test() {
        String address = "zookeeper://10.1.62.134:2181";
        ReferenceConfig<UserServiceApi> referenceConfig = getReferenceConfig(address, null, null, UserServiceApi.class);
        if (null != referenceConfig) {
            UserServiceApi userServiceApi = referenceConfig.get();
            // 然后就是对提供者的部分方法测试..
            UserDTO userDTO = userServiceApi.get("e10adc3949ba59abbe56e057f20f88dd");
            System.out.println(userDTO.getRealname());
        }
    }

    /**
     * 获取注册中心信息
     * @param address 注册中心地址
     * @param version 服务提供者版本号
     * @param group 服务所在的组
     * @return
     */
    private static RegistryConfig getRegistryConfig(String address, String version, String group) {
        key = address + "-" + version +  "-" + group;
        RegistryConfig registryConfig = registryConfigMap.get(key);
        if (null == registryConfig) {
            registryConfig = new RegistryConfig();
            registryConfig.setAddress(address);
            registryConfig.setVersion(version);
            registryConfig.setGroup(group);
            registryConfigMap.put(key, registryConfig);
        }
        return registryConfig;
    }

    /**
     * 获取服务提供者的代理对象
     * @param address
     * @param version
     * @param group
     * @param tClass 服务提供者接口
     * @param <T>
     * @return
     */
    private static <T> ReferenceConfig<T> getReferenceConfig(String address, String version, String group, Class<T> tClass) {
        // 该实例很重,有必要可缓存,否则可能造成内存泄漏和连接泄漏
        ReferenceConfig<T> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface(tClass);
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(getRegistryConfig(address, version, group));
        referenceConfig.setVersion(version);
        return referenceConfig;
    }
}

If you want to start the service provider through API configuration, please refer to the Dubbo Chinese manual: https://www.bookstack.cn/read/ApacheDubbo-zh/11.md

 

Guess you like

Origin blog.csdn.net/m0_38001814/article/details/102500467