白季飞龙的个人主页

Dubbo大杂烩

Dubbo入门

配置Maven依赖

<dependency>
    <groupId>com.alibaba.spring.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

服务提供端

package io.github.baijifeilong.dubbo.mydubboprovider;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import io.github.baijifeilong.dubbo.mydubboconsumer.MyDubboConsumerApp;
import lombok.SneakyThrows;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;

@SuppressWarnings("SpringFacetCodeInspection")
@SpringBootApplication
@EnableDubboConfiguration
public class MyDubboProviderApp implements ApplicationListener<ApplicationReadyEvent> {

    public static void main(String[] args) {
        // Dubbo服务端和消费端都必须配置applicationName
        System.setProperty("spring.application.name", MyDubboConsumerApp.class.getSimpleName());
        // 使用本地注册器
        System.setProperty("spring.dubbo.registry", "N/A");
        // 启动非Web服务
        new SpringApplicationBuilder(MyDubboProviderApp.class).web(WebApplicationType.NONE).run(args);
    }

    @Override
    @SneakyThrows
    public void onApplicationEvent(@Nonnull ApplicationReadyEvent applicationReadyEvent) {
        System.out.println("Ready");
        Thread.currentThread().join();
    }

    public interface IHelloService {
        String hello(String name);
    }

    @com.alibaba.dubbo.config.annotation.Service
    @Component
    static class HelloService implements IHelloService {
        @Override
        public String hello(String name) {
            return "Hello " + name;
        }
    }
}

服务消费端

package io.github.baijifeilong.dubbo.mydubboconsumer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import io.github.baijifeilong.dubbo.mydubboprovider.MyDubboProviderApp;
import lombok.SneakyThrows;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import javax.annotation.Resource;

@SuppressWarnings("SpringFacetCodeInspection")
@SpringBootApplication
@EnableDubboConfiguration
public class MyDubboConsumerApp implements ApplicationListener<ApplicationReadyEvent> {

    public static void main(String[] args) {
        // Dubbo服务端和消费端都必须配置applicationName
        System.setProperty("spring.application.name", MyDubboConsumerApp.class.getSimpleName());
        // Dubbo(服务端、客户端都有)默认会开启qos服务,只有在dubbo配置文件中才能禁用。所以此处只能换个端口
        System.setProperty("dubbo.qos.port", "33333");
        new SpringApplicationBuilder(MyDubboConsumerApp.class).web(WebApplicationType.NONE).run(args);
    }

    @Resource
    private Holder holder;

    @Override
    @SneakyThrows
    public void onApplicationEvent(@Nonnull ApplicationReadyEvent applicationReadyEvent) {
        System.out.println("ready");
        holder.run();
        Thread.currentThread().join();
    }

    /**
     * "@Reference"在@EnableDubboConfiguration注解的类里面不能直接注入成功,所以此处放在Holder里
     */
    @Component
    static class Holder {
        // 必须配置,没默认值
        @Reference(url = "dubbo://127.0.0.1:20880")
        private MyDubboProviderApp.IHelloService helloService;

        void run() {
            System.out.println(helloService.hello("word"));
        }
    }
}

控制台输出

服务端

2019-01-12 10:49:55.132  INFO 11467 --- [           main] c.a.dubbo.common.logger.LoggerFactory    : using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter

  ████████▄  ███    █▄  ▀█████████▄  ▀█████████▄   ▄██████▄  
  ███   ▀███ ███    ███   ███    ███   ███    ███ ███    ███ 
  ███    ███ ███    ███   ███    ███   ███    ███ ███    ███ 
  ███    ███ ███    ███  ▄███▄▄▄██▀   ▄███▄▄▄██▀  ███    ███ 
  ███    ███ ███    ███ ▀▀███▀▀▀██▄  ▀▀███▀▀▀██▄  ███    ███ 
  ███    ███ ███    ███   ███    ██▄   ███    ██▄ ███    ███ 
  ███   ▄███ ███    ███   ███    ███   ███    ███ ███    ███ 
  ████████▀  ████████▀  ▄█████████▀  ▄█████████▀   ▀██████▀  


 :: Dubbo ::        (v2.6.0)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2019-01-12 10:49:55.545  INFO 11467 --- [           main] i.g.b.d.m.MyDubboProviderApp             : Starting MyDubboProviderApp on bogon with PID 11467 (/Users/bj/workspace/java/myjavademos/target/classes started by bj in /Users/bj/workspace/java/myjavademos)
