视频69(敏感词校验)

master
Administrator 3 years ago
parent 49a871c972
commit 5f5dfcd7c4

@ -38,6 +38,13 @@ public class CacheController {
redisClient.sAdd(key,value);
}
@PostMapping(value = "/cache/saddstr/{key}")
public void saddStr(@PathVariable(value = "key")String key, @RequestBody String... value){
log.info("【缓存模块】 saddStr方法存储key = {}存储value = {}", key, value);
redisClient.sAdd(key,value);
}
@GetMapping("/cache/hgetall/{key}")
public Map hGetAll(@PathVariable(value = "key")String key){
log.info("【缓存模块】 hGetAll方法获取key ={} 的数据", key);
@ -62,6 +69,23 @@ public class CacheController {
return values;
}
@PostMapping("/cache/pipeline/string")
public void pipelineString(@RequestBody Map<String,String> map){
log.info("【缓存模块】 pipelineString获取到存储的数据map的长度 ={}的数据", map.size());
redisClient.pipelined(operations -> {
for (Map.Entry<String, String> entry : map.entrySet()) {
operations.opsForValue().set(entry.getKey(),entry.getValue());
}
});
}
@GetMapping(value = "/cache/get/{key}")
public Object get(@PathVariable(value = "key") String key) {
log.info("【缓存模块】 get方法查询key = {}", key);
Object value = redisClient.get(key);
log.info("【缓存模块】 get方法查询key = {}对应的value = {}", key,value);
return value;
}

@ -31,17 +31,19 @@ public class TestController {
Map<String, Object> result = redisClient.getMap(key);
return result;
}
@GetMapping("/test/xxx")
public Map xxx(){
// 管道测试
@PostMapping("/test/pipeline")
public String pipeline(){
Map<String,Object> maps = new HashMap<>();
maps.put("1801003","北京 北京,电信");
maps.put("1734310","北京 北京,电信");
maps.put("1888888","北京 北京,移动");
maps.put("1888889","北京 北京,电信");
redisClient.pipelined(operations -> {
for (Map.Entry<String, Object> entry : maps.entrySet()) {
operations.opsForValue().set(entry.getKey(),entry.getValue());
}
});
return null;
return "ok";
}
}

@ -23,6 +23,15 @@ public interface CacheConstant {
*
*/
String CLIENT_BALANCE = "client_balance:";
/**
*
*/
String PHASE = "phase:";
/**
*
*/
String DIRTY_WORD = "dirty_word";
}

@ -11,4 +11,10 @@ public interface RabbitMQConstants {
*
*/
String SMS_PRE_SEND = "sms_pre_send_topic";
/**
* &
*/
String MOBILE_AREA_OPERATOR = "mobile_area_operator_topic";
}

@ -0,0 +1,27 @@
package com.mashibing.common.enums;
import lombok.Getter;
/**
* @author zjw
* @description
*/
@Getter
public enum MobileOperatorEnum {
CHINA_MOBILE(1,"移动"),
CHINA_UNICOM(2,"联通"),
CHINA_TELECOM(3,"电信"),
UNKNOWN(0,"未知");
private Integer operatorId;
private String operatorName;
MobileOperatorEnum(Integer operatorId, String operatorName) {
this.operatorId = operatorId;
this.operatorName = operatorName;
}
}

@ -0,0 +1,33 @@
package com.mashibing.common.util;
import com.mashibing.common.enums.MobileOperatorEnum;
import java.util.HashMap;
import java.util.Map;
/**
* @author zjw
* @description
*/
public class OperatorUtil {
private static Map<String,Integer> operators = new HashMap<>();
static{
MobileOperatorEnum[] operatorEnums = MobileOperatorEnum.values();
for (MobileOperatorEnum operatorEnum : operatorEnums) {
operators.put(operatorEnum.getOperatorName(),operatorEnum.getOperatorId());
}
}
/**
* Id
* @param operatorName
* @return
*/
public static Integer getOperatorIdByOperatorName(String operatorName){
return operators.get(operatorName);
}
}

@ -4,6 +4,8 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author zjw
@ -18,4 +20,9 @@ public class StrategyStarterApp {
SpringApplication.run(StrategyStarterApp.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

@ -3,6 +3,7 @@ package com.mashibing.strategy.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
/**
* @author zjw
@ -13,4 +14,7 @@ public interface BeaconCacheClient {
@GetMapping("/cache/hget/{key}/{field}")
String hget(@PathVariable(value = "key")String key, @PathVariable(value = "field")String field);
@GetMapping(value = "/cache/get/{key}")
String getString(@PathVariable(value = "key") String key);
}

@ -0,0 +1,26 @@
package com.mashibing.strategy.config;
import com.mashibing.common.constant.RabbitMQConstants;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* &
* @author zjw
* @description
*/
@Configuration
public class RabbitMQConfig {
/**
*
* @return
*/
@Bean
public Queue preSendQueue(){
return QueueBuilder.durable(RabbitMQConstants.MOBILE_AREA_OPERATOR).build();
}
}

@ -0,0 +1,55 @@
package com.mashibing.strategy.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* RabbitTemplateconfirm&return
* @author zjw
* @description
*/
@Configuration
@Slf4j
public class RabbitTemplateConfig {
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
//1、构建RabbitTemplate对象
RabbitTemplate rabbitTemplate = new RabbitTemplate();
//2、设置connectionFactory
rabbitTemplate.setConnectionFactory(connectionFactory);
//3、配置confirm机制
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback(){
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
// ack为false代表消息没有发送到exchange。
if(!ack){
log.error("【接口模块-发送消息】 消息没有发送到交换机correlationData = {}cause = {}",correlationData,cause);
}
}
});
//4、配置return机制
rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback(){
// 触发这个回调,说明交换机没有把消息路由到指定的队列中
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
log.error("【接口模块-发送消息】 消息没有路由到指定的Queue。 message = {},exchange = {},routingKey = {}",
new String(message.getBody()),exchange,routingKey);
}
});
//5、返回
return rabbitTemplate;
}
}

