bkbk Knowledge Base

website·Indexed 31 days ago

https://bkbk-site.netlify.app

A technical knowledge base covering Java development, including JVM structure, Spring, Spring Cloud, MyBatis, Redis, and RocketMQ. The site also features guides on Vue, algorithm implementations (such as Quicksort and KMP), and deployment tutorials for ECS and FTP.

Tokens
24.5K
Snippets
106
Records
139
Agent score
75%

What's inside bkbk-site

  1. Overview of Server-Sent Events (SSE)

    Server-Sent Events (SSE) is a lightweight alternative to WebSockets that uses the standard HTTP protocol for one-way server-to-client message pushing. Unlike WebSockets, which are bidirectional, SSE allows the server to keep a connection open and stream data to the browser. It is simpler to implement than WebSockets but is limited to unidirectional communication.
  2. Overview of Spring Cloud Gateway

    Spring Cloud Gateway is the second-generation gateway for Spring Cloud, designed to replace Zuul. It is built on Netty, Reactor, and WebFlux, providing high performance (approximately 1.6x that of Zuul 1.x) and built-in features for request forwarding, monitoring, and rate limiting.

    Key constraints and requirements:

    • It uses a non-Servlet programming model (Netty/WebFlux), which may require a learning curve for developers used to Servlets.
    • It cannot run in a Servlet container and cannot be packaged as a WAR file.
    • It does not support Spring Boot 1.x.
  3. Understand the Java Class object and Class Loading

    In Java, every loaded class has a corresponding Class object that contains metadata about the class. This object is created when the .class file is loaded into the JVM at runtime. A single Class instance exists per loaded class in the JVM. The Class object serves as the entry point for the Reflection API, allowing programs to dynamically inspect and manipulate classes.
  4. Redis Overview and Data Types

    Redis (Remote Dictionary Server) is an open-source, in-memory, key-value NoSQL database written in ANSI C. It supports network access and persistence via RDB and AOF. While traditionally single-threaded (using I/O multiplexing to handle concurrency), version 6.x introduced multi-threading support.

    Core Data Types:

    • String: Basic key-value pairs.
    • List: Ordered lists of strings.
    • Set: Unordered collections of unique strings.
    • Hash (Map): Field-value pairs associated with a key.
    • ZSet (Sorted Set): Unique strings associated with a score for sorting.

    Specialized Data Types:

    • Geospatial: Used for calculating geographic location information (Redis 3.2+).
    • HyperLogLog: Probabilistic cardinality counting with a fixed memory footprint of 12KB (Redis 2.8.9+).
    • BitMap: Efficiently stores binary states using bitwise operations.
  5. Integrate RocketMQ with Spring Cloud Stream

    Use spring-cloud-starter-stream-rocketmq to implement a message-driven architecture. This requires the @EnableBinding(Source.class) annotation on the startup class and using the Source interface to send messages.
    <!-- Dependency -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
    </dependency>
    # Configuration
    cloud:
      stream:
        rocketmq:
          binder:
            name-server: 192.168.78.134:9876
            group: mail-group
          bindings:
            output:
              destination: mail-group
    // Startup class
    @EnableBinding(Source.class)
    @SpringBootApplication
    public class Application { ... }
    
    // Service implementation
    @Autowired
    private Source source;
    
    @GetMapping("/mail")
    public ResultVo mail(){
        source.output().send(MessageBuilder.withPayload("group mail aaaa").build());
        return ResultVo.DEFAULT_SUCCESS;
    }
  6. Customize Docusaurus layouts using React

    Docusaurus is powered by React, allowing developers to extend or customize the website layout. You can create custom components or layouts while maintaining the existing header and footer.
  7. Integrate RocketMQ with Spring Cloud Stream

    Use spring-cloud-starter-stream-rocketmq to implement a message-driven architecture using Spring Cloud Stream bindings. This approach uses Source for producing messages and Sink for consuming them.
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
    </dependency>

    application.yml

    cloud: stream: rocketmq: binder: name-server: 192.168.78.134:9876 group: mail-group bindings: output: destination: mail-group

    Producer Implementation

    @EnableBinding(Source.class) public class ProducerService { @Autowired private Source source;

    @GetMapping("/mail")
    public ResultVo mail(){
        source.output().send(MessageBuilder.withPayload("group mail aaaa").build());
        return ResultVo.DEFAULT_SUCCESS;
    }

    }

  8. Understand Java's Memory Safety and Runtime Design

    Java is designed as a memory-safe language to prevent common issues like buffer overflows, memory leaks, and the use of uninitialized variables. It achieves this through:

    • Runtime Error Detection: For example, throwing ArrayIndexOutOfBoundsException.
    • Pointer Restrictions: Preventing direct memory manipulation.

    Despite being a 'safe, managed runtime', Java provides 'backdoors' for high-performance or low-level access via:

    • JNI (Java Native Interface): For calling native code.
    • sun.misc.Unsafe: For low-level memory and concurrency operations.
  9. Obtain a Class object in Java

    There are several ways to obtain a Class object depending on the available information:

    1. Using the .class literal: The most performant and safest method when the class is known at compile time.
    2. Using getClass(): Used when you have an existing instance of the class.
    3. Using Class.forName(): Used when you only have the fully qualified name of the class as a string. This may throw a ClassNotFoundException.
    4. Using .TYPE: Used for primitive data types (e.g., Integer.TYPE).
    // 1. Class literal
    Class<?> c1 = Person.class;
    
    // 2. From instance
    Person person = new Person();
    Class<?> c2 = person.getClass();
    
    // 3. From fully qualified name
    Class<?> c3 = Class.forName("com.liu.User");
    
    // 4. Primitive type
    Class<?> c4 = Integer.TYPE;
  10. Create a blog post in Docusaurus

    Docusaurus automatically generates a dedicated page for every blog post file created, along with a blog index page, a tag system, and an RSS feed. To create a post, add a markdown file to the blog/ directory. The filename typically follows a date-based naming convention (e.g., YYYY-MM-DD-title.md).
    ---
    slug: greetings
    title: Greetings!
    authors:
      - name: Joel Marcey
        title: Co-creator of Docusaurus 1
        url: https://github.com/JoelMarcey
        image_url: https://github.com/JoelMarcey.png
      - name: Sébastien Lorber
        title: Docusaurus maintainer
        url: https://sebastienlorber.com
        image_url: https://github.com/slorber.png
    tags: [greetings]
    ---
    
    Congratulations, you have made your first post!
    
    Feel free to play around and edit this post as much you like.
  11. Integrate RocketMQ with Spring Boot using rocketmq-spring-boot-starter

    Use the rocketmq-spring-boot-starter to integrate RocketMQ into a Spring application. This involves adding the dependency, configuring the NameServer and producer group, and using RocketMQTemplate to send messages.
    <!-- Dependency -->
    <dependency> 
        <groupId>org.apache.rocketmq</groupId> 
        <artifactId>rocketmq-spring-boot-starter</artifactId> 
        <version>2.0.3</version> 
    </dependency>
    # Configuration
    rocketmq:
      name-server: 192.168.78.134:9876
      producer:
        group: test-group
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    
    @GetMapping("/mq")
    public ResultVo mq(){
        List<Integer> x = new ArrayList<>();
        x.add(1); x.add(2);
        Group a = new Group(3, "awd", x);
        // Send message to topic "gg"
        rocketMQTemplate.convertAndSend("gg", a);
        return ResultVo.DEFAULT_SUCCESS;
    }
  12. Read annotations using Reflection

    Reflection can be used to retrieve annotations from a class, which is a core mechanism for ORM (Object Relationship Mapping) frameworks. Use getAnnotations() to get all annotations or getAnnotation(Class<A> annotationClass) to retrieve a specific annotation and access its values.
    Class<?> c = Class.forName("com.liu.Student");
    
    // Get all annotations
    Annotation[] annotations = c.getAnnotations();
    
    // Get a specific annotation and its value
    Tableliu tableliu = (Tableliu) c.getAnnotation(Tableliu.class);
    String value = tableliu.value();