對標 Spring Boot & Cloud ,輕量框架 Solon 1.4.8 發布


Solon 是一個輕量的Java基礎開發框架。強調,克制 + 簡潔 + 開放的原則;力求,更小、更快、更自由的體驗。支持:RPC、REST API、MVC、Job、Micro service、WebSocket、Socket 等多種開發模式。

Solon Cloud 是一系列的接口標准和配置規范,算是 Solon 的分布式開發套件方案。

快速了解Solon的材料:

《Solon 特性簡集,相較於 Springboot 有什么區別?》

《Solon Cloud 分布式服務開發套件清單,感覺受與 Spring Cloud 的不同》

《Solon 的想法與架構筆記》

《Solon 生態插件清單》,目前已有100多個生態插件

《Solon 框架入門》

所謂更小:

內核0.1m,最小的接口開發單位0.2m(相較於 Dubbo、Springboot 的依賴包,小到可以乎略不計)

所謂更快:

本機http helloworld測試,Qps可達12萬之多。可參考:《helloworld_wrk_test

所謂更自由:(代碼操控自由)

// 除了注解模式之外,還可以按需手動
//
//手動獲取配置(Props 為 Properties 增強版)
Props db = Solon.cfg().getProp("db");

//手動獲取容器里的Bean
UserService userService = Aop.get(UserService.class);

//手動監聽http post請求
Solon.global().post("/user/update", x-> userService.updateById(x.paramMap()));

//手動添加個RPC服務
Solon.global().add("/rpc/", HelloService.class, true);

//手動獲取一個RPC服務消費端
HelloService helloService = Nami.builder().create(HelloService.class);

//手動為容器添加組件
Aop.wrapAndPut(DemoService.class);

本次版本主要變化:

1、增加 sa-token-solon-plugin 插件,適配 sa-token 認證框架

應用樣例

@Controller
@Mapping("/test/")
public class TestController {
    @SaCheckLogin					
    @SaCheckRole("super-admin")		
    @SaCheckPermission("user-add")	
    @Mapping("atCheck")
    public AjaxJson atCheck() {
        System.out.println("只有通過注解鑒權,才能進入此方法");
        return AjaxJson.getSuccess();
    }
	
    @Mapping("atJurOr")
    @SaCheckPermission(value = {"user-add", "user-all", "user-delete"}, mode = SaMode.OR)	
    public AjaxJson atJurOr() {
        return AjaxJson.getSuccessData("用戶信息");
    }
}

2、增加 solon.extend.auth 插件,Solon 自擴展的認證框架

適配樣例

@Configuration
public class Config {
    @Bean
     public AuthAdapter init() {
        return new AuthAdapter()
            .loginUrl("/login")
            .authPathMatchers(new AuthPathMatchersImpl())
            .authInterceptor(new AuthInterceptorImpl())
            .authProcessor(new AuthProcessorImpl())
            .authOnFailure((ctx, rst) -> {
                ctx.render(Result.failure(403,"沒有權限:("));
            });
    }
}

應用樣例

@Controller
@Mapping("/test/")
public class TestController {
    @Mapping("/hello/")
    public String hello() {
        return "hello world!";
    }
    
    @AuthRoles("admin")
    @Mapping("/admin/")
    public String admin() {
        return "hello admin!";
    }
}

3、增加 solon-enjoy-web 快速開發套件(支持 enjob + activerecord 體驗)

jFinal 系列技術的愛好者,可以有不同的玩具了。solon-enjoy-web,支持 solon 完整的事務能力及緩存能力

//配置默認數據源
@Configuration
public class Config {
    @Bean
    public DataSource db1(@Inject("${test.db1}") HikariDataSource dataSource) {
        return dataSource;
    }
}

//應用示列
@Mapping("/demo/")
@Controller
public class DemoController {
    @Mapping("")
    public Object test(){
        return Db.template("appx_get").findFirst();
    }

    @Cache(tags = "test2")
    @Mapping("/test2")
    public Object test2(){
        AppxModel dao = new AppxModel().dao();

        return dao.findById(4);
    }
}

4、增加 異常訂閱轉換為正常輸出的能力

@Component
public class GlobalException implements EventListener<Throwable> {
    @Override
    public void onEvent(Throwable e) {
        Context c = Context.current();

        if (c != null) {
            AjaxJson aj = null;
            if (e instanceof SaTokenException) {    
                NotLoginException ee = (NotLoginException) e;
                aj = AjaxJson.getNotLogin().setMsg(ee.getMessage());
            } else if (e instanceof NotRoleException) {     
                NotRoleException ee = (NotRoleException) e;
                aj = AjaxJson.getNotJur("無此角色:" + ee.getRole());
            } else if (e instanceof NotPermissionException) {  
                NotPermissionException ee = (NotPermissionException) e;
                aj = AjaxJson.getNotJur("無此權限:" + ee.getCode());
            } else if (e instanceof DisableLoginException) { 
                DisableLoginException ee = (DisableLoginException) e;
                aj = AjaxJson.getNotJur("賬號被封禁:" + ee.getDisableTime() + "秒后解封");
            } else {
                aj = AjaxJson.getError(e.getMessage());
            }

            c.result = aj;
        }
    }
}

附:項目地址

附:入門示例


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM