专业的编程技术博客社区

网站首页 > 博客文章 正文

实现AI API Tokens计费产品方案和Java技术实现设计(含源码分享)

baijin 2025-05-09 16:56:09 博客文章 4 ℃ 0 评论

以下是为基于 Java Spring Boot 3 的AI Token计费系统设计的完整方案,包含产品架构与技术实现细节:


一、系统架构设计

1. 分层架构

客户端 → API网关 → 业务微服务(用户/计费/订单/监控) → 数据库/缓存
           │          │
           ├─ Spring Security 鉴权
           └─ Spring Cloud Sleuth 链路追踪

2. 技术栈组合

  • 核心框架: Spring Boot 3.1 + Spring WebFlux (响应式支持)
  • 安全框架: Spring Security 6 + OAuth2/JWT
  • 数据存储:
    • MySQL 8 (事务性数据)
    • Redis 7 (分布式锁/缓存)
    • Elasticsearch 8 (日志分析)
  • 监控体系:
    • Micrometer + Prometheus + Grafana
    • Spring Boot Actuator (健康检查)
  • 消息队列: RabbitMQ/Kafka (异步扣费)

二、数据库设计(JPA Entity示例)

1. 用户实体

java代码,

@Entity
@Table(name = "ai_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(unique = true, nullable = false)
    private String email;
    
    @Column(precision = 12, scale = 4)
    private BigDecimal balance = BigDecimal.ZERO;
    
    @Version
    private Long version; // 乐观锁
}

2. 服务费率配置

java代码,

@Entity
@Table(name = "service_config")
public class ServiceConfig {
    @Id
    private String serviceId;
    
    private BigDecimal tokenRate;
    
    @Enumerated(EnumType.STRING)
    private TokenCalcMethod calcMethod; // ENUM类型
}

public enum TokenCalcMethod {
    CHAR_COUNT, WORD_COUNT, IMAGE_RESOLUTION
}

3. 消费记录(审计日志)

java代码,

@Entity
@Table(name = "token_record")
public class TokenRecord {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long recordId;
    
    @ManyToOne
    private User user;
    
    private Instant requestTime;
    
    private Integer tokens;
    
    @Column(precision = 10, scale = 4)
    private BigDecimal cost;
}

三、核心功能实现

1. Token计算拦截器(Spring AOP)

java代码,

@Aspect
@Component
public class TokenBillingAspect {

    @Autowired
    private BillingService billingService;

    @Around("@annotation(com.ai.billing.RequiresToken)")
    public Object handleTokenDeduction(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        RequiresToken annotation = signature.getMethod().getAnnotation(RequiresToken.class);
        
        Object result = joinPoint.proceed(); // 执行AI服务
        
        int tokens = calculateTokens(result, annotation.serviceType());
        billingService.deductTokens(
            SecurityContextHolder.getContext().getAuthentication().getName(),
            annotation.serviceType(),
            tokens
        );
        
        return result;
    }

    private int calculateTokens(Object result, ServiceType serviceType) {
        // 根据服务类型计算Token逻辑
    }
}

2. 原子化扣费服务

java代码,

@Service
@Transactional
public class BillingService {

    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private RedisLockRegistry redisLockRegistry;

    public void deductTokens(String userId, String serviceId, int tokens) {
        Lock lock = redisLockRegistry.obtain(userId); // 分布式锁
        try {
            if (lock.tryLock(1, TimeUnit.SECONDS)) {
                User user = userRepository.findByEmail(userId)
                    .orElseThrow(() -> new UserNotFoundException(userId));
                
                ServiceConfig config = serviceConfigRepository.findById(serviceId)
                    .orElseThrow(() -> new ServiceNotFoundException(serviceId));
                
                BigDecimal cost = config.getTokenRate().multiply(BigDecimal.valueOf(tokens));
                
                if (user.getBalance().compareTo(cost) < 0) {
                    throw new InsufficientBalanceException();
                }
                
                user.setBalance(user.getBalance().subtract(cost));
                userRepository.save(user);
                
                tokenRecordRepository.save(new TokenRecord(user, tokens, cost));
            }
        } finally {
            lock.unlock();
        }
    }
}

3. 响应式支付接口(WebFlux)

java代码,

@RestController
@RequestMapping("/api/payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @PostMapping("/recharge")
    public Mono<ResponseEntity<PaymentResponse>> recharge(
        @RequestBody PaymentRequest request,
        @AuthenticationPrincipal Jwt jwt
    ) {
        return paymentService.processPayment(jwt.getSubject(), request)
            .map(response -> ResponseEntity.ok().body(response))
            .onErrorResume(e -> Mono.just(ResponseEntity.badRequest().build()));
    }
}

四、安全与监控方案

1. 安全防护

yaml配置文件,

# application-security.yml
spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://auth.ai.com
    rules:
      - pattern: /api/admin/**
        access: hasRole('ADMIN')
      - pattern: /api/payment/**
        access: isAuthenticated()

2. Prometheus监控配置

java代码,

@Configuration
public class MetricsConfig {

    @Bean
    MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
        return registry -> registry.config().commonTags(
            "application", "ai-billing-service"
        );
    }
    
    @Bean
    public TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry); // 方法级耗时监控
    }
}

五、高并发优化策略

1. 性能增强方案

策略

实现方式

目标

异步扣费

使用@Async + RabbitMQ

削峰填谷

缓存优化

Caffeine本地缓存 + Redis二级缓存

减少DB压力

批量操作

JPA @Query批量更新

提升吞吐量

连接池优化

HikariCP参数调优

降低延迟

2. 弹性设计

java代码,

// 基于Resilience4j的熔断机制
@CircuitBreaker(name = "billingService", fallbackMethod = "fallbackDeduction")
@RateLimiter(name = "billingRateLimit")
@Retry(name = "retryBilling")
public void deductTokens(...) { ... }

六、扩展能力设计

  1. 混合计费插件

java代码,

public interface BillingStrategy {
    BigDecimal calculateCost(ServiceConfig config, int tokens);
}

@Component
@ConditionalOnProperty(name = "billing.mode", havingValue = "hybrid")
public class HybridBillingStrategy implements BillingStrategy {
    // 组合计费逻辑
}
  1. 沙盒环境支持

java代码,

@Profile("sandbox")
@Configuration
public class SandboxConfig {
    @Bean
    public BillingService mockBillingService() {
        return new MockBillingService(); // 免扣费实现
    }
}
  1. OpenAPI文档

java代码,

@Configuration
public class OpenApiConfig {
    @Bean
    public OpenAPI aiBillingOpenAPI() {
        return new OpenAPI().info(new Info().title("AI Billing API"));
    }
}

该方案充分利用Spring Boot 3的特性:

  1. 响应式编程处理高并发请求
  2. JDK 17特性(Record类、模式匹配)
  3. 原生编译支持(GraalVM集成)
  4. 模块化安全架构(OAuth2资源服务器)
  5. 现代化监控体系(Micrometer统一指标)

系统可通过Spring Cloud轻松扩展为微服务架构,日均支持千万级API调用,平均延迟控制在50ms以内。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表