当前位置: 首页 > wzjs >正文

深圳手机网站建设多少钱网站等保建设

深圳手机网站建设多少钱,网站等保建设,学做网站在什么地方学,上海网站建设与设计公司好基于Spring Boot集成Redisson实现布隆过滤器 在高并发和大数据量的场景下,布隆过滤器是一种非常高效的存储结构,可以用于快速判断一个元素是否存在于集合中。本文将介绍如何在Spring Boot中集成Redisson来实现布隆过滤器,并通过一个订单查询…

基于Spring Boot集成Redisson实现布隆过滤器

在高并发和大数据量的场景下,布隆过滤器是一种非常高效的存储结构,可以用于快速判断一个元素是否存在于集合中。本文将介绍如何在Spring Boot中集成Redisson来实现布隆过滤器,并通过一个订单查询的示例来展示其应用。

1. 项目搭建

1.1 创建Spring Boot项目

首先,创建一个Spring Boot项目,并添加以下依赖:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.23.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>
</dependencies>

1.2 配置Redisson

application.yml文件中配置Redisson的连接信息:

spring:application:name: redisson-bloom-filter-demoredisson:config:single-server-config:address: "redis://127.0.0.1:6379"password: "your-redis-password"database: 0connection-pool-config:max-idle: 10min-idle: 1max-active: 100min-evictable-idle-time: 300000time-between-eviction-runs: 30000block-wait-time: 1000idle-instance-soft-abandon: falseidle-instance-close-timeout: 30000bloom:filter:expected-insertions: 1000000  # 预期插入数量,默认值为 1000000false-probability: 0.03       # 误判率,默认值为 3%

2. 布隆过滤器实现

2.1 创建布隆过滤器服务

创建一个BloomFilterService类,用于初始化和操作布隆过滤器:

package com.example.redission.service;import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;@Service
public class BloomFilterService {private static final Logger logger = LoggerFactory.getLogger(BloomFilterService.class);@Value("${bloom.filter.expected-insertions:1000000}")private long expectedInsertions;@Value("${bloom.filter.false-probability:0.03}")private double falseProbability;private static final String BLOOM_FILTER_NAME = "bloomFilter";private final RedissonClient redissonClient;private RBloomFilter<String> bloomFilter;public BloomFilterService(RedissonClient redissonClient) {this.redissonClient = redissonClient;}public synchronized void initBloomFilter() {if (bloomFilter == null) {bloomFilter = redissonClient.getBloomFilter(BLOOM_FILTER_NAME);// 检查布隆过滤器是否已经初始化if (!bloomFilter.isExists()) {bloomFilter.tryInit(expectedInsertions, falseProbability);logger.info("Bloom filter initialized with expected insertions: {} and false probability: {}",expectedInsertions, falseProbability);} else {logger.info("Bloom filter already exists, using existing instance");}}}public void add(String key) {if (key == null) {logger.warn("Attempt to add null key to Bloom filter");return;}bloomFilter.add(key);logger.debug("Added key: {} to Bloom filter", key);}public boolean contains(String key) {if (key == null) {logger.warn("Attempt to check null key in Bloom filter");return false;}boolean result = bloomFilter.contains(key);logger.debug("Checked key: {} in Bloom filter, result: {}", key, result);return result;}/*** 重新初始化布隆过滤器(仅在需要清空时调用)*/public synchronized void reinitialize() {bloomFilter.delete();bloomFilter.tryInit(expectedInsertions, falseProbability);logger.info("Bloom filter reinitialized");}/*** 获取布隆过滤器的错误概率*/public double getFalseProbability() {return falseProbability;}/*** 获取布隆过滤器的预期插入数量*/public long getExpectedInsertions() {return expectedInsertions;}
}

2.2 初始化布隆过滤器

创建一个BloomFilterInitializer类,在应用启动时初始化布隆过滤器:

package com.example.redission.runner;import com.example.redission.dto.OrderDetailDTO;
import com.example.redission.service.BloomFilterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;@Component
public class BloomFilterInitializer implements CommandLineRunner {private static final Logger logger = LoggerFactory.getLogger(BloomFilterInitializer.class);private final BloomFilterService bloomFilterService;@Autowiredpublic BloomFilterInitializer(BloomFilterService bloomFilterService) {this.bloomFilterService = bloomFilterService;}@Overridepublic void run(String... args) throws Exception {// 在应用启动时初始化布隆过滤器bloomFilterService.initBloomFilter();logger.info("Bloom filter initialized during application startup");// 模拟订单数据并写入布隆过滤器List<OrderDetailDTO> orderData = generateSampleOrderData();initializeBloomFilterWithOrderData(orderData);}private void initializeBloomFilterWithOrderData(List<OrderDetailDTO> orderData) {// 将订单数据写入布隆过滤器for (OrderDetailDTO order : orderData) {// 将订单号写入布隆过滤器bloomFilterService.add(order.getOrderNumber());// 将订单中的商品 SKU ID 写入布隆过滤器for (OrderDetailDTO.OrderItemDTO item : order.getItems()) {bloomFilterService.add(item.getSkuId());}}logger.info("Bloom filter initialized with {} orders and their items", orderData.size());}private List<OrderDetailDTO> generateSampleOrderData() {// 使用 Arrays.asList 替代 List.of(JDK 8 兼容)return Arrays.asList(new OrderDetailDTO("1","ORD-001",new BigDecimal("100.00"),"COMPLETED","PAID","2024-01-01 12:00:00",Arrays.asList(new OrderDetailDTO.OrderItemDTO("SKU-001","Product 1","image1.jpg",2,new BigDecimal("50.00"),new BigDecimal("100.00")))),new OrderDetailDTO("2","ORD-002",new BigDecimal("200.00"),"COMPLETED","PAID","2024-01-02 12:00:00",Arrays.asList(new OrderDetailDTO.OrderItemDTO("SKU-002","Product 2","image2.jpg",1,new BigDecimal("200.00"),new BigDecimal("200.00")))));}
}

3. 订单查询功能实现

3.1 创建订单查询服务

创建一个OrderDetailDTO类:

package com.example.redission.dto;import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;import java.math.BigDecimal;
import java.util.List;@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class OrderDetailDTO {private String orderId; // 订单IDprivate String orderNumber; // 订单号private BigDecimal totalAmount; // 总金额private String orderStatus; // 订单状态private String paymentStatus; // 支付状态private String createTime; // 创建时间private List<OrderItemDTO> items; // 订单商品项@Data@AllArgsConstructor@NoArgsConstructor@Builderpublic static class OrderItemDTO {private String skuId; // 商品SKU IDprivate String skuName; // 商品名称private String skuImage; // 商品图片private int quantity; // 商品数量private BigDecimal price; // 商品单价private BigDecimal subtotal; // 商品小计}}

创建一个OrderQueryService类,用于查询订单信息:

package com.example.redission.service;import com.example.redission.dto.OrderDetailDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.math.BigDecimal;
import java.util.Arrays;@Service
public class OrderQueryService {private static final Logger logger = LoggerFactory.getLogger(OrderQueryService.class);private final BloomFilterService bloomFilterService;@Autowiredpublic OrderQueryService(BloomFilterService bloomFilterService) {this.bloomFilterService = bloomFilterService;}/*** 查询订单信息** @param orderNumber 订单号* @return 订单信息,如果不存在则返回 null*/public OrderDetailDTO getOrderDetails(String orderNumber) {// 先通过布隆过滤器判断订单号是否存在if (!bloomFilterService.contains(orderNumber)) {logger.info("Order {} does not exist in Bloom filter", orderNumber);return null;}// 模拟订单数据OrderDetailDTO order = generateSampleOrderData(orderNumber);if (order != null) {logger.info("Order {} found in simulated data", orderNumber);return order;} else {logger.info("Order {} exists in Bloom filter but not found in simulated data", orderNumber);return null;}}/*** 模拟生成订单数据** @param orderNumber 订单号* @return 模拟的订单数据*/private OrderDetailDTO generateSampleOrderData(String orderNumber) {// 模拟的订单数据if ("ORD-001".equals(orderNumber)) {return new OrderDetailDTO("1","ORD-001",new BigDecimal("100.00"),"COMPLETED","PAID","2024-01-01 12:00:00",Arrays.asList(new OrderDetailDTO.OrderItemDTO("SKU-001","Product 1","image1.jpg",2,new BigDecimal("50.00"),new BigDecimal("100.00"))));} else if ("ORD-002".equals(orderNumber)) {return new OrderDetailDTO("2","ORD-002",new BigDecimal("200.00"),"COMPLETED","PAID","2024-01-02 12:00:00",Arrays.asList(new OrderDetailDTO.OrderItemDTO("SKU-002","Product 2","image2.jpg",1,new BigDecimal("200.00"),new BigDecimal("200.00"))));}return null;}
}

3.2 创建订单查询控制器

创建一个OrderQueryController类,用于处理订单查询的HTTP请求:

package com.example.redission.controller;import com.example.redission.dto.OrderDetailDTO;
import com.example.redission.service.OrderQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api/orders")
public class OrderQueryController {private static final Logger logger = LoggerFactory.getLogger(OrderQueryController.class);private final OrderQueryService orderQueryService;@Autowiredpublic OrderQueryController(OrderQueryService orderQueryService) {this.orderQueryService = orderQueryService;}/*** 查询订单信息** @param orderNumber 订单号* @return 订单信息,如果不存在则返回 null*/@GetMapping("/{orderNumber}")public OrderDetailDTO getOrderDetails(@PathVariable String orderNumber) {OrderDetailDTO order = orderQueryService.getOrderDetails(orderNumber);logger.info("Query order {}: result = {}", orderNumber, order);return order;}
}

4. 测试与验证

启动应用后,可以通过以下API接口进行测试:

  • 查询订单信息:GET /api/orders/{orderNumber}

示例请求

查询订单信息
GET /api/orders/ORD-001

示例响应

如果订单存在:

{"orderId": "1","orderNumber": "ORD-001","totalAmount": 100.00,"orderStatus": "COMPLETED","paymentStatus": "PAID","createTime": "2024-01-01 12:00:00","items": [{"skuId": "SKU-001","skuName": "Product 1","skuImage": "image1.jpg","quantity": 2,"price": 50.00,"subtotal": 100.00}]
}

5. 总结

通过本文的示例,我们展示了如何在Spring Boot中集成Redisson来实现布隆过滤器,并通过订单查询功能展示了其应用。布隆过滤器可以有效地减少数据库查询的次数,提高系统的性能和响应速度。在实际应用中,可以根据业务需求调整布隆过滤器的参数,以达到最佳的性能和误判率平衡。


文章转载自:

http://m608jfQv.bqwsz.cn
http://oIqMvyS4.bqwsz.cn
http://eDE12n9L.bqwsz.cn
http://2D9zkq1x.bqwsz.cn
http://rjVkRKkB.bqwsz.cn
http://FrJBxcAx.bqwsz.cn
http://TDuKUs5r.bqwsz.cn
http://vpG2FpeO.bqwsz.cn
http://4icaF98W.bqwsz.cn
http://5ZL8sslo.bqwsz.cn
http://jWL1m3sv.bqwsz.cn
http://KSFicZmL.bqwsz.cn
http://ssQatfdC.bqwsz.cn
http://E7mAyLXK.bqwsz.cn
http://qO5XYB4o.bqwsz.cn
http://R7ebjCDg.bqwsz.cn
http://CvSWCJfC.bqwsz.cn
http://Z6gEo65n.bqwsz.cn
http://DTAWUN6H.bqwsz.cn
http://EjUoJ64G.bqwsz.cn
http://3CsATovL.bqwsz.cn
http://LAOv0a9H.bqwsz.cn
http://ugszkMra.bqwsz.cn
http://4RcH75Bd.bqwsz.cn
http://ZUbAl6T9.bqwsz.cn
http://VBuv02Sy.bqwsz.cn
http://A6UAQi47.bqwsz.cn
http://y4EGWW1h.bqwsz.cn
http://fQqz4vax.bqwsz.cn
http://h8KqNMUo.bqwsz.cn
http://www.dtcms.com/wzjs/605778.html

相关文章:

  • c网站开发视频教程哪个公司的微信商城系统
  • 南昌珠峰网站建设快速搭建网站 数据存储
  • 临沂建设局网站免费咨询医生回答在线妇科
  • 成都网站建设外包海外网站推广优化专员
  • 表白网站源码大全企业营销培训课程
  • 很多国外ip访问网站网站怎么做彩页
  • 三网合一 营销型网站263net企业邮箱
  • 网站浮漂 代码手机编程
  • html 做网站案例简单广州网站建设网页设计
  • 佛山做网站建设公司平台搭建是什么
  • 网站建设服务费属于什么科目微信 微网站
  • 做视频网站多大空间够平台网站建设方案
  • 下载学校网站模板下载欧美seo查询
  • 运城哪家做网站的公司好安全的网站建设服务
  • 网站优化公司 网络服务做网站柳州
  • 天津建站服务营销crm系统网站设计
  • 网站服务器 数据库服务器网页版html编辑器
  • 怎么做同城网站上海高端网站定
  • 网站建设全python 网站开发 实例
  • wordpress模板适合做什么站怎么在手机上建网站
  • 廊坊做网站的哪最多上传网站的软件
  • 如何在linux服务器上架设网站律师建网站
  • 如何在百度云上建设网站企业网上购物系统
  • 广东建设行业招聘 什么网站西安企业网站制作
  • 电脑怎么打不开建设银行网站huntt wordpress主题
  • 网站提速网站网络营销推广制作
  • vps搭建网站教程建网站程序下载
  • asp网站制作工具宜昌网站建设选择宜昌慧享互动
  • 淘宝运营是做什么的工作网站ul优化
  • 千博网站后台闵行网站建设哪家好