Dubbo笔记 ① : 入门篇
发布日期:2021-05-10 01:56:43 浏览次数:21 分类:精选文章

本文共 5504 字,大约阅读时间需要 18 分钟。

Apache Dubbo ������������������

������������

��������������� Dubbo ������������������������������������Apache Dubbo ������������������������������������������������������������������������������������ Dubbo 2.7.0 ������������������������������������������������������


Dubbo ������������������������������������������������������������������������������������ RPC ��������������������������������������������� Spring ���������������������Dubbo ���������������������������������������������������������������������������������������������������������������������������������


������Dubbo ������������

Dubbo ������������������������������������������

  • Provider���������������������������������
  • Consumer���������������������������������������
  • Registry���������������������������������������
  • Monitor���������������������������������������������������������
  • Container���������������������
  • ������������������

    • ������������������������������������������������������������
    • ������������������������������������������������������������������������
    • ���������������������������������������������������������������
    • ���������������������������������������������������������������������������
    • ���������������������������������������������������������������������������������������
    • ������������������������������������������������������������������������������������

    ������������������������

    1. Dubbo ��� Spring ������������

    Dubbo ��� Spring ������������������������ Dependency Injection��������������������� AOP���������������������������

    2. ������������������

    ��������������������������� Dubbo ���������������������������������

    public interface GreetingService {
    String sayHello(String name);
    CompletableFuture
    sayHelloForAsync(String name);
    String sayHelloForAsyncByContext(String name);
    }
    public class GreetingServiceImpl implements GreetingService {
    private static final ThreadPoolExecutor POOL = new ThreadPoolExecutor(8, 15, 1, TimeUnit.MINUTES, new SynchronousQueue<>(), new NamedThreadFactory("thread-pool"), new CallerRunsPolicy());
    // ������������������������������������
    }

    ���������������������������

    4.1 ���������������

    ��������������������� ServiceConfig ������������������������

    public class ApiProvider {
    public static void main(String[] args) throws IOException {
    ServiceConfig serviceServiceConfig = new ServiceConfig();
    serviceServiceConfig.setApplication(new ApplicationConfig("Api-provider"));
    serviceServiceConfig.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));
    serviceServiceConfig.setInterface(GreetingService.class);
    serviceServiceConfig.setRef(new GreetingServiceImpl());
    serviceServiceConfig.export();
    System.out.println("���������������");
    System.in.read();
    }
    }

    4.2 ���������������

    ��������������������� ReferenceConfig ������������������������������

    public class ApiConsumer {
    public static void main(String[] args) {
    ReferenceConfig referenceConfig = new ReferenceConfig();
    referenceConfig.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));
    referenceConfig.setTimeout(5000);
    GreetingService greetingService = referenceConfig.get();
    // ���������������������������
    RpcContext.getContext().setAttachment("attribute", "ApiConsumer");
    String hello = greetingService.sayHello("hello");
    System.out.println("hello = " + hello);
    }
    }

    ������������������������

    5.1 ������������

    ������������������������������������������������������������������������������

    5.2 ������������

    ���������������������������������������

  • ������������ CompletableFuture��������� whenComplete ������������
  • ������ AsyncContext ���������������������������������

  • ������������������������

    Dubbo ������������������������������������

  • true������������������
  • bean��������� JavaBean ������������
  • nativejava��������� Java ���������������
  • ���������������������������������������������������������

    public class ApiGenericConsumer {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
    ReferenceConfig referenceConfig = new ReferenceConfig();
    referenceConfig.setTimeout(5000);
    referenceConfig.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));
    referenceConfig.setGeneric(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA);
    GenericService genericService = referenceConfig.get();
    // ������������������������������
    ExtensionLoader projLoader = ExtensionLoader.getExtensionLoader(Serialization.class);
    projLoader.ExtensionConstants.GENERIC_SERIALIZATION_NATIVE_JAVA
    .getExtension()
    .serialize(null, new ByteArrayOutputStream())
    .writeObject("������");
    Object result = projLoader.ExtensionConstants.GENERIC_SERIALIZATION_NATIVE_JAVA
    .getExtension()
    .deserialize(null, new ByteArrayInputStream(resultBytes.toByteArray()));
    System.out.println("���������������" + result);
    }
    }

    ������������������

    Dubbo ���������������������������

  • force:return������������������������mock���
  • fail:return������������������������ucceed���������mock���
  • ������������������������������mock���������

    referenceConfig.setMock(true);

    ��������������������������������������� Dubbo ������������������������������������������������������������������������������������������������Apache Dubbo ������������������������������������������������������������������������������������

    上一篇:Dubbo笔记 ② : 架构概述
    下一篇:Spring 源码分析补充篇一 :DeferredImportSelector 的处理

    发表评论

    最新留言

    留言是一种美德,欢迎回访!
    [***.207.175.100]2025年04月24日 01时03分04秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章