基于SpringBoot的Dubbo泛化调用的实现

            Dubbo的泛化调用不需要引入调用方的接口,只需要指定接口的全类名,就可以调用服务,一般用于框架集成。接下来就基于SpringBoot实现了Dubbo的泛化调用。

1、服务端定义

1.1 服务定义及实现

package com.smart.springboot.api;

public interface HelloServiceFacade {
    void sayHello();
}
package com.smart.springboot.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.smart.springboot.api.HelloServiceFacade;

/**
 * @author : cuantianhou 2020/1/14
 */
@Service(version = "1.0.0",timeout = 20000)
public class HelloService implements HelloServiceFacade {
    @Override
    public void sayHello() {
        System.out.println("123");
    }
}

1.2 服务提供者配置

package com.smart.springboot.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author : cuantianhou 2020/4/1
 */
@Configuration
public class DubboServiceConfig {

    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig applicationConfig = new ApplicationConfig();

        applicationConfig.setId("springboot-dubbo-provider");

        applicationConfig.setName("springboot-dubbo-provider");

        return applicationConfig;
    }


    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setPort(20880);
        protocolConfig.setName("dubbo");
        return protocolConfig;
    }


    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://ip1:2181?backup=ip2:2181");
        registryConfig.setCheck(false);
        return registryConfig;
    }


    @Bean
    public MonitorConfig monitorConfig() {
       MonitorConfig monitorConfig = new MonitorConfig();
       monitorConfig.setProtocol("registry");
       return monitorConfig;
    }
}

1.3 启动类

package com.smart.springboot;

import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 启动类
 *
 * @author : cuantianhou 2020/1/14
 */

@SpringBootApplication
@DubboComponentScan("com.smart.springboot")
public class SpringBootDubboProvider {


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

1.4 pom文件



    4.0.0

    com.smart.springboot
    springboot-dubbo-provider
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.0.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter
        


        
        
            com.alibaba.boot
            dubbo-spring-boot-starter
            0.2.0
        

        
            com.smart.springboot
            springboot-dubbo-api
            1.0-SNAPSHOT
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2、消费端定义

2.1 Dubbo配置类

package com.smart.springboot.config;

import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.rpc.service.GenericService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author : cuantianhou 2020/4/1
 */
@Configuration
public class DubboConsumerConfig {

    @Bean
    public ApplicationConfig applicationConfig(){
     ApplicationConfig applicationConfig = new ApplicationConfig();
     applicationConfig.setId("springboot-dubbo-consumer");
     applicationConfig.setName("springboot-dubbo-consumer");
     return applicationConfig;
    }

    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setPort(20880);
        protocolConfig.setName("dubbo");
        return protocolConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://10.0.20.121:2181?backup=10.0.20.131:2181,10.0.20.132:2181,10.0.20.133:2181");
        registryConfig.setCheck(false);
        return registryConfig;
    }

    @Bean
    public MonitorConfig monitorConfig() {
        MonitorConfig monitorConfig = new MonitorConfig();
        monitorConfig.setProtocol("registry");
        return monitorConfig;
    }


    @Bean
    public ReferenceConfig referenceConfig(){
        ReferenceConfig referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface("com.smart.springboot.api.HelloServiceFacade");
        referenceConfig.setApplication(applicationConfig());
        referenceConfig.setRegistry(registryConfig());
        referenceConfig.setMonitor(monitorConfig());
        referenceConfig.setVersion("1.0.0");
        referenceConfig.setTimeout(20000);
        referenceConfig.setId("helloService");
        referenceConfig.setGeneric(Boolean.TRUE);
        return referenceConfig;
    }
}

2.2 启动类

package com.smart.springboot;


import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.rpc.service.GenericService;



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.Arrays;


/**
 * @author : cuantianhou 2020/1/14
 */
@SpringBootApplication
public class SpringBootDubboConsumer {


    public static void main(String[] args) {
        ConfigurableApplicationContext configurableApplicationContext =   SpringApplication.run(SpringBootDubboConsumer.class,args);
        String[] beanNames = configurableApplicationContext.getBeanDefinitionNames();
        Arrays.asList(beanNames).forEach(System.out::println);
        ReferenceConfig referenceConfig = configurableApplicationContext.getBean(ReferenceConfig.class);
        GenericService genericService =  referenceConfig.get();
        genericService.$invoke("sayHello",null,null);
    }
}

2.3 pom文件



    4.0.0

    com.smart.springboot
    springboot-dubbo-consumer
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.0.RELEASE
    



    
        
            org.springframework.boot
            spring-boot-starter-web
        


        
        
            com.alibaba.boot
            dubbo-spring-boot-starter
            0.2.0
        


    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

3、 运行结果

基于SpringBoot的Dubbo泛化调用的实现_第1张图片

4 、结论

       从泛化调用实现的过程来看,我们可以对自己提供所有服务进行测试,不需要引入调用的接口,减少代码的侵入,如有问题,欢迎指正。

5、改进

关于服务暴露的改进

5.1 关于服务的实现

/**
 * @author : cuantianhou 2020/1/14
 */
public class HelloService implements HelloServiceFacade {
    @Override
    public void sayHello() {
        System.out.println("123");
    }
}

5.2 在服务端配置中增加代码

  @Bean
    public ServiceConfig serviceConfig(){
        ServiceConfig serviceConfig = new ServiceConfig<>();
        serviceConfig.setInterface("com.smart.springboot.api.HelloServiceFacade");
        serviceConfig.setRef(helloService());
        serviceConfig.setApplication(applicationConfig());
        serviceConfig.setRegistry(registryConfig());
        serviceConfig.setMonitor(monitorConfig());
        serviceConfig.setVersion("1.0.0");
        serviceConfig.setTimeout(20000);
        serviceConfig.setId("helloService");
        serviceConfig.export();
        return serviceConfig;
    }
    @Bean
    public HelloService helloService(){
        return new HelloService();
    }

 

你可能感兴趣的:(Java,springboot)