@ -1,12 +1,21 @@
package com.mashibing.strategy.filter.impl;
import com.mashibing.common.constant.CacheConstant;
import com.mashibing.common.constant.RabbitMQConstants;
import com.mashibing.common.model.StandardSubmit;
import com.mashibing.common.util.OperatorUtil;
import com.mashibing.strategy.client.BeaconCacheClient;
import com.mashibing.strategy.filter.StrategyFilter;
import com.mashibing.strategy.util.MobileOperatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
*
* @author zjw
* @description
*/
@ -14,8 +23,57 @@ import org.springframework.stereotype.Service;
@Slf4j
public class PhaseStrategyFilter implements StrategyFilter {
/**
* 7
*/
private final int MOBILE_START = 0;
private final int MOBILE_END = 7;
/**
*
*/
private final int LENGTH = 2;
/**
*
*/
private final String SEPARATE = ",";
/**
*
*/
private final String UNKNOWN = "未知 未知,未知";
@Autowired
private BeaconCacheClient cacheClient;
@Autowired
private MobileOperatorUtil mobileOperatorUtil;
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public void strategy(StandardSubmit submit) {
log.info("【策略模块-号段补齐】 校验ing…………");
log.info("【策略模块-号段补齐】 补全ing…………");
//1、根据手机号前7位查询手机号信息
String mobile = submit.getMobile().substring(MOBILE_START, MOBILE_END);
String mobileInfo = cacheClient.getString(CacheConstant.PHASE + mobile);
getMobileInfo: if (StringUtils.isEmpty(mobileInfo)) {
//2、查询不到需要调用三方接口查询手机号对应信息
mobileInfo = mobileOperatorUtil.getMobileInfoBy360(mobile);
if(!StringUtils.isEmpty(mobileInfo)){
//3、调用三方查到信息后发送消息到MQ并且同步到MySQL和Redis
rabbitTemplate.convertAndSend(RabbitMQConstants.MOBILE_AREA_OPERATOR,submit.getMobile());
break getMobileInfo;
}
mobileInfo = UNKNOWN;
}
//4、无论是Redis还是三方接口查询到之后封装到StandardSubmit对象中
String[] areaAndOperator = mobileInfo.split(SEPARATE);
if (areaAndOperator.length == LENGTH) {
submit.setArea(areaAndOperator[0]);
submit.setOperatorId(OperatorUtil.getOperatorIdByOperatorName(areaAndOperator[1]));
}
}
}

