Giter Site home page Giter Site logo

extension's Introduction

这是什么

相对于普通spi,你只能调用同jvm中的spi实现,本框架支持对远程spi的调用。

架构图

调用逻辑图

如何使用

maven依赖

平台方增加依赖

<dependency>
    <groupId>com.github.dsc-cmt</groupId>
    <artifactId>extension-consumer</artifactId>
    <version>1.2.0</version>
</dependency>

业务方增加依赖

<dependency>
    <groupId>com.github.dsc-cmt</groupId>
    <artifactId>extension-provider</artifactId>
    <version>1.2.0</version>
</dependency>

平台方-spi接口定义者

  1. 增加spring配置
@EnableExtensionConsumer(appName = "test",configMode = ConfigMode.LOCAL )

configMode默认为LOCAL,需要在classpath配置spi.yml,文件示例

appName: test
spis:
  - spiInterface: com.cmt.extension.spi.IHelloService
    extensions:
      - bizCode: a
        invokeMethod: local
        expireTime: 5000
        isDefault: true
      - bizCode: b
        invokeMethod: local
        expireTime: 5000
        isDefault: false

若ConfigMode为REMOTE,appName需要和控制台一致

  1. 标记SPI接口 给需要作为扩展点的接口加上SPI注解
@SPI
public interface IHelloService {
  String hello();
}
  1. 注入spi
@Resource(name = "IHelloService")
IHelloService helloService;

使用spring的@Resource注解,name和接口名一致

4.注册本地默认实现

//@Extension(bizCode = DEFAULT_BIZ_CODE,invokeMethod = SpiTypeEnum.LOCAL)
@Extension
public class DefaultHelloServiceImpl implements IHelloService {
    public String hello() {
        return "default hello";
    }
}

当找不到bizCode对应的spi时,调用spi默认实现。

  1. 注册spi本地实现(可选)
@Extension(bizCode = "d",invokeMethod = SpiTypeEnum.LOCAL)
public class LocalHelloServiceImpl implements IHelloService {
    public String hello() {
        return "local";
    }
}
  1. 调用 在该spi在被调用前,需要通过BusinessContext设置bizCode才能路由到正确的spi实现
BusinessContext.setBizCode("a");
testService.hello();

或者(推荐方式)

ExtensionHelper.execute("f",()->{
    String rst =  service.hello();
    System.out.println(rst);
    return rst;
});

7.当ConfigMode配置为REMOTE时,需在application.properties中配置admin服务地址来加载,同步spi配置

spi.portal=adminUrl

业务方-spi接口实现者

  1. 增加spring配置
@EnableExtensionProvider
  1. 提供spi接口远程实现
@Extension(bizCode = "a", invokeMethod = SpiTypeEnum.DUBBO)
public class AHelloServiceImpl implements IHelloService{
    public String hello() {
        return "HelloA";
    }
}

配置admin控制台

当ConfigMode配置为REMOTE时,为了让spi消费者能够正常消费远程/本地spi,必须在admin控制台注册你的spi实现

dubbo filter

为了在dubbo调用时传递bizCode,框架实现了com.alibaba.dubbo.rpc.Filter接口, 其中ConsumerBizCodeFilter接口从BusinessContext中获取当前bizCode并设置到RpcContext当中, ProviderBizCodeFilter从RpcContext中获取bizCode并设置回BusinessContext.

示例

见源码extension-demo项目 使用步骤:

  1. 本地创建数据库,脚本见release-note项目
  2. 打包Extension项目,命令如下 mvn clean package -DskipTests=true
  3. 启动extension-admin项目 2.1 访问本地的http://localhost:8088 2.2 登录系统(默认用户名:admin 密码:admin) 2.3 打开应用管理界面去创建应用,应用名称设置为test 2.4 打开spi管理界面,为test应用创建spi,spi的接口名称为spi-interface项目下的IHelloService接口全路径(com.cmt.extension.spi.IHelloService) SPI的bizCode属性见IHelloService接口所有实现类,按照与实现类上的@Extension注解的bizCode属性一致即可
  4. 启动zookeeper。若本地未按照zookeeper可以启动zookeeper-server项目的内嵌zookeeper
  5. 启动provider项目
  6. 运行consumer项目下的ApplicationContextHolderTest类。
  7. 可以自定义接口,只要按照IHelloService一样的到spi管理页面添加一下即可。

extension's People

Contributors

dependabot[bot] avatar shengchaojie avatar tuzhenxian avatar younghuang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.