类似Spring Boot的开源框架jboot

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

jboot

jboot is a similar springboot project base on jfinal and undertow,we have using in product environment.

jboot 中文描述

jboot是一个基于jfinal、undertow开发的一个类似springboot的开源框架, 我们已经在正式的商业上线项目中使用。她集成了代码生成,微服务,MQ,RPC,监控等功能, 开发者使用及其简单。

maven dependency

<dependency>
    <groupId>io.jboot</groupId>
    <artifactId>jboot</artifactId>
    <version>${last_version}</version>
</dependency>

controller example

new a controller

@RequestMapping("/")
public class MyController extend JbootController{
   public void index(){
        renderText("hello jboot");
   }
}

start

public class MyStarter{
   public static void main(String [] args){
       Jboot.run(args);
   }
}

visit: http://127.0.0.1:8088

mq example

config jboot.properties

#type default redis (support: redis,activemq,rabbitmq,hornetq,aliyunmq )
jboot.mq.type = redis
jboot.mq.redis.address = 127.0.0.1
jboot.mq.redis.password =
jboot.mq.redis.database =

server a sendMqMessage

Jboot.getMq().publish(yourObject, toChannel);

server b message listener

Jboot.getMq().addMessageListener(new JbootmqMessageListener(){
        @Override
        public void onMessage(String channel, Object obj) {
           System.out.println(obj);
        }
}, channel);

rpc example

config jboot.properties

#type default motan (support:local,motan,grpc,thrift)
jboot.rpc.type = motan
jboot.rpc.requestTimeOut
jboot.rpc.defaultPort
jboot.rpc.defaultGroup
jboot.rpc.defaultVersion
jboot.rpc.registryType = consul
jboot.rpc.registryName
jboot.rpc.registryAddress = 127.0.0.1:8500

define interface

public interface HelloService {
    public String hello(String name);
}

server a export serviceImpl

@JbootrpcService
public class myHelloServiceImpl extends JbootService implements HelloService {
    public String hello(String name){
         System.out.println("hello" + name);
         return "hello ok";
    }
}

server b call

HelloService service = Jboot.service(HelloService.class);
 service.hello("michael");

cache example

config jboot.properties

#type default ehcache (support:ehcache,redis,ehredis (ehredis:tow level cache,ehcache level one and redis level tow))
jboot.cache.type = redis
jboot.cache.redis.address =
jboot.cache.redis.password =
jboot.cache.redis.database =

use cache

Jboot.getCache().put("cacheName", "key", "value");

database access example

config jboot.properties

#type default mysql (support:mysql,oracle,db2...)
jboot.datasource.type=
jboot.datasource.url=
jboot.datasource.user=
jboot.datasource.password=
jboot.datasource.driverClassName=
jboot.datasource.connectionInitSql=
jboot.datasource.cachePrepStmts=
jboot.datasource.prepStmtCacheSize=
jboot.datasource.prepStmtCacheSqlLimit=

define model

@Table(tableName = "user", primaryKey = "id")
public class User extends JbootModel<User> {
    
}

dao query

public class UserDao {
    public static find User DAO = new User();
    
    public User findById(String id){
        return DAO.findById(id);
    }
    
    public List<User> findByNameAndAge(String name,int age){
        return DAO.findListByColums(Columns.create().like("name","%"+name+"%").gt("age",age));
    }
}

event example

send event

Jboot.sendEvent(actionStr,  dataObj)

event listener

@EventConfig(action = {User.ACTION_ADD,User.ACTION_DELETE})
public class MyEventListener implements JbootEventListener {
    
    public  void onMessage(JbootEvent event){
        
        if(event.getAction.equals(User.ACTION_ADD)){
            System.out.println("new user add, user:"+event.getData);
        }else if(event.getAction.equals(User.ACTION_DELETE)){
            System.out.println("user deleted, user:"+event.getData);
        }
        
    }
    
}

read config

config jboot.properties

jboot.myconfig.user = aaa
jboot.myconfig.password = bbb

define config model

@PropertieConfig(prefix = "jboot.myconfig")
public class MyConfig {

    private String name;
    private String password;
    
    // getter and setter
}

get config model

MyConfig config = Jboot.config(MyConfig.class);
    System.out.println(config.getName());

code generator

public static void main(String[] args) {

        String modelPackage = "io.jboot.test";

        String dbHost = "127.0.0.1";
        String dbName = "yourDbName";
        String dbUser = "root";
        String dbPassword = "";

        JbootModelGenerator.run(modelPackage, dbHost, dbName, dbUser, dbPassword);

    }

build

config pom.xml

<build>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <configuration>
                   <source>1.8</source>
                   <target>1.8</target>
               </configuration>
           </plugin>


           <plugin>
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>appassembler-maven-plugin</artifactId>
               <version>1.10</version>
               <configuration>


                   <assembleDirectory>${project.build.directory}/app</assembleDirectory>
                   <repositoryName>lib</repositoryName>
                   <binFolder>bin</binFolder>
                   <configurationDirectory>webRoot</configurationDirectory>
                   <copyConfigurationDirectory>true</copyConfigurationDirectory>
                   <configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
                   <repositoryLayout>flat</repositoryLayout>
                   <encoding>UTF-8</encoding>
                   <logsDirectory>logs</logsDirectory>
                   <tempDirectory>tmp</tempDirectory>

                   <programs>
                       <program>
                           <mainClass>io.jboot.Jboot</mainClass>
                           <id>jboot</id>
                           <platforms>
                               <platform>windows</platform>
                               <platform>unix</platform>
                           </platforms>
                        
                   </programs>
               </configuration>
           </plugin>

       </plugins>

   </build>

maven build

mvn package appassembler:assemble

start app

cd yourProjectPath/target/app/bin
./jboot

start app and change config

cd yourProjectPath/target/app/bin
./jboot --jboot.server.port=8080 --jboot.rpc.type=local

use your properties replace jboot.properties

cd yourProjectPath/target/app/bin
./jboot --jboot.model=dev --jboot.server.port=8080

use jboot-dev.proerties replace jboot.properties and set jboot.server.port=8080

thanks

rpc framework:

  • motan
  • grpc
  • thrift

mq framework:

  • activemq
  • rabbitmq
  • redis mq
  • hornetq
  • aliyun mq

cache framework

  • ehcache
  • redis

core framework:

  • jfinal
  • undertow

author

  • name:yangfuhai
  • qq:1506615067
  • email:fuhai999@gmail.com

文章转载自 开源中国社区 [http://www.oschina.net]

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
27天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
40 0
|
6月前
|
缓存 前端开发 Java
深入理解Spring、Spring MVC、Spring Boot等开源框架
深入理解Spring、Spring MVC、Spring Boot等开源框架
|
6天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
8天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
【Spring系列】Sping VS Sping Boot区别与联系
|
2月前
|
XML 监控 druid
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
|
3月前
|
Java
springboot项目打包瘦身
springboot项目打包瘦身
|
5月前
|
Java 测试技术
Springboot集成JUnit5优雅进行单元测试
Springboot集成JUnit5优雅进行单元测试
|
安全 Java Maven
Spring Boot资源文件问题总结(Spring Boot的静态资源访问,配置文件外置)
Spring Boot资源文件问题总结(Spring Boot的静态资源访问,配置文件外置)
1304 1
|
9月前
|
Java Maven
【Springboot】创建boot工程spring-boot-maven-plugin报红、出错_解决方案
【Springboot】创建boot工程spring-boot-maven-plugin报红、出错_解决方案
317 0
|
9月前
|
SQL druid 前端开发
让SpringBoot不需要Controller、Service、DAO、Mapper,卧槽!这款工具绝了!
让SpringBoot不需要Controller、Service、DAO、Mapper,卧槽!这款工具绝了!