Solon 1.2.13 发布,开启与 Springboot 的互通

Solon 一个类似Springboot的微型开发框架。项目从2018年启动以来,参考过大量前人作品;历时两年,3500多次的commit;内核保持0.1m的身材,超高的Web跑分,良好的使用体验。

Solon 强调:克制 + 简洁 + 开放的原则;力求:更小、更快、更自由的体验。

所谓更小:

内核0.1m,最小Web开发单位0.2m(相比Springboot项目包,小到可以乎略不计了)

所谓更快:

本机helloworld测试,Qps可达12万之多。可参考:《helloworld_wrk_test

所谓更自由:(代码操控自由)

// 除了注入模式之外,还可以按需手动
//
//手动获取配置
Map<String,String> db = Solon.cfg().getMap("db");

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

//手动监听http post请求
Solon.global().post("/user/update", x-> userService.updateById(x.paramMap()));

本次版本重大变更:

1、增加插件:springboot-solon-plugin(实现与Springboot互通)

  • 引入框架,约0.1m大小
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>springboot-solon-plugin</artifactId>
    <version>1.2.13</version>
</dependency>

? 如果有需要可引入Solon的其它插件,Solon适配的框架多较小巧 ?

  • 嵌入Springboot系统后,同时利用两套框架的容器资源与特性;或者过度期临时混合
  1. 启动应用
@SpringBootLinkSolon
@SpringBootApplication
public class DemoApp {
    
    
    public static void main(String[] args) {
    
    
        //先
        Solon.start(DemoApp.class, args);
        //后
        SpringApplication.run(DemoApp.class, args);
    }
}

  1. 定义Springboog组件
public interface HelloService {
    
    
    String hello(String name);
}

//此处为Springboot的注解(Solon 也有同名的注解)
@Component
public class HelloServiceImp implements HelloService {
    
    
    @Override
    public String hello(String name) {
    
    
        return "hello: " + name;
    }
}
  1. 在Solon类里使用Springboot容器内的组件
//此处为Solon的注解(Springboot 也有同名的注解)
@Controller
public class SolonController {
    
    
    //使用Solon注解,将Springboot bean注入到Solon的托管类中
    @Inject
    HelloService helloService;
    
    //注入配置
    @Inject("${user.name}")
    String name;

    @Mapping("/test")
    public String home(String msg) throws Exception {
    
    
        return "solon: " + helloService.hello();
    }
}
  1. 在Springboot类里使用Solon的手写特性
@RestController
public class Text2Controller {
    
    

    HelloService helloService;

    @RequestMapping("/test2")
    public String home(String msg) throws Exception {
    
    
        //使用Solon的手写特性赋值,进行懒加载
        //
        if(helloService == null){
    
    
            helloService = Aop.get(HelloService.class);
        }

        //手动获取配置
        //
        String name = Solon.cfg().get("user.name");
        
        //也可以动态增加一个请求监听
        //
        //Solon.global().get("/hello",(c)->c.output("Hello world!"));

        return "springboot: " + helloService.hello(name);
    }
}

?以上仅为演示效果设计,不一定匹配真实场景?

2、增加插件:consul-solon-plugin(实现对Consul的适配)

  • 此插件由社区开发者贡献,Solon的第一个社区贡献。。。非常感谢
  1. 配置示例
solon.app.name=test-consul-api
solon.app.group=test

consul.host=localhost
#consul.port=8500
#consul.token=
#consul.discovery.enable=true
#consul.discovery.hostname=12.12.12:12
#consul.discovery.tags=dev

#consul.discovery.healthCheckInterval=10s
#consul.discovery.healthCheckPath=/run/check/
consul.discovery.healthDetector=jvm,cpu,memory,disk,qps,os

#consul.locator.enable=true
#consul.locator.interval=10000

#consul.config.enable=true
#consul.config.key=test
consul.config.watch=config/,gateway/
#consul.config.interval=10000
  1. 使用示例
@Controller
public class HelloController {
    
    

    //使用了consul的注册与发现服务
    //
    @NamiClient("test-consul-api:/")
    HelloInterface helloInterface;

    @Mapping("/hello")
    public String sayHello() {
    
    
    
        //consul的配置内容集成到Solon的配置体系,可注入,可手动获取
        //
        return "config:" + Solon.cfg().get("hello")+",rpc:"+helloInterface.hello0();
    }
}

附:Solon项目地址

猜你喜欢

转载自blog.csdn.net/cwzb/article/details/112006634