(三)dubbo api方式

具体参考我的gitee:springboot-dubbo-api

ProviderConfig

package com.dubbo.api.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.dubbo.api.service.DubboApiService;
import com.dubbo.api.service.DubboApiServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Classname ProviderConfig
 * @Description TODO
 * @Date 2020/4/23 9:11
 * @Created by Administrator
 */
@Configuration
public class ProviderConfig {

   @Bean
   public void getProviderConfig(){
       // 服务实现
       DubboApiService dubboApiService = new DubboApiServiceImpl();
       // 当前应用配置
       ApplicationConfig application = new ApplicationConfig();
       application.setName("DubboProvider");
       // qos-server can not bind localhost:22222  java.net.BindException: Address already in use: bind
       application.setQosEnable(true);
       application.setQosPort(33333);
       application.setQosAcceptForeignIp(false);
       // 连接注册中心配置
       RegistryConfig registry = new RegistryConfig();
       registry.setAddress("zookeeper://127.0.0.1:2181");
       //registry.setUsername("aaa");
       //registry.setPassword("bbb");

       // 服务提供者协议配置
       ProtocolConfig protocol = new ProtocolConfig();
       protocol.setName("dubbo");
       protocol.setPort(20880);
       protocol.setThreads(200);

       // 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
       // 服务提供者暴露服务配置
       ServiceConfig<DubboApiService> service = new ServiceConfig<DubboApiService>(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
       service.setApplication(application);
       service.setRegistry(registry); // 多个注册中心可以用setRegistries()
       service.setProtocol(protocol); // 多个协议可以用setProtocols()
       service.setInterface(DubboApiService.class);
       service.setRef(dubboApiService);
       service.setVersion("1.0.0");
       // 暴露及注册服务
       service.export();
   }
 }

provider结构
在这里插入图片描述

ConsumerConfig

package com.dubbo.api.config;


import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.dubbo.api.service.DubboApiService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Classname ConsumerConfig
 * @Description TODO
 * @Date 2020/4/23 9:11
 * @Created by Administrator
 */
@Configuration
public class ConsumerConfig {

    @Bean
    public DubboApiService getConsumerConfig(){
        // 当前应用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("DubboConsumer");
        // qos-server can not bind localhost:22222  java.net.BindException: Address already in use: bind
        application.setQosEnable(true);
        application.setQosPort(44444);
        application.setQosAcceptForeignIp(false);
        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");
        //registry.setPort(20881);
        //registry.setUsername("aaa");
        //registry.setPassword("bbb");
        // 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
        // 引用远程服务
        ReferenceConfig<DubboApiService> reference = new ReferenceConfig<DubboApiService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
        reference.setApplication(application);
        reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
        reference.setInterface(DubboApiService.class);
        reference.setVersion("1.0.0");
        //启动时检查服务是否存在
        reference.setCheck(false);
        // 和本地bean一样使用xxxService
        DubboApiService dubboApiService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
        //如果要销毁Cache中的ReferenceConfig,将销毁ReferenceConfig并释放对应的资源
        //reference.destroy();
        return  dubboApiService;
    }

}

consumer结构
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章