2019-01-12 10:49:55.549  INFO 11467 --- [           main] i.g.b.d.m.MyDubboProviderApp             : No active profile set, falling back to default profiles: default
2019-01-12 10:49:57.977  WARN 11467 --- [           main] t.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[io.github.baijifeilong.dubbo.mydubboprovider]' package. Please check your configuration.
2019-01-12 10:49:58.000  WARN 11467 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[io.github.baijifeilong.dubbo.mydubboprovider]' package. Please check your configuration.
2019-01-12 10:49:58.578  INFO 11467 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-01-12 10:49:58.585  INFO 11467 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-01-12 10:49:58.648  INFO 11467 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 23ms. Found 0 repository interfaces.
2019-01-12 10:49:59.703  INFO 11467 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$19704d5] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-01-12 10:49:59.833  INFO 11467 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.dubbo-com.alibaba.dubbo.spring.boot.DubboProperties' of type [com.alibaba.dubbo.spring.boot.DubboProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-01-12 10:49:59.840  INFO 11467 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.alibaba.dubbo.spring.boot.DubboConsumerAutoConfiguration' of type [com.alibaba.dubbo.spring.boot.DubboConsumerAutoConfiguration$$EnhancerBySpringCGLIB$$3bb19df7] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-01-12 10:50:00.088  INFO 11467 --- [           main] com.alibaba.dubbo.qos.server.Server      :  [DUBBO] qos-server bind localhost:22222, dubbo version: 2.6.0, current host: 127.0.0.1
2019-01-12 10:50:00.283  INFO 11467 --- [           main] com.alibaba.dubbo.config.AbstractConfig  :  [DUBBO] Export dubbo service io.github.baijifeilong.dubbo.mydubboprovider.MyDubboProviderApp$IHelloService to local registry, dubbo version: 2.6.0, current host: 127.0.0.1
2019-01-12 10:50:00.283  INFO 11467 --- [           main] com.alibaba.dubbo.config.AbstractConfig  :  [DUBBO] Export dubbo service io.github.baijifeilong.dubbo.mydubboprovider.MyDubboProviderApp$IHelloService to url dubbo://192.168.1.104:20880/io.github.baijifeilong.dubbo.mydubboprovider.MyDubboProviderApp$IHelloService?anyhost=true&application=MyDubboConsumerApp&bind.ip=192.168.1.104&bind.port=20880&dubbo=2.6.0&generic=false&interface=io.github.baijifeilong.dubbo.mydubboprovider.MyDubboProviderApp$IHelloService&methods=hello&pid=11467&side=provider&timestamp=1547261400196, dubbo version: 2.6.0, current host: 127.0.0.1
2019-01-12 10:50:00.422  INFO 11467 --- [           main] c.a.d.remoting.transport.AbstractServer  :  [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.1.104:20880, dubbo version: 2.6.0, current host: 127.0.0.1
2019-01-12 10:50:00.463  INFO 11467 --- [           main] t.m.m.autoconfigure.MapperCacheDisabler  : Clear tk.mybatis.mapper.util.MsUtil CLASS_CACHE cache.
2019-01-12 10:50:00.464  INFO 11467 --- [           main] t.m.m.autoconfigure.MapperCacheDisabler  : Clear tk.mybatis.mapper.genid.GenIdUtil CACHE cache.
2019-01-12 10:50:00.464  INFO 11467 --- [           main] t.m.m.autoconfigure.MapperCacheDisabler  : Clear tk.mybatis.mapper.version.VersionUtil CACHE cache.
2019-01-12 10:50:00.468  INFO 11467 --- [           main] t.m.m.autoconfigure.MapperCacheDisabler  : Clear EntityHelper entityTableMap cache.
2019-01-12 10:50:01.125  INFO 11467 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-01-12 10:50:01.391  INFO 11467 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-01-12 10:50:01.743  INFO 11467 --- [           main] i.g.b.d.m.MyDubboProviderApp             : Started MyDubboProviderApp in 7.458 seconds (JVM running for 8.32)
Ready
2019-01-12 10:50:02.011  INFO 11467 --- [on(2)-127.0.0.1] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2019-01-12 10:50:02.027  INFO 11467 --- [on(2)-127.0.0.1] io.lettuce.core.KqueueProvider           : Starting with kqueue library

消费端

2019-01-12 10:50:15.800  INFO 11470 --- [           main] c.a.dubbo.common.logger.LoggerFactory    : using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter

  ████████▄  ███    █▄  ▀█████████▄  ▀█████████▄   ▄██████▄
  ███   ▀███ ███    ███   ███    ███   ███    ███ ███    ███
  ███    ███ ███    ███   ███    ███   ███    ███ ███    ███
  ███    ███ ███    ███  ▄███▄▄▄██▀   ▄███▄▄▄██▀  ███    ███
  ███    ███ ███    ███ ▀▀███▀▀▀██▄  ▀▀███▀▀▀██▄  ███    ███
  ███    ███ ███    ███   ███    ██▄   ███    ██▄ ███    ███
  ███   ▄███ ███    ███   ███    ███   ███    ███ ███    ███
  ████████▀  ████████▀  ▄█████████▀  ▄█████████▀   ▀██████▀


 :: Dubbo ::        (v2.6.0)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2019-01-12 10:50:16.205  INFO 11470 --- [           main] i.g.b.d.m.MyDubboConsumerApp             : Starting MyDubboConsumerApp on bogon with PID 11470 (/Users/bj/workspace/java/myjavademos/target/classes started by bj in /Users/bj/workspace/java/myjavademos)
2019-01-12 10:50:16.209  INFO 11470 --- [           main] i.g.b.d.m.MyDubboConsumerApp             : No active profile set, falling back to default profiles: default
2019-01-12 10:50:18.951  WARN 11470 --- [           main] t.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[io.github.baijifeilong.dubbo.mydubboconsumer]' package. Please check your configuration.
2019-01-12 10:50:18.978  WARN 11470 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[io.github.baijifeilong.dubbo.mydubboconsumer]' package. Please check your configuration.
2019-01-12 10:50:19.403  INFO 11470 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-01-12 10:50:19.405  INFO 11470 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-01-12 10:50:19.423  INFO 11470 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 7ms. Found 0 repository interfaces.
2019-01-12 10:50:19.714  INFO 11470 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$c644d36b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-01-12 10:50:19.741  INFO 11470 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.dubbo-com.alibaba.dubbo.spring.boot.DubboProperties' of type [com.alibaba.dubbo.spring.boot.DubboProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-01-12 10:50:19.743  INFO 11470 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.alibaba.dubbo.spring.boot.DubboConsumerAutoConfiguration' of type [com.alibaba.dubbo.spring.boot.DubboConsumerAutoConfiguration$$EnhancerBySpringCGLIB$$5f6c8d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-01-12 10:50:19.865  INFO 11470 --- [           main] com.alibaba.dubbo.qos.server.Server      :  [DUBBO] qos-server bind localhost:33333, dubbo version: 2.6.0, current host: 127.0.0.1
2019-01-12 10:50:20.150  INFO 11470 --- [           main] c.a.d.remoting.transport.AbstractClient  :  [DUBBO] Successed connect to server /192.168.1.104:20880 from NettyClient 192.168.1.104 using dubbo version 2.6.0, channel is NettyChannel [channel=[id: 0x28501a4b, /192.168.1.104:61488 => /192.168.1.104:20880]], dubbo version: 2.6.0, current host: 192.168.1.104
2019-01-12 10:50:20.150  INFO 11470 --- [           main] c.a.d.remoting.transport.AbstractClient  :  [DUBBO] Start NettyClient bogon/192.168.1.104 connect to the server /192.168.1.104:20880, dubbo version: 2.6.0, current host: 192.168.1.104
2019-01-12 10:50:20.188  INFO 11470 --- [           main] com.alibaba.dubbo.config.AbstractConfig  :  [DUBBO] Refer dubbo service io.github.baijifeilong.dubbo.mydubboprovider.MyDubboProviderApp$IHelloService from url dubbo://127.0.0.1:20880/io.github.baijifeilong.dubbo.mydubboprovider.MyDubboProviderApp$IHelloService?application=MyDubboConsumerApp&dubbo=2.6.0&interface=io.github.baijifeilong.dubbo.mydubboprovider.MyDubboProviderApp$IHelloService&methods=hello&pid=11470&register.ip=192.168.1.104&side=consumer&timestamp=1547261419977, dubbo version: 2.6.0, current host: 192.168.1.104
2019-01-12 10:50:20.269  INFO 11470 --- [           main] t.m.m.autoconfigure.MapperCacheDisabler  : Clear tk.mybatis.mapper.util.MsUtil CLASS_CACHE cache.
2019-01-12 10:50:20.270  INFO 11470 --- [           main] t.m.m.autoconfigure.MapperCacheDisabler  : Clear tk.mybatis.mapper.genid.GenIdUtil CACHE cache.
2019-01-12 10:50:20.270  INFO 11470 --- [           main] t.m.m.autoconfigure.MapperCacheDisabler  : Clear tk.mybatis.mapper.version.VersionUtil CACHE cache.
2019-01-12 10:50:20.274  INFO 11470 --- [           main] t.m.m.autoconfigure.MapperCacheDisabler  : Clear EntityHelper entityTableMap cache.
2019-01-12 10:50:20.923  INFO 11470 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-01-12 10:50:21.213  INFO 11470 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-01-12 10:50:21.551  INFO 11470 --- [           main] i.g.b.d.m.MyDubboConsumerApp             : Started MyDubboConsumerApp in 7.218 seconds (JVM running for 8.4)
ready
Hello word
2019-01-12 10:50:21.806  INFO 11470 --- [on(4)-127.0.0.1] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2019-01-12 10:50:21.821  INFO 11470 --- [on(4)-127.0.0.1] io.lettuce.core.KqueueProvider           : Starting with kqueue library

Dubbo之坑

Dubbo死磕XML,注定到处是坑

不能用Dubbo传输匿名类

不能用Dubbo传输匿名类,匿名容器类除外。Dubbo默认使用Hession序列化对象。对于容器类,序列化容器中的所有元素并组装。对于非容器类,用Java的反射API获取这个类的所有Field,并依次序列化。匿名类自带一个名叫"this$0"的Field,存放的是匿名类所在类。所以,序列化匿名类时,会报匿名类所在类未实现序列化的异常,或者是匿名类所在类所引用的类的未实现序列化的异常,或是因为循环递归引用导致的堆栈溢出异常,或是其他乱七八糟的异常。

文章首发: https://baijifeilong.github.io/2018/11/27/dubbo


漫漫路,莫论逍遥;潜心修,只为悟道