Solon

使用网关

使用网关,为同一套接口提供不同的输出(Solon 自带)

网关的技术本质,是一个定制了的 Solon Handler。如此理解,新切感会好些:)

//网关1
@Mapping("/api/rest/**")
@Component
public class Gateway1 extends Gateway {
    @Override
    protected void register() {
        //设定默认render
        before(c -> c.attrSet("@render", "@json"));

        //添加服务
        add("user", UserServiceImpl.class, true);

    }
}

//网关2
@Mapping("/api/rpc/**")
@Component
public class Gateway3 extends Gateway {
    @Override
    protected void register() {
        //设定默认render
        before(c -> c.attrSet("@render", "@type_json"));

        //添加服务(不带mapping的函数;需要 remoting = true,才会加载出来)
        add("user", UserServiceImpl.class, true);
    }
}

//网关3(这个比较复杂,和实战性)
@Mapping("/api/v2/app/**")
@Component
public class Gateway3 extends UapiGateway {
    @Override
    protected void register() {
    
        filter(new BreakerFilter()); //融断过滤器

        before(new StartHandler()); //开始计时
        before(new ParamsParseHandler()); //参数解析
        before(new ParamsSignCheckHandler(new Md5Encoder())); //参数签名较验
        before(new ParamsRebuildHandler(new AesDecoder())); //参数重构

        after(new OutputBuildHandler(new AesEncoder())); //输出构建
        after(new OutputSignHandler(new Md5Encoder())); //输出签名
        after(new OutputHandler()); //输出
        after(new EndBeforeLogHandler()); //日志
        after(new EndHandler("v2.api.app")); //结束计时

        addBeans(bw -> "api".equals(bw.tag()));
    }
}