solon架构(spring-boot未来最有效的竞争力)

  一、现在基本WEB的开发都是用spring-boot来做开发了,但是springboot存在很多弊端。体量大,启动慢等。优点就是生态比较完整,文档说明也比较多。

  二、solon架构,是我学习其他框架兼容时了解的,说说其区别之处。

  1)solon目前已经有一定规模了,有自己的生态圈了吧

  

  2)solon算的上新生态的开发框架了。

  优点:更快、更小、更简单。(主要体现在启动很快,目前基本在5S(不绝对,官网说5~10倍)以内,Qps10万+。打包更小,官方说小1/2~1/10。内存更小。简单主要是在开发上面)

  

  

  缺点:文档相对较少,虽然后官方实例,但是要完整的用起来还是得自己研究。且存在BUG。

  3)与springboot的区别

  a、与 Springboot 的常用注解比较

Solon 2.2.0Springboot 2.7.8说明
@Inject * @Autowired 注入Bean(by type)
@Inject("name") @Qualifier+@Autowired 注入Bean(by name)
@Inject("${name}") @Value("${name}") + @ConfigurationProperties(prefix="name") 注入配置
@Singleton @Scope(“singleton”) 单例(Solon 默认是单例)
@Singleton(false) @Scope(“prototype”) 非单例
     
@Import @Import + @ComponentScan 配置组件导入或扫描(一般加在启动类上)
@PropertySource @PropertySource 配置属性源(一般加在启动类上)
     
@Configuration @Configuration 配置类
@Bean @Bean 配置Bean
@Condition @ConditionalOnClass + @ConditionalOnProperty 配置条件
     
@Controller @Controller,@RestController 控制器类
@Remoting   远程控制器类(即 Rpc 服务端)
@Mapping ... @RequestMapping,@GetMapping... 映射
@Param @RequestParam 请求参数
@Header @RequestHeader 请求头
@Body @RequestBody 请求体
@Cookie @CookieValue 请求Cookie
     
@Component @Component 普通托管组件
@ProxyComponent @Service,@Dao,@Repository 代理托管组件
     
@TestPropertySource @TestPropertySource 配置测试属性源
@TestRollback @TestRollback 执行测试回滚
     
LifecycleBean InitializingBean + DisposableBean 组件初始化和销毁
     
Solon 2.2.0 Java EE(Java 11 后更名为 Jakarta)  
LifecycleBean::start @PostConstruct 组件构造完成并注入后的初始化
LifecycleBean::stop @PreDestroy 组件销毁

  

  b、重要的区别,Solon 不是基于 Servlet 的开发框架

   

  c、其他区别也比较多。这里不一一说明。参考地址:https://solon.noear.org/article/compare-springboot

   三、开发部分说明:(这里只有cloud相关的,单体的相对简单)

  1)集群注册与发现,以nacos为例。

        <dependency>
            <groupId>org.noear</groupId>
            <artifactId>nacos-solon-cloud-plugin</artifactId>
        </dependency>

  2)配置app.yml(测试只能在resources下),具体配置:https://solon.noear.org/article/148

solon:
  app:
    name: demo
    group: xbd
    namespace: public
  cloud:
    nacos:
      server: 127.0.0.1:8848

  3)rpc适用简单,优点类似dubbo的调用方式,抽公共的接口就行。

        <dependency>
            <groupId>org.noear</groupId>
            <artifactId>solon-rpc</artifactId>
        </dependency>

  server

@Service
@Mapping("/user")
@Remoting
public class UserServiceImpl implements IUserService {

    @Tran
    public List<String> listUser() {
        List<String> users = new ArrayList<String>();
        for (int i = 0; i < 5; i++) {
            users.add("user" + i);
        }
        return users;
    }
}

  client

    @NamiClient(name = "demo", path = "/user", headers = ContentTypes.PROTOBUF)
    private IUserService userService;

  

   4)认证(jwt)

        <dependency>
            <groupId>org.noear</groupId>
            <artifactId>solon.auth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.noear</groupId>
            <artifactId>solon.sessionstate.jwt</artifactId>
        </dependency>

  配置

server:
  port: 8673
  session:
    #超时配置。单位秒(可不配,默认:7200timeout: 86400
    state:
      #可共享域配置(可不配,默认当前服务域名;多系统共享时要配置)
      domain: "xbd.auth.com"
      jwt:
        #Jwt 令牌变量名;(可不配,默认:TOKEN)
        name: "Authorization"
        #Jwt 密钥(使用 JwtUtils.createKey() 生成);(可不配,默认:xxx)
        secret: "QQRsCNRmPqZQtAo5ANqZ9OgG5N2mOqZl5D3i1VSDvJs="
        #Jwt 令牌前缀(可不配,默认:空)
        prefix: "Bearer"
        #Jwt 允许超时;(可不配,默认:true);false,则token一直有效
        allowExpire: true
        #Jwt 允许自动输出;(可不配,默认:true);flase,则不向header 或 cookie 设置值(由用户手动控制)
        allowAutoIssue: false
        #Jwt 允许使用Header传递;(可不配,默认:使用 Cookie 传递);true,则使用 header 传递
        allowUseHeader: true
@Controller
@Mapping("/auth")
public class AuthController {

    @Inject
    private IUserService userService;

    @Get
    @Mapping("/test")
    public Result test(Context context) {
        return Result.succeed(context.session("userId"));
    }