@ -0,0 +1,66 @@
package com.mashibing.strategy.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.aspectj.apache.bcel.generic.ObjectType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
/**
*
* @author zjw
* @description
*/
@Component
public class MobileOperatorUtil {
@Autowired
private RestTemplate restTemplate;
@Autowired
private ObjectMapper objectMapper;
private final String url1 = "https://cx.shouji.360.cn/phonearea.php?number=";
private final String CODE = "code";
private final String DATA = "data";
private final String PROVINCE = "province";
private final String CITY = "city";
private final String SP = "sp";
private final String SPACE = " ";
private final String SEPARATE = ",";
/**
*
* @param mobile 7
* @return
*/
public String getMobileInfoBy360(String mobile){
String url = url1;
//1、发送请求获取信息
String mobileInfoJSON = restTemplate.getForObject(url + mobile, String.class);
// {"code":0,"data":{"province":"\u4e91\u5357","city":"\u6606\u660e","sp":"\u79fb\u52a8"}}
//2、解析JSON
Map map = null;
try {
map = objectMapper.readValue(mobileInfoJSON, Map.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
Integer code = (Integer) map.get(CODE);
if(code != 0){
return null;
}
Map<String, String> areaAndOperator = (Map<String, String>) map.get(DATA);
String province = areaAndOperator.get(PROVINCE);
String city = areaAndOperator.get(CITY);
String sp = areaAndOperator.get(SP);
//3、封装为 省 市,运营商 的格式返回
return province + SPACE +city + SEPARATE + sp;
}
}

@ -25,4 +25,9 @@ public interface CacheClient {
@PostMapping(value = "/cache/sadd/{key}")
void sadd(@PathVariable(value = "key")String key, @RequestBody Map<String,Object>... maps);
@PostMapping("/cache/pipeline/string")
void pipelineString(@RequestBody Map<String,String> map);
@PostMapping(value = "/cache/saddstr/{key}")
void saddStr(@PathVariable(value = "key")String key, @RequestBody String... value);
}

@ -0,0 +1,38 @@
package com.mashibing.test.entity;
/**
* @author zjw
* @description
*/
public class MobileArea {
private String mobileNumber;
private String mobileArea;
private String mobileType;
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getMobileArea() {
return mobileArea;
}
public void setMobileArea(String mobileArea) {
this.mobileArea = mobileArea;
}
public String getMobileType() {
return mobileType;
}
public void setMobileType(String mobileType) {
this.mobileType = mobileType;
}
}

@ -0,0 +1,19 @@
package com.mashibing.test.mapper;
import com.mashibing.test.entity.ClientBalance;
import com.mashibing.test.entity.MobileArea;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @author zjw
* @description
*/
public interface MobileAreaMapper {
@Select("select mobile_number,mobile_area,mobile_type from mobile_area")
List<MobileArea> findAll();
}

@ -0,0 +1,17 @@
package com.mashibing.test.mapper;
import com.mashibing.test.entity.MobileArea;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @author zjw
* @description
*/
public interface MobileDirtyWordMapper {
@Select("select dirtyword from mobile_dirtyword")
List<String> findDirtyWord();
}

@ -0,0 +1,37 @@
package com.mashibing.test.mapper;
import com.mashibing.test.client.CacheClient;
import com.mashibing.test.entity.MobileArea;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MobileAreaMapperTest {
@Autowired
private MobileAreaMapper mapper;
@Autowired
private CacheClient cacheClient;
@Test
public void findAll() {
List<MobileArea> list = mapper.findAll();
Map map = new HashMap(list.size());
for (MobileArea mobileArea : list) {
map.put("phase:" + mobileArea.getMobileNumber(),mobileArea.getMobileArea() + "," + mobileArea.getMobileType());
}
cacheClient.pipelineString(map);
}
}

@ -0,0 +1,32 @@
package com.mashibing.test.mapper;
import com.mashibing.test.client.CacheClient;
import com.mashibing.test.entity.MobileArea;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MobileDirtyWordMapperTest {
@Autowired
private MobileDirtyWordMapper mapper;
@Autowired
private CacheClient cacheClient;
@Test
public void findAll() {
List<String> dirtyWords = mapper.findDirtyWord();
cacheClient.saddStr("dirty_word",dirtyWords.toArray(new String[]{}));
}
}
Loading…
Cancel
Save