bkbk Knowledge Base
website·Indexed 31 days ago
https://bkbk-site.netlify.appA 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.
What's inside bkbk-site
- 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.
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.
Understand the Java Class object and Class Loading
In Java, every loaded class has a correspondingClassobject that contains metadata about the class. This object is created when the.classfile is loaded into the JVM at runtime. A singleClassinstance exists per loaded class in the JVM. TheClassobject serves as the entry point for the Reflection API, allowing programs to dynamically inspect and manipulate classes.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.
Integrate RocketMQ with Spring Cloud Stream
Usespring-cloud-starter-stream-rocketmqto implement a message-driven architecture. This requires the@EnableBinding(Source.class)annotation on the startup class and using theSourceinterface 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; }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.Integrate RocketMQ with Spring Cloud Stream
Usespring-cloud-starter-stream-rocketmqto implement a message-driven architecture using Spring Cloud Stream bindings. This approach usesSourcefor producing messages andSinkfor 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; }}
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.
- Runtime Error Detection: For example, throwing
Obtain a Class object in Java
There are several ways to obtain a
Classobject depending on the available information:- Using the
.classliteral: The most performant and safest method when the class is known at compile time. - Using
getClass(): Used when you have an existing instance of the class. - Using
Class.forName(): Used when you only have the fully qualified name of the class as a string. This may throw aClassNotFoundException. - 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;- Using the
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 theblog/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.Integrate RocketMQ with Spring Boot using rocketmq-spring-boot-starter
Use therocketmq-spring-boot-starterto integrate RocketMQ into a Spring application. This involves adding the dependency, configuring the NameServer and producer group, and usingRocketMQTemplateto 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; }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. UsegetAnnotations()to get all annotations orgetAnnotation(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();