    @Post
    @Mapping("/login")
    public Result login(Context context, @Body JSONObject jsonObject) {
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        boolean result = userService.checkUser(context, username, password);
        return result ? Result.succeed() : Result.failure("密码错误!");
    }
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public boolean checkUser(Context context, String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = baseMapper.selectOne(wrapper);
        if (user == null) {
            throw new ApiException("用户不存在");
        }
        boolean check = DigestUtil.bcryptCheck(password, user.getPassword());
        if (check) {
            context.sessionSet("userId", user.getId());
            context.sessionSet("username", user.getUsername());
            context.cookieSet("TOKEN", context.sessionState().sessionToken());
        }
        return check;
    }
}
@Configuration
public class AuthConfiguration {

    @Bean
    public AuthAdapter authAdapter() {
        return new AuthAdapter()
//            .loginUrl("/login") //设定登录地址,未登录时自动跳转(如果不设定,则输出401错误)
            .addRule(r -> r.include("/**").exclude("/auth/login").verifyLogined()) //添加规则
            .processor(new AuthProcessor() {
                @Override
                public boolean verifyIp(String ip) {
                    return false;
                }

                @Override
                public boolean verifyLogined() {
                    Context context = ContextUtil.current();
                    if (context.session("userId") != null) {
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean verifyPath(String path, String method) {
                    return false;
                }

                @Override
                public boolean verifyPermissions(String[] permissions, Logical logical) {
                    return false;
                }

                @Override
                public boolean verifyRoles(String[] roles, Logical logical) {
                    return false;
                }
            }).failure((ctx, rst) -> {
                System.out.println(rst);
                ctx.output(JSONObject.toJSONString(rst));
            });
    }

}

   参考地址:https://solon.noear.org/article/59 https://solon.noear.org/article/132

  说实话这个认证太简单了,二次校验等等问题,相对于security真的太轻量级了。在结合redis缓存,基本可以最到登陆和登出的一些判定了。

  5)多数据源:地址:https://solon.noear.org/article/231

        <dependency>
            <groupId>org.noear</groupId>
            <artifactId>mybatis-plus-extension-solon-plugin</artifactId>
        </dependency>
@Configuration
public class DataSourceConfiguration {

    @Bean(name = "default", typed = true)
    public DataSource dataSource(@Inject("${datasource.default}") HikariDataSource dataSource){
        return dataSource;
    }
}

  不多说,应用就会了。

  6)网关(官网推荐nginx等三方产品):说实话这个就真的比较一般了。数据转发出现乱码的情况,改用其他的就正常。

        <dependency>
            <groupId>org.noear</groupId>
            <artifactId>solon-api</artifactId>
        </dependency>
        <!-- 适用httputils乱码 -->
        <dependency>
            <groupId>org.noear</groupId>
            <artifactId>solon.cloud.httputils</artifactId>
        </dependency>

  

@Slf4j
@Component
@Mapping("/**")
public class RouterConfiguration extends Gateway {

    @Override
    protected void register() {
        //添加个前置处理
        before(ctx -> {
            String token = ctx.sessionState().sessionToken();
            System.out.println(token);
        });

        //添加缺省处理
        add(Any.class);
    }

    public static class Any {

        @Mapping
        public String any(Context ctx) throws Throwable {
            //检测请求,并尝试获取二级接口服务名
            String serviceName = ctx.pathMap("/{serviceName}/**").get("serviceName");
            if (serviceName == null) {
                log.error(MessageFormat.format("{0} service not found", serviceName));
                throw new ApiException(500, "服务异常!");
            }

            //转发请求
            String method = ctx.method();
            String server = LoadBalance.get(serviceName).getServer();
            if (server == null) {
                log.error(MessageFormat.format("{0} service not exception", serviceName));
                throw new ApiException(500, "服务异常!");
            }
            HttpRequest request = HttpUtil.createRequest(Method.valueOf(method), server + ctx.path().substring(StrUtil.indexOf(ctx.path(), '/', 2)));
            request.addHeaders(ctx.headerMap());
            request.header(CloudClient.trace().HEADER_TRACE_ID_NAME(), CloudClient.trace().getTraceId());
            request.body(ctx.body());
            HttpResponse response = request.execute();
            if (200 == response.getStatus()) {
                return response.body();
            } else {
                throw new ApiException(response.getStatus(), response.body());
            }

        }
    }

    @Override
    public void render(Object obj, Context context) throws Throwable {
        if (context.getRendered()) {
            return;
        }

        context.setRendered(true);

        Object result;
        if (obj instanceof ApiException) {
            ApiException exception = (ApiException) obj;
            result = exception.data();
        } else {
            //处理java bean数据(为扩展新的)
            result = obj;
        }
        context.result = result;
        //如果想对输出时间点做控制,可以不在这里渲染(由后置处理进行渲染)
        context.render(context.result);
    }
}
public class ApiException extends DataThrowable {

    public ApiException() {
        super.data(Result.failure());
    }

    public ApiException(int code, String message) {
        super.data(Result.failure(code, message));
    }

    public ApiException(Throwable cause) {
        super(cause);
        super.data(Result.failure(cause.getMessage()));
    }

    public ApiException(String message) {
        super(message);
        super.data(Result.failure(message));
    }

    public ApiException(String message, Throwable cause) {
        super(message, cause);
        super.data(Result.failure(message));
    }


}

  四、开发的几个重要部分,简单搞了一下,其他的都有对应策略,这里不细讲了。

  官网地址:https://solon.noear.org/

  五、总体来说这套框架还是非常优秀的。但是还是存在很多bug吧,个人准备弄着玩。

  demo地址:https://gitee.com/lilin409546297/solon-demo

 

posted @ 2023-05-12 11:28  小不点丶  阅读(1397)  评论(0编辑  收藏  举报