Jboot Framework Documentation

repository·master·Indexed 20 days ago

https://github.com/yangfuhai/jboot

A comprehensive Java microservices framework built on JFinal, Dubbo, Seata, Sentinel, ShardingSphere, and Nacos. Jboot provides a complete ecosystem for distributed, scalable applications, featuring MVC + ORM, distributed transactions, RPC implementation, flow control, configuration management, and two-level caching. It includes developer tools for hot-reloading, code generation, and API documentation, as well as JbootAdmin for rapid enterprise development scaffolding.

Tokens
67.1K
Snippets
193
Records
244
Agent score
72%

What's inside Jboot

  1. Overview of Jboot Microservices Framework

    master

    Jboot is a microservices framework built upon several industry-standard technologies including JFinal, JFinal-Undertow, Dubbo, Seata, Sentinel, ShardingSphere, and Nacos. It is designed to lower the barrier to entry for microservices development.

    Key features include:

    • MVC + ORM: Rapid development based on JFinal.
    • Distributed Transactions & Sharding: Support for distributed transactions and database sharding via ShardingSphere and Seata.
    • RPC Implementation: Remote Procedure Call support using Dubbo or Motan.
    • Flow Control: Distributed rate limiting and degradation via Sentinel.
    • Configuration Management: Distributed configuration centers using Apollo and Nacos.
    • Caching: Distributed two-level caching using EhCache and Redis.
    • Developer Experience: Supports hot-reloading for Java code, HTML, CSS, and JS files in IDEA and Eclipse across multi-Maven module projects.
  2. Overview of Jboot features

    master

    Jboot is a domestic framework built upon JFinal, Dubbo, Seata, Sentinel, ShardingSphere, and Nacos. Key capabilities include:

    • MVC & ORM: Full support via JFinal.
    • Data Management: Supports multiple data sources, database sharding (splitting tables/databases), and distributed transactions.
    • RPC: Full Dubbo RPC functionality.
    • Resilience: Single-point and distributed rate limiting (Sentinel).
    • Configuration: Distributed configuration via Apollo and Nacos.
    • Distributed Services: Support for distributed caching, sessions, and attachments.
    • Gateway: Built-in powerful portal gateway.
    • DevOps: Full unit testing support, code generation tools, API documentation generation, and Docker/K8S compatibility.
  3. Overview of Jboot Framework

    master

    Jboot is a comprehensive Java framework built upon several industry-standard technologies including JFinal, Dubbo, Seata, Sentinel, ShardingSphere, and Nacos. It is designed to provide a complete ecosystem for enterprise application development.

    Key capabilities include:

    • MVC & ORM: Full support via JFinal.
    • Data Management: Supports multiple data sources, database sharding (splitting), and table sharding.
    • Distributed Systems: Full Dubbo RPC functionality, distributed transactions (via Seata), and distributed configuration management (via Apollo and Nacos).
    • Resilience & Scaling: Single-point and distributed rate limiting (via Sentinel), and distributed caching, sessions, and attachment support.
    • Infrastructure: Built-in powerful gateway, full unit testing support, and high compatibility with Docker and Kubernetes (K8S).
    • Developer Tools: Includes robust code generation tools and API documentation generation tools.
  4. Configure Host-based Routing

    master

    Host routing matches requests based on the domain name (host). Jboot provides four matching modes:

    1. hostEquals: Matches the exact domain. Example: xxx.xxx.com matches xxx.xxx.com/user/xx but not www.xxx.com/user/xxx.
    2. hostContains: Matches if the string is part of the domain. Example: xxx.xxx.com matches aaa.bbb.xxx.xxx.com.
    3. hostStartsWith: Matches if the domain starts with the string. Example: xxx matches xxx.xxx.com but not www.xxx.com.
    4. hostEndsWith: Matches if the domain ends with the string. Example: com matches www.xxx.com but not www.xxx.org.
    jboot.gateway.name = name
    jboot.gateway.uri = http://youdomain:8080
    jboot.gateway.enable = true
    
    # Example: Host contains xxx.com
    jboot.gateway.hostContains = xxx.com
  5. How Attachment management works in distributed systems

    master

    In distributed deployments (e.g., multiple servers behind Nginx or SLB), a file uploaded to Server A might not be available if the user's next request is routed to Server B. Jboot solves this using the AttachmentManager and AttachmentContainer abstractions.

    • AttachmentContainer: A specialized container responsible for storing, reading, and rendering images/files.
    • AttachmentManager: A manager that orchestrates multiple AttachmentContainer instances. An application can have several containers (e.g., one for local storage, one for Aliyun OSS, one for FastDFS).

    When saving a file via AttachmentManager.me().saveFile(file), the manager propagates the save operation to all registered containers. When reading a file via AttachmentManager.me().getFile(relativePath), the manager first checks the default container. If the file is not found there, it iterates through all other registered containers until the file is located.

    // Conceptual flow of file retrieval:
    // 1. Check Default Container
    // 2. If not found, iterate through all other registered AttachmentContainers
    // 3. Return the File if found, otherwise return null
  6. Configure Query-based Routing

    master

    Query routing matches requests based on GET request parameters. Note: POST request parameters are not supported for routing.

    1. queryEquals: Matches specific key-value pairs. Example: aaa:bbb matches www.xxx.com/controller?aaa=bbb but not www.xxx.com/controller?aaa=ccc.
    2. queryContains: Matches if the key exists in the query string. Example: aaa matches www.xxx.com/controller?aaa=bbb or www.xxx.com/controller?aaa=ccc.
    jboot.gateway.name = name
    jboot.gateway.uri = http://youdomain:8080
    jboot.gateway.enable = true
    
    # Example: Query parameter aaa must equal bbb
    jboot.gateway.queryEquals = aaa:bbb
  7. Use automatic injection with @Bean and @Inject

    master

    Jboot provides robust dependency injection via Google Guice using two primary annotations:

    1. @Bean: Marks a class as available for automatic injection. Service layer classes generated by the code generator typically include @Bean and @Singleton by default.
    2. @Inject: Used on a field to perform property injection.

    Example of a service implementation:

    @Bean
    public class UserServiceImpl extends JbootServiceBase<User> implements UserService {
    }

    Example of injecting that service into a controller:

    @RequestMapping("/")
    public class IndexController extends JbootController {
    
        @Inject
        private UserService userService;
    
        public void users() {
            List<User> users = userService.findAll();
            renderText(Arrays.toString(users.toArray()));
        }
    }
    @Bean
    public class UserServiceImpl extends JbootServiceBase<User> implements UserService {
    }
    
    @RequestMapping("/")
    public class IndexController extends JbootController {
    
        @Inject
        private UserService userService;
    }
  8. Understand Dubbo3 Service Introspection

    master

    When using Dubbo3 with application-level registration (registerMode=instance), the registry center only stores application instance information, not specific interface service information.

    Instead, interface service information is stored in a Metadata Center, which maintains the mapping between instances and interfaces. Consumers use this mapping to discover required services. This mechanism is known as Service Introspection (服务自省) and is designed to maintain compatibility between Dubbo2 and Dubbo3.

  9. Leverage automatic return value rendering in Controllers

    master

    Jboot enhances the standard Controller by allowing methods to return different types, which are then automatically rendered to the client. This reduces the need for explicit render... calls.

    Supported return types:

    • String:
      • Returns a filename (e.g., "test.html") to render an HTML template.
      • Returns "redirect: /path" for automatic redirection.
      • Returns "forward: /path" for action forwarding.
      • Returns error strings like "error: 404" or "error: 500" to render specific error states.
      • Returns plain text (e.g., "test2...").
    • File: Automatically triggers a file download.
    • Object (POJO/Map): Automatically renders the object as JSON.
    • Render: Returns a specific JFinal Render object (e.g., TextRender).
    @RequestMapping("/")
    public class MyController extends Controller {
    
        // Renders test1.html
        public String test1() {
            return "test1.html";
        }
    
        // Automatic redirect
        public String test5() {
            return "redirect: /to/your/path";
        }
    
        // Automatic file download
        public File test7() {
            return new File("/file/path");
        }
    
        // Automatic JSON rendering
        public Object test8() {
            Map<String,Object> map = new HashMap<>();
            map.put("key","value");
            return map;
        }
    }
  10. Use comma-separated values for multiple routing patterns

    master

    When a configuration key accepts multiple values (such as pathContains), separate the values using an English comma (,). For example, if you configure jboot.gateway.pathContains = /user,/article, the gateway will match any request containing either /user or /article (e.g., www.xxx.com/user/xxx or www.xxx.com/article/xxx).

    jboot.gateway.name = name
    jboot.gateway.uri = http://youdomain:8080
    jboot.gateway.enable = true
    jboot.gateway.pathContains = /user,/article
  11. Synchronize cache with database using @CacheEvict

    master

    When using @Cacheable to cache database query results, the cache can become stale if the underlying data is modified (inserted, updated, or deleted).

    To ensure the cache stays synchronized with the database, you should override the mutation methods (like save, update, or delete) from JbootServiceBase and annotate them with @CacheEvict.

    Using key = "*" with @CacheEvict will clear all keys within the specified cache name, ensuring that subsequent queries fetch fresh data from the database.

    @Override
    @CacheEvict(name = "myCache", key = "*")
    public boolean update(User model) {
        return super.update(model);
    }