Spring Boot 数据缓存与性能优化

Spring Boot 数据缓存与性能优化

Spring Boot 数据缓存与性能优化

在这里插入图片描述
23.1 学习目标与重点提示

学习目标:掌握Spring Boot数据缓存与性能优化的核心概念与使用方法,包括数据缓存的定义与特点、Spring Boot与数据缓存的集成、Spring Boot与数据缓存的配置、Spring Boot与数据缓存的基本方法、Spring Boot的实际应用场景,学会在实际开发中处理数据缓存与性能优化问题。
重点:数据缓存的定义与特点Spring Boot与数据缓存的集成Spring Boot与数据缓存的配置Spring Boot与数据缓存的基本方法Spring Boot的实际应用场景

23.2 数据缓存概述

数据缓存是Java开发中的重要组件。

23.2.1 数据缓存的定义

定义:数据缓存是一种存储机制,用于将常用数据存储在高速存储设备中,以便快速访问。
作用

  • 提高应用程序的性能。
  • 减少数据库的访问次数。
  • 提高用户体验。

常见的数据缓存

  • EhCache:Apache EhCache是一款开源的缓存库。
  • Caffeine:Caffeine是一款高性能的缓存库。
  • Redis:Redis是一款开源的缓存服务器。

✅ 结论:数据缓存是一种存储机制,作用是提高应用程序的性能、减少数据库的访问次数、提高用户体验。

23.2.2 数据缓存的特点

定义:数据缓存的特点是指数据缓存的特性。
特点

  • 高速访问:数据缓存提供高速访问。
  • 数据一致性:数据缓存提供数据一致性。
  • 可扩展性:数据缓存可以扩展到多个应用程序之间的缓存通信。
  • 易用性:数据缓存提供易用的编程模型。

✅ 结论:数据缓存的特点包括高速访问、数据一致性、可扩展性、易用性。

23.3 Spring Boot与数据缓存的集成

Spring Boot与数据缓存的集成是Java开发中的重要内容。

23.3.1 集成EhCache的步骤

定义:集成EhCache的步骤是指使用Spring Boot与EhCache集成的方法。
步骤

  1. 创建Spring Boot项目。
  2. 添加所需的依赖。
  3. 配置EhCache。
  4. 创建数据访问层。
  5. 创建业务层。
  6. 创建控制器类。
  7. 测试应用。

示例
pom.xml文件中的依赖:

<dependencies><!-- Web依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Data JPA依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- H2数据库依赖 --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><!-- EhCache依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.properties文件中的EhCache配置:

# 服务器端口 server.port=8080 # 数据库连接信息 spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password # JPA配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true # H2数据库控制台 spring.h2.console.enabled=true spring.h2.console.path=/h2-console # EhCache配置 spring.cache.type=ehcache spring.cache.ehcache.config=classpath:ehcache.xml 

ehcache.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?><ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"><defaultCachemaxEntriesLocalHeap="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="false"diskPersistent="false"diskExpiryThreadIntervalSeconds="120"></defaultCache><cachename="productCache"maxEntriesLocalHeap="1000"eternal="false"timeToIdleSeconds="60"timeToLiveSeconds="60"overflowToDisk="false"diskPersistent="false"diskExpiryThreadIntervalSeconds="120"></cache></ehcache>

实体类:

importjavax.persistence.*;@Entity@Table(name ="product")publicclassProduct{@Id@GeneratedValue(strategy =GenerationType.IDENTITY)privateLong id;privateString productId;privateString productName;privatedouble price;privateint sales;publicProduct(){}publicProduct(String productId,String productName,double price,int sales){this.productId = productId;this.productName = productName;this.price = price;this.sales = sales;}// Getter和Setter方法publicLonggetId(){return id;}publicvoidsetId(Long id){this.id = id;}publicStringgetProductId(){return productId;}publicvoidsetProductId(String productId){this.productId = productId;}publicStringgetProductName(){return productName;}publicvoidsetProductName(String productName){this.productName = productName;}publicdoublegetPrice(){return price;}publicvoidsetPrice(double price){this.price = price;}publicintgetSales(){return sales;}publicvoidsetSales(int sales){this.sales = sales;}@OverridepublicStringtoString(){return"Product{"+"id="+ id +",+ productId +'\''+",+ productName +'\''+", price="+ price +", sales="+ sales +'}';}}

Repository接口:

importorg.springframework.data.jpa.repository.JpaRepository;importorg.springframework.stereotype.Repository;@RepositorypublicinterfaceProductRepositoryextendsJpaRepository<Product,Long>{}

Service类:

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cache.annotation.CacheEvict;importorg.springframework.cache.annotation.CachePut;importorg.springframework.cache.annotation.Cacheable;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importjava.util.List;@ServicepublicclassProductService{@AutowiredprivateProductRepository productRepository;@Transactional@CachePut(value ="productCache", key ="#product.id")publicProductaddProduct(Product product){return productRepository.save(product);}@Transactional@CachePut(value ="productCache", key ="#product.id")publicProductupdateProduct(Product product){return productRepository.save(product);}@Transactional@CacheEvict(value ="productCache", key ="#id")publicvoiddeleteProduct(Long id){ productRepository.deleteById(id);}@Transactional(readOnly =true)@Cacheable(value ="productCache", key ="#id")publicProductgetProductById(Long id){System.out.println("从数据库查询产品:"+ id);return productRepository.findById(id).orElse(null);}@Transactional(readOnly =true)publicList<Product>getAllProducts(){return productRepository.findAll();}}

控制器类:

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importjava.util.List;@RestController@RequestMapping("/api/products")publicclassProductController{@AutowiredprivateProductService productService;@GetMapping("/")publicList<Product>getAllProducts(){return productService.getAllProducts();}@PostMapping("/")publicProductaddProduct(@RequestBodyProduct product){return productService.addProduct(product);}@PutMapping("/{id}")publicProductupdateProduct(@PathVariableLong id,@RequestBodyProduct product){ product.setId(id);return productService.updateProduct(product);}@DeleteMapping("/{id}")publicvoiddeleteProduct(@PathVariableLong id){ productService.deleteProduct(id);}@GetMapping("/{id}")publicProductgetProductById(@PathVariableLong id){return productService.getProductById(id);}}

应用启动类:

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCachingpublicclassProductApplication{publicstaticvoidmain(String[] args){SpringApplication.run(ProductApplication.class, args);}@AutowiredprivateProductService productService;publicvoidrun(String... args){// 初始化数据 productService.addProduct(newProduct("P001","手机",1000.0,100)); productService.addProduct(newProduct("P002","电脑",5000.0,50)); productService.addProduct(newProduct("P003","电视",3000.0,80)); productService.addProduct(newProduct("P004","手表",500.0,200)); productService.addProduct(newProduct("P005","耳机",300.0,150));}}

测试类:

importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.boot.test.web.client.TestRestTemplate;importorg.springframework.boot.web.server.LocalServerPort;importstaticorg.assertj.core.api.Assertions.assertThat;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)classProductApplicationTests{@LocalServerPortprivateint port;@AutowiredprivateTestRestTemplate restTemplate;@TestvoidcontextLoads(){}@TestvoidtestGetProductById(){Product product1 = restTemplate.getForObject("http://localhost:"+ port +"/api/products/1",Product.class);assertThat(product1).isNotNull();assertThat(product1.getProductId()).isEqualTo("P001");Product product2 = restTemplate.getForObject("http://localhost:"+ port +"/api/products/1",Product.class);assertThat(product2).isNotNull();assertThat(product2.getProductId()).isEqualTo("P001");}@TestvoidtestAddProduct(){Product product =newProduct("P006","平板",2000.0,70);Product savedProduct = restTemplate.postForObject("http://localhost:"+ port +"/api/products/", product,Product.class);assertThat(savedProduct).isNotNull();assertThat(savedProduct.getProductId()).isEqualTo("P006");}@TestvoidtestUpdateProduct(){Product product =newProduct("P001","手机",1500.0,120); restTemplate.put("http://localhost:"+ port +"/api/products/1", product);Product updatedProduct = restTemplate.getForObject("http://localhost:"+ port +"/api/products/1",Product.class);assertThat(updatedProduct).isNotNull();assertThat(updatedProduct.getPrice()).isEqualTo(1500.0);}@TestvoidtestDeleteProduct(){ restTemplate.delete("http://localhost:"+ port +"/api/products/2");Product product = restTemplate.getForObject("http://localhost:"+ port +"/api/products/2",Product.class);assertThat(product).isNull();}}

✅ 结论:集成EhCache的步骤包括创建Spring Boot项目、添加所需的依赖、配置EhCache、创建数据访问层、创建业务层、创建控制器类、测试应用。

23.4 Spring Boot与数据缓存的配置

Spring Boot与数据缓存的配置是Java开发中的重要内容。

23.4.1 配置Caffeine缓存

定义:配置Caffeine缓存是指使用Spring Boot与Caffeine缓存集成的方法。
步骤

  1. 创建Spring Boot项目。
  2. 添加所需的依赖。
  3. 配置Caffeine缓存。
  4. 创建数据访问层。
  5. 创建业务层。
  6. 创建控制器类。
  7. 测试应用。

示例
pom.xml文件中的依赖:

<dependencies><!-- Web依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Data JPA依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- H2数据库依赖 --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><!-- Caffeine依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.properties文件中的Caffeine缓存配置:

# 服务器端口 server.port=8080 # 数据库连接信息 spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password # JPA配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true # H2数据库控制台 spring.h2.console.enabled=true spring.h2.console.path=/h2-console # Caffeine缓存配置 spring.cache.type=caffeine spring.cache.caffeine.spec=maximumSize=1000,expireAfterWrite=60s 

控制器类:

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importjava.util.List;@RestController@RequestMapping("/api/products")publicclassProductController{@AutowiredprivateProductService productService;@GetMapping("/")publicList<Product>getAllProducts(){return productService.getAllProducts();}@PostMapping("/")publicProductaddProduct(@RequestBodyProduct product){return productService.addProduct(product);}@PutMapping("/{id}")publicProductupdateProduct(@PathVariableLong id,@RequestBodyProduct product){ product.setId(id);return productService.updateProduct(product);}@DeleteMapping("/{id}")publicvoiddeleteProduct(@PathVariableLong id){ productService.deleteProduct(id);}@GetMapping("/{id}")publicProductgetProductById(@PathVariableLong id){return productService.getProductById(id);}}

测试类:

importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.boot.test.web.client.TestRestTemplate;importorg.springframework.boot.web.server.LocalServerPort;importstaticorg.assertj.core.api.Assertions.assertThat;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)classProductApplicationTests{@LocalServerPortprivateint port;@AutowiredprivateTestRestTemplate restTemplate;@TestvoidcontextLoads(){}@TestvoidtestGetProductById(){Product product1 = restTemplate.getForObject("http://localhost:"+ port +"/api/products/1",Product.class);assertThat(product1).isNotNull();assertThat(product1.getProductId()).isEqualTo("P001");Product product2 = restTemplate.getForObject("http://localhost:"+ port +"/api/products/1",Product.class);assertThat(product2).isNotNull();assertThat(product2.getProductId()).isEqualTo("P001");}@TestvoidtestAddProduct(){Product product =newProduct("P006","平板",2000.0,70);Product savedProduct = restTemplate.postForObject("http://localhost:"+ port +"/api/products/", product,Product.class);assertThat(savedProduct).isNotNull();assertThat(savedProduct.getProductId()).isEqualTo("P006");}@TestvoidtestUpdateProduct(){Product product =newProduct("P001","手机",1500.0,120); restTemplate.put("http://localhost:"+ port +"/api/products/1", product);Product updatedProduct = restTemplate.getForObject("http://localhost:"+ port +"/api/products/1",Product.class);assertThat(updatedProduct).isNotNull();assertThat(updatedProduct.getPrice()).isEqualTo(1500.0);}@TestvoidtestDeleteProduct(){ restTemplate.delete("http://localhost:"+ port +"/api/products/2");Product product = restTemplate.getForObject("http://localhost:"+ port +"/api/products/2",Product.class);assertThat(product).isNull();}}

✅ 结论:配置Caffeine缓存是指使用Spring Boot与Caffeine缓存集成的方法,步骤包括创建Spring Boot项目、添加所需的依赖、配置Caffeine缓存、创建数据访问层、创建业务层、创建控制器类、测试应用。

23.5 Spring Boot与数据缓存的基本方法

Spring Boot与数据缓存的基本方法包括使用@Cacheable、@CachePut、@CacheEvict注解。

23.5.1 使用@Cacheable注解

定义:使用@Cacheable注解是指Spring Boot与数据缓存集成的基本方法之一。
作用

  • 实现数据缓存。
  • 提高应用程序的性能。

示例

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cache.annotation.Cacheable;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importjava.util.List;@ServicepublicclassProductService{@AutowiredprivateProductRepository productRepository;@Transactional(readOnly =true)@Cacheable(value ="productCache", key ="#id")publicProductgetProductById(Long id){System.out.println("从数据库查询产品:"+ id);return productRepository.findById(id).orElse(null);}@Transactional(readOnly =true)publicList<Product>getAllProducts(){return productRepository.findAll();}}

✅ 结论:使用@Cacheable注解是指Spring Boot与数据缓存集成的基本方法之一,作用是实现数据缓存、提高应用程序的性能。

23.5.2 使用@CachePut注解

定义:使用@CachePut注解是指Spring Boot与数据缓存集成的基本方法之一。
作用

  • 实现数据缓存。
  • 提高应用程序的性能。

示例

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cache.annotation.CachePut;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importjava.util.List;@ServicepublicclassProductService{@AutowiredprivateProductRepository productRepository;@Transactional@CachePut(value ="productCache", key ="#product.id")publicProductaddProduct(Product product){return productRepository.save(product);}@Transactional@CachePut(value ="productCache", key ="#product.id")publicProductupdateProduct(Product product){return productRepository.save(product);}@Transactional(readOnly =true)@Cacheable(value ="productCache", key ="#id")publicProductgetProductById(Long id){System.out.println("从数据库查询产品:"+ id);return productRepository.findById(id).orElse(null);}@Transactional(readOnly =true)publicList<Product>getAllProducts(){return productRepository.findAll();}}

✅ 结论:使用@CachePut注解是指Spring Boot与数据缓存集成的基本方法之一,作用是实现数据缓存、提高应用程序的性能。

23.5.3 使用@CacheEvict注解

定义:使用@CacheEvict注解是指Spring Boot与数据缓存集成的基本方法之一。
作用

  • 实现数据缓存。
  • 提高应用程序的性能。

示例

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cache.annotation.CacheEvict;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importjava.util.List;@ServicepublicclassProductService{@AutowiredprivateProductRepository productRepository;@Transactional@CachePut(value ="productCache", key ="#product.id")publicProductaddProduct(Product product){return productRepository.save(product);}@Transactional@CachePut(value ="productCache", key ="#product.id")publicProductupdateProduct(Product product){return productRepository.save(product);}@Transactional@CacheEvict(value ="productCache", key ="#id")publicvoiddeleteProduct(Long id){ productRepository.deleteById(id);}@Transactional(readOnly =true)@Cacheable(value ="productCache", key ="#id")publicProductgetProductById(Long id){System.out.println("从数据库查询产品:"+ id);return productRepository.findById(id).orElse(null);}@Transactional(readOnly =true)publicList<Product>getAllProducts(){return productRepository.findAll();}}

✅ 结论:使用@CacheEvict注解是指Spring Boot与数据缓存集成的基本方法之一,作用是实现数据缓存、提高应用程序的性能。

23.6 Spring Boot的实际应用场景

在实际开发中,Spring Boot数据缓存与性能优化的应用场景非常广泛,如:

  • 实现产品信息的缓存。
  • 实现用户信息的缓存。
  • 实现订单信息的缓存。
  • 实现日志信息的缓存。

示例

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCachingpublicclassProductApplication{publicstaticvoidmain(String[] args){SpringApplication.run(ProductApplication.class, args);}@AutowiredprivateProductService productService;publicvoidrun(String... args){// 初始化数据 productService.addProduct(newProduct("P001","手机",1000.0,100)); productService.addProduct(newProduct("P002","电脑",5000.0,50)); productService.addProduct(newProduct("P003","电视",3000.0,80)); productService.addProduct(newProduct("P004","手表",500.0,200)); productService.addProduct(newProduct("P005","耳机",300.0,150));}}@ServiceclassProductService{@AutowiredprivateProductRepository productRepository;@Transactional@CachePut(value ="productCache", key ="#product.id")publicProductaddProduct(Product product){return productRepository.save(product);}@Transactional@CachePut(value ="productCache", key ="#product.id")publicProductupdateProduct(Product product){return productRepository.save(product);}@Transactional@CacheEvict(value ="productCache", key ="#id")publicvoiddeleteProduct(Long id){ productRepository.deleteById(id);}@Transactional(readOnly =true)@Cacheable(value ="productCache", key ="#id")publicProductgetProductById(Long id){System.out.println("从数据库查询产品:"+ id);return productRepository.findById(id).orElse(null);}@Transactional(readOnly =true)publicList<Product>getAllProducts(){return productRepository.findAll();}}@RestController@RequestMapping("/api/products")classProductController{@AutowiredprivateProductService productService;@GetMapping("/")publicList<Product>getAllProducts(){return productService.getAllProducts();}@PostMapping("/")publicProductaddProduct(@RequestBodyProduct product){return productService.addProduct(product);}@PutMapping("/{id}")publicProductupdateProduct(@PathVariableLong id,@RequestBodyProduct product){ product.setId(id);return productService.updateProduct(product);}@DeleteMapping("/{id}")publicvoiddeleteProduct(@PathVariableLong id){ productService.deleteProduct(id);}@GetMapping("/{id}")publicProductgetProductById(@PathVariableLong id){return productService.getProductById(id);}}// 测试类@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)classProductApplicationTests{@LocalServerPortprivateint port;@AutowiredprivateTestRestTemplate restTemplate;@TestvoidcontextLoads(){}@TestvoidtestGetProductById(){Product product1 = restTemplate.getForObject("http://localhost:"+ port +"/api/products/1",Product.class);assertThat(product1).isNotNull();assertThat(product1.getProductId()).isEqualTo("P001");Product product2 = restTemplate.getForObject("http://localhost:"+ port +"/api/products/1",Product.class);assertThat(product2).isNotNull();assertThat(product2.getProductId()).isEqualTo("P001");}@TestvoidtestAddProduct(){Product product =newProduct("P006","平板",2000.0,70);Product savedProduct = restTemplate.postForObject("http://localhost:"+ port +"/api/products/", product,Product.class);assertThat(savedProduct).isNotNull();assertThat(savedProduct.getProductId()).isEqualTo("P006");}@TestvoidtestUpdateProduct(){Product product =newProduct("P001","手机",1500.0,120); restTemplate.put("http://localhost:"+ port +"/api/products/1", product);Product updatedProduct = restTemplate.getForObject("http://localhost:"+ port +"/api/products/1",Product.class);assertThat(updatedProduct).isNotNull();assertThat(updatedProduct.getPrice()).isEqualTo(1500.0);}@TestvoidtestDeleteProduct(){ restTemplate.delete("http://localhost:"+ port +"/api/products/2");Product product = restTemplate.getForObject("http://localhost:"+ port +"/api/products/2",Product.class);assertThat(product).isNull();}}

输出结果

  • 访问http://localhost:8080/api/products/1:第一次访问从数据库查询,第二次访问从缓存查询。

控制台输出:

从数据库查询产品:1 

✅ 结论:在实际开发中,Spring Boot数据缓存与性能优化的应用场景非常广泛,需要根据实际问题选择合适的缓存策略。

总结

本章我们学习了Spring Boot数据缓存与性能优化,包括数据缓存的定义与特点、Spring Boot与数据缓存的集成、Spring Boot与数据缓存的配置、Spring Boot与数据缓存的基本方法、Spring Boot的实际应用场景,学会了在实际开发中处理数据缓存与性能优化问题。其中,数据缓存的定义与特点、Spring Boot与数据缓存的集成、Spring Boot与数据缓存的配置、Spring Boot与数据缓存的基本方法、Spring Boot的实际应用场景是本章的重点内容。从下一章开始,我们将学习Spring Boot的其他组件、微服务等内容。

Read more

OpenClaw技术深度解析:原理、架构与实战应用

2026年初,OpenClaw开源项目十天获13万GitHub星标,成为AI智能体领域现象级产品,重新定义“AI助手”能力边界,开启从“被动对话”到“主动执行”的范式革命。 1. 引言:从“聊天框”到“工具箱” 传统AI助手(如ChatGPT、Claude)只能“动嘴”给出建议,而OpenClaw的核心创新在于赋予AI“动手能力”——它能够直接操作本地应用、读写文件、执行Shell命令、控制浏览器、发送邮件、管理日程等,真正成为一个长期驻留在设备上的“数字员工”。 这款由奥地利工程师Peter Steinberger发布的开源项目,在中文圈有个更接地气的昵称——“龙虾”。这个可爱的名字来源于项目创始人对甲壳类动物的偏爱,而OpenClaw的图标正是一只活灵活现的龙虾。 2. 核心设计理念 2.1 本地优先(Local-First)架构 OpenClaw采用“本地优先”的设计哲学,所有用户数据(

By Ne0inhk
基于 Rust 与 DeepSeek 大模型的智能 API Mock 生成器构建实录:从环境搭建到架构解析

基于 Rust 与 DeepSeek 大模型的智能 API Mock 生成器构建实录:从环境搭建到架构解析

前言 在现代软件工程中,API 接口的开发与前端联调往往存在时间差。为了解耦前后端开发进度,Mock 数据(模拟数据)的生成显得尤为关键。传统的 Mock 数据生成依赖于静态 JSON 文件或简单的规则引擎,难以覆盖复杂的业务逻辑与语义关联。随着大语言模型(LLM)的兴起,利用 AI 根据 Schema 定义动态生成高保真的模拟数据成为可能。本文详细记录了使用 Rust 语言结合 DeepSeek-V3.2 模型构建智能 Mock 生成器的完整技术路径,涵盖操作系统层面的环境准备、Rust 工具链的深度配置、代码层面的异步架构设计以及编译期的版本兼容性处理。 第一部分:Linux 系统底层的构建环境初始化 Rust 语言的编译与链接过程高度依赖于底层的系统工具链。Rust 编译器 rustc 在生成二进制文件时,需要调用链接器(Linker)将编译后的对象文件(Object Files)与系统库(

By Ne0inhk
【MySQL】数据库的 “红绿灯”:非空、主键、外键到底管什么?

【MySQL】数据库的 “红绿灯”:非空、主键、外键到底管什么?

表的约束:表中一定要有各种约束,通过各种约束,保证未来数据库中的数据的准确的;约束的本质是:通过技术手段倒逼程序员,插入正确的数据,进而保证数据库中的数据的正确的; 一、非空约束 两个值:null(默认的)和not null(不为空) 数据库默认字段基本都是字段为空,但是实际开发时,尽可能保证字段不为空,因为数据为空没办法参与运算。 null Vs ''  null : 表示什么都没有; '' :有,但是为空; 二、default 约束 default : 跟 C++ 的缺省值一样; not null  and default: 注意:如果我们的表中没有设置 default 和 not null 约束,他默认 default

By Ne0inhk
二、Kafka核心架构与分布式存储

二、Kafka核心架构与分布式存储

思维导图 一、Kafka定位与核心特性 Kafka不仅是传统的消息队列中间件,更被官方定义为新一代的分布式事件流平台。它在海量流式计算场景中占据绝对核心地位,具备以下底层物理特性: 高吞吐与高并发:摒弃缓慢的随机寻址,深度依赖操作系统的页缓存与磁盘的顺序追加写。单机即可支撑每秒百万级的高并发数据吞吐。 可靠性与持久化存储:流动的数据直接落盘持久化至日志文件。配合多副本冗余机制,确保物理节点宕机时核心业务数据绝对不丢失。 高可扩展性与解耦:支持零停机数据处理。支持在线动态扩容Broker节点,自动实现海量数据流的负载均衡。极大解耦了微服务系统,提升了全链路数据处理效率。 二、分布式存储基石:HDFS架构深度剖析 要理解现代中间件的数据分布逻辑,必须先解剖大数据存储基石HDFS的底层架构。 HDFS采用中心化控制模型,由主管元数据的NameNode与负责物理存储的DataNode构成。一个超大文件会被物理切分为默认128MB的数据块,分散存储在不同DataNode的磁盘上。 为保障极高的容错率,HDFS制定了基于机架感知的副本放置关键原则。 默认的三副

By Ne0inhk