Dubbo-spi-demo与Adaptive使用

33 篇文章 0 订阅

在这里插入图片描述

建立API模块-dubbo-spi-demo-api
  • api添加依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.5</version>
        </dependency>
    </dependencies>


  • 建立Service-添加spi注解
@SPI
public interface HelloService {

    String  sayHello();
}

  • 建立实现-依赖上面的模块
    在这里插入图片描述
public class DogHelloServiceImpl implements HelloService {
    @Override
    public String sayHello() {
        return "hello dog";
    }
}
public class HumanHelloServiceImpl implements HelloService {

    @Override
    public String sayHello() {
        return "hello Human";
    }
}


  • 在resources目录下建立META-INF/dubbo目录
    在这里插入图片描述
    文件名就是接口名-内容key=value
dog=com.liu.service.impl.DogHelloServiceImpl
human=com.liu.service.impl.HumanHelloServiceImpl
  • 测试
 ExtensionLoader<HelloService> extensionLoader = ExtensionLoader.getExtensionLoader(HelloService.class);


        Set<String> supportedExtensions = extensionLoader.getSupportedExtensions();

        supportedExtensions.forEach(s->{
            System.out.println(extensionLoader.getExtension(s).sayHello());
        });

hello dog
hello Human

  • 添加spi默认-在接口上
@SPI("dog")
public interface HelloService {

    String  sayHello();
}


  • 测试修改
    public static void main(String[] args) {

        ExtensionLoader<HelloService> extensionLoader = ExtensionLoader.getExtensionLoader(HelloService.class);


        Set<String> supportedExtensions = extensionLoader.getSupportedExtensions();

        supportedExtensions.forEach(s->{
            System.out.println(extensionLoader.getExtension(s).sayHello());
        });

        System.out.println("===============");
        HelloService defaultExtension = extensionLoader.getDefaultExtension();
        System.out.println(defaultExtension.sayHello());

    }
}

hello dog
hello Human
===============
hello dog

getDefaultExtension可以获取默认配置的是哪个接口实现

  • JDK 标准的 SPI 会一次性实例化扩展点所有实现,如果有扩展实现初始化很耗时,但如果没用上也加
    载,会很浪费资源
  • 如果有扩展点加载失败,则所有扩展点无法使用

Adaptive


@SPI("dog")
public interface HelloService {

    String  sayHello();

    @Adaptive
    String sayHello(URL url);
}

public class DogHelloServiceImpl implements HelloService {
    @Override
    public String sayHello() {
        return "hello dog";
    }

    @Override
    public String sayHello(URL url) {
        return "hello dog : "+url;
    }
}

public class HumanHelloServiceImpl implements HelloService {

    @Override
    public String sayHello() {
        return "hello Human";
    }

    @Override
    public String sayHello(URL url) {
        return "hello Human   "+url;
    }
}


public class AdaptiveMain {
    public static void main(String[] args) {
        /**
         * 这里注意?后面
         * hello.service=HelloService
         * =后面就是在配置文件中的key
         */
        URL   url  = URL.valueOf("test://localhost/hello?hello.service=human");
        ExtensionLoader<HelloService> extensionLoader = ExtensionLoader.getExtensionLoader(HelloService.class);

        HelloService helloService = extensionLoader.getAdaptiveExtension();

        System.out.println(helloService.sayHello(url));
    }
}

hello Human test://localhost/hello?hello.service=human

如果不写就调用@SPI注解配置的,

public class AdaptiveMain {
    public static void main(String[] args) {
        /**
         * 这里注意?后面
         * hello.service=HelloService
         * =后面就是在配置文件中的key
         * ?hello.service=human
         */
        URL   url  = URL.valueOf("test://localhost/hello");
        ExtensionLoader<HelloService> extensionLoader = ExtensionLoader.getExtensionLoader(HelloService.class);

        HelloService helloService = extensionLoader.getAdaptiveExtension();

        System.out.println(helloService.sayHello(url));
    }
}

hello dog : test://localhost/hello

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值