19 changed files with 435 additions and 180 deletions
-
10pom.xml
-
41src/main/java/com/example/demo/controller/AdminController.java
-
14src/main/java/com/example/demo/controller/ConsumeController.java
-
18src/main/java/com/example/demo/controller/KafkaConfig.java
-
152src/main/java/com/example/demo/controller/KafkaConsumer.java
-
32src/main/java/com/example/demo/controller/KafkaProducer.java
-
24src/main/java/com/example/demo/controller/PermissionController.java
-
25src/main/java/com/example/demo/controller/RechargeController.java
-
113src/main/java/com/example/demo/domain/entity/Admin.java
-
11src/main/java/com/example/demo/domain/entity/Permission.java
-
9src/main/java/com/example/demo/domain/entity/Recharge.java
-
41src/main/java/com/example/demo/mapper/AdminMapper.java
-
15src/main/java/com/example/demo/mapper/PermissionMapper.java
-
25src/main/java/com/example/demo/serviceImpl/AdminServiceImpl.java
-
24src/main/java/com/example/demo/serviceImpl/PermissionServiceImpl.java
-
2src/main/java/com/example/demo/serviceImpl/RechargeServiceImpl.java
-
3src/main/java/com/example/demo/sevice/AdminService.java
-
9src/main/java/com/example/demo/sevice/PermissionService.java
-
45src/main/resources/application.yml
@ -1,9 +1,9 @@ |
|||||
package com.example.demo.controller; |
|
||||
|
|
||||
import org.springframework.context.annotation.Configuration; |
|
||||
import org.springframework.kafka.annotation.EnableKafka; |
|
||||
|
|
||||
@Configuration |
|
||||
@EnableKafka |
|
||||
public class KafkaConfig { |
|
||||
} |
|
||||
|
//package com.example.demo.controller; |
||||
|
// |
||||
|
//import org.springframework.context.annotation.Configuration; |
||||
|
//import org.springframework.kafka.annotation.EnableKafka; |
||||
|
// |
||||
|
//@Configuration |
||||
|
//@EnableKafka |
||||
|
//public class KafkaConfig { |
||||
|
//} |
@ -1,76 +1,76 @@ |
|||||
package com.example.demo.controller; |
|
||||
|
|
||||
import com.example.demo.domain.entity.Detail; |
|
||||
import com.example.demo.domain.entity.DetailY; |
|
||||
import com.example.demo.domain.entity.Recharge; |
|
||||
import com.example.demo.serviceImpl.ConsumeServiceImpl; |
|
||||
import com.example.demo.sevice.RechargeService; |
|
||||
import com.fasterxml.jackson.core.type.TypeReference; |
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.kafka.annotation.KafkaListener; |
|
||||
import org.springframework.stereotype.Service; |
|
||||
|
|
||||
import java.io.IOException; |
|
||||
import java.util.List; |
|
||||
|
|
||||
@Service |
|
||||
@Slf4j |
|
||||
public class KafkaConsumer { |
|
||||
|
|
||||
@Autowired |
|
||||
private ConsumeServiceImpl consumeService; |
|
||||
|
|
||||
@Autowired |
|
||||
private RechargeService rechargeService; |
|
||||
|
|
||||
private final ObjectMapper objectMapper = new ObjectMapper(); |
|
||||
|
|
||||
@KafkaListener(topics = "rechargeadd_topic") |
|
||||
public void listenRechargeadd(String message) { |
|
||||
try { |
|
||||
// 反序列化为List<Recharge> |
|
||||
List<Recharge> rechargeList = objectMapper.readValue(message, new TypeReference<List<Recharge>>() {}); |
|
||||
|
|
||||
// 遍历并处理每个充值记录 |
|
||||
for (Recharge recharge : rechargeList) { |
|
||||
processRecharge(recharge); |
|
||||
} |
|
||||
} catch (IOException e) { |
|
||||
// 处理反序列化异常 |
|
||||
e.printStackTrace(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private void processRecharge(Recharge recharge) { |
|
||||
// 具体的业务处理逻辑 |
|
||||
System.out.println("Processing recharge: " + recharge); |
|
||||
} |
|
||||
|
|
||||
@KafkaListener(topics = "consume-topic", groupId = "my-group") |
|
||||
public void listenConsume(String message) { |
|
||||
try { |
|
||||
DetailY detailY = objectMapper.readValue(message, DetailY.class); |
|
||||
|
|
||||
// 处理消费请求 |
|
||||
Integer result = consumeService.insert(detailY); |
|
||||
log.info("Processed consume request with result: {}", result); |
|
||||
} catch (Exception e) { |
|
||||
log.error("Error processing consume message: {}", message, e); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@KafkaListener(topics = "recharge-topic", groupId = "my-group") |
|
||||
public void listenRecharge(String message) { |
|
||||
try { |
|
||||
Recharge recharge = objectMapper.readValue(message, Recharge.class); |
|
||||
|
|
||||
// 处理充值请求 |
|
||||
rechargeService.add(recharge); |
|
||||
log.info("Processed recharge request with id: {}", recharge.getRechargeId()); |
|
||||
} catch (Exception e) { |
|
||||
log.error("Error processing recharge message: {}", message, e); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
//package com.example.demo.controller; |
||||
|
// |
||||
|
//import com.example.demo.domain.entity.Detail; |
||||
|
//import com.example.demo.domain.entity.DetailY; |
||||
|
//import com.example.demo.domain.entity.Recharge; |
||||
|
//import com.example.demo.serviceImpl.ConsumeServiceImpl; |
||||
|
//import com.example.demo.sevice.RechargeService; |
||||
|
//import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
//import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
//import lombok.extern.slf4j.Slf4j; |
||||
|
//import org.springframework.beans.factory.annotation.Autowired; |
||||
|
//import org.springframework.kafka.annotation.KafkaListener; |
||||
|
//import org.springframework.stereotype.Service; |
||||
|
// |
||||
|
//import java.io.IOException; |
||||
|
//import java.util.List; |
||||
|
// |
||||
|
//@Service |
||||
|
//@Slf4j |
||||
|
//public class KafkaConsumer { |
||||
|
// |
||||
|
// @Autowired |
||||
|
// private ConsumeServiceImpl consumeService; |
||||
|
// |
||||
|
// @Autowired |
||||
|
// private RechargeService rechargeService; |
||||
|
// |
||||
|
// private final ObjectMapper objectMapper = new ObjectMapper(); |
||||
|
// |
||||
|
// @KafkaListener(topics = "rechargeadd_topic") |
||||
|
// public void listenRechargeadd(String message) { |
||||
|
// try { |
||||
|
// // 反序列化为List<Recharge> |
||||
|
// List<Recharge> rechargeList = objectMapper.readValue(message, new TypeReference<List<Recharge>>() {}); |
||||
|
// |
||||
|
// // 遍历并处理每个充值记录 |
||||
|
// for (Recharge recharge : rechargeList) { |
||||
|
// processRecharge(recharge); |
||||
|
// } |
||||
|
// } catch (IOException e) { |
||||
|
// // 处理反序列化异常 |
||||
|
// e.printStackTrace(); |
||||
|
// } |
||||
|
// } |
||||
|
// |
||||
|
// private void processRecharge(Recharge recharge) { |
||||
|
// // 具体的业务处理逻辑 |
||||
|
// System.out.println("Processing recharge: " + recharge); |
||||
|
// } |
||||
|
// |
||||
|
// @KafkaListener(topics = "consume-topic", groupId = "my-group") |
||||
|
// public void listenConsume(String message) { |
||||
|
// try { |
||||
|
// DetailY detailY = objectMapper.readValue(message, DetailY.class); |
||||
|
// |
||||
|
// // 处理消费请求 |
||||
|
// Integer result = consumeService.insert(detailY); |
||||
|
// log.info("Processed consume request with result: {}", result); |
||||
|
// } catch (Exception e) { |
||||
|
// log.error("Error processing consume message: {}", message, e); |
||||
|
// } |
||||
|
// } |
||||
|
// |
||||
|
// @KafkaListener(topics = "recharge-topic", groupId = "my-group") |
||||
|
// public void listenRecharge(String message) { |
||||
|
// try { |
||||
|
// Recharge recharge = objectMapper.readValue(message, Recharge.class); |
||||
|
// |
||||
|
// // 处理充值请求 |
||||
|
// rechargeService.add(recharge); |
||||
|
// log.info("Processed recharge request with id: {}", recharge.getRechargeId()); |
||||
|
// } catch (Exception e) { |
||||
|
// log.error("Error processing recharge message: {}", message, e); |
||||
|
// } |
||||
|
// } |
||||
|
//} |
@ -1,16 +1,16 @@ |
|||||
package com.example.demo.controller; |
|
||||
|
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.kafka.core.KafkaTemplate; |
|
||||
import org.springframework.stereotype.Service; |
|
||||
|
|
||||
@Service |
|
||||
public class KafkaProducer { |
|
||||
|
|
||||
@Autowired |
|
||||
private KafkaTemplate<String, String> kafkaTemplate; |
|
||||
|
|
||||
public void sendMessage(String topic, String message) { |
|
||||
kafkaTemplate.send(topic, message); |
|
||||
} |
|
||||
} |
|
||||
|
//package com.example.demo.controller; |
||||
|
// |
||||
|
//import org.springframework.beans.factory.annotation.Autowired; |
||||
|
//import org.springframework.kafka.core.KafkaTemplate; |
||||
|
//import org.springframework.stereotype.Service; |
||||
|
// |
||||
|
//@Service |
||||
|
//public class KafkaProducer { |
||||
|
// |
||||
|
// @Autowired |
||||
|
// private KafkaTemplate<String, String> kafkaTemplate; |
||||
|
// |
||||
|
// public void sendMessage(String topic, String message) { |
||||
|
// kafkaTemplate.send(topic, message); |
||||
|
// } |
||||
|
//} |
@ -0,0 +1,24 @@ |
|||||
|
package com.example.demo.controller; |
||||
|
|
||||
|
|
||||
|
import com.example.demo.domain.vo.Result; |
||||
|
import com.example.demo.sevice.PermissionService; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.web.bind.annotation.CrossOrigin; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
@RestController |
||||
|
@RequestMapping("/permission") |
||||
|
@RequiredArgsConstructor |
||||
|
@Slf4j |
||||
|
@CrossOrigin |
||||
|
public class PermissionController { |
||||
|
private final PermissionService permissionService; |
||||
|
|
||||
|
@RequestMapping("/getPermission") |
||||
|
public Result getPermission() { |
||||
|
return Result.success(permissionService.getPermission()); |
||||
|
} |
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
package com.example.demo.domain.entity; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
@Data |
||||
|
@NoArgsConstructor |
||||
|
public class Permission { |
||||
|
private Integer permissionId; |
||||
|
private String permissionName; |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
package com.example.demo.mapper; |
||||
|
|
||||
|
import com.example.demo.domain.entity.Permission; |
||||
|
import org.apache.ibatis.annotations.Mapper; |
||||
|
import org.apache.ibatis.annotations.Select; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Mapper |
||||
|
public interface PermissionMapper { |
||||
|
@Select({ |
||||
|
"select * from permission" |
||||
|
}) |
||||
|
List<Permission> getPermission(); |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
package com.example.demo.serviceImpl; |
||||
|
|
||||
|
import com.example.demo.domain.entity.Permission; |
||||
|
import com.example.demo.mapper.PermissionMapper; |
||||
|
import com.example.demo.sevice.PermissionService; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.cache.annotation.CacheConfig; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Transactional |
||||
|
@Service |
||||
|
@RequiredArgsConstructor |
||||
|
@CacheConfig(cacheNames = "permission") |
||||
|
public class PermissionServiceImpl implements PermissionService { |
||||
|
|
||||
|
private final PermissionMapper permissionMapper; |
||||
|
@Override |
||||
|
public List<Permission> getPermission() { |
||||
|
return permissionMapper.getPermission(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
package com.example.demo.sevice; |
||||
|
|
||||
|
import com.example.demo.domain.entity.Permission; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface PermissionService { |
||||
|
List<Permission> getPermission(); |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue