From 373d6a95333b246fe94d73d9b7fb9706d55da3a3 Mon Sep 17 00:00:00 2001 From: kezhen0805 Date: Sun, 25 Dec 2022 23:53:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E7=9C=9F=E5=AE=9EIP=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/encodings.xml | 2 + beacon-cache/pom.xml | 48 +++++++++++++ .../com/mashibing/cache/CacheStarterApp.java | 18 +++++ .../mashibing/cache/config/RedisConfig.java | 68 +++++++++++++++++++ .../cache/contorller/TestController.java | 37 ++++++++++ beacon-cache/src/main/resources/bootstrap.yml | 13 ++++ beacon-common/pom.xml | 27 ++++++++ .../common/model/StandardSubmit.java | 56 +++++++++++++++ beancon-api/pom.xml | 6 ++ .../api/controller/SmsController.java | 48 ++++++++++++- pom.xml | 13 +++- 11 files changed, 331 insertions(+), 5 deletions(-) create mode 100644 beacon-cache/pom.xml create mode 100755 beacon-cache/src/main/java/com/mashibing/cache/CacheStarterApp.java create mode 100644 beacon-cache/src/main/java/com/mashibing/cache/config/RedisConfig.java create mode 100644 beacon-cache/src/main/java/com/mashibing/cache/contorller/TestController.java create mode 100644 beacon-cache/src/main/resources/bootstrap.yml create mode 100644 beacon-common/pom.xml create mode 100644 beacon-common/src/main/java/com/mashibing/common/model/StandardSubmit.java diff --git a/.idea/encodings.xml b/.idea/encodings.xml index b066421..29f4707 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -1,6 +1,8 @@ + + diff --git a/beacon-cache/pom.xml b/beacon-cache/pom.xml new file mode 100644 index 0000000..4d560c7 --- /dev/null +++ b/beacon-cache/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + com.mashibing + beacon-cloud + 1.0-SNAPSHOT + + + beacon-cache + + + 8 + 8 + UTF-8 + + + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + + + + + com.msb.cloud + horse-framework-starter-redis + 1.0.0 + + + + \ No newline at end of file diff --git a/beacon-cache/src/main/java/com/mashibing/cache/CacheStarterApp.java b/beacon-cache/src/main/java/com/mashibing/cache/CacheStarterApp.java new file mode 100755 index 0000000..ff0ba31 --- /dev/null +++ b/beacon-cache/src/main/java/com/mashibing/cache/CacheStarterApp.java @@ -0,0 +1,18 @@ +package com.mashibing.cache; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * @author kezhen + * @date 2022/12/25 + * @description + */ +@SpringBootApplication +@EnableDiscoveryClient +public class CacheStarterApp { + public static void main(String[] args) { + SpringApplication.run(CacheStarterApp.class, args); + } +} diff --git a/beacon-cache/src/main/java/com/mashibing/cache/config/RedisConfig.java b/beacon-cache/src/main/java/com/mashibing/cache/config/RedisConfig.java new file mode 100644 index 0000000..8a432af --- /dev/null +++ b/beacon-cache/src/main/java/com/mashibing/cache/config/RedisConfig.java @@ -0,0 +1,68 @@ +package com.mashibing.cache.config; + +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializer; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * @author kezhen + * @date 2022/12/25 + * @description 设置redisTemplate的序列化方式 + */ +@Configuration +public class RedisConfig { +// @Bean +// public RedisTemplate redisTemplate(RedisConnectionFactory factory,RedisSerializer redisSerializer){ +// RedisTemplate redisTemplate = new RedisTemplate<>(); +// redisTemplate.setConnectionFactory(factory); +// redisTemplate.setKeySerializer(RedisSerializer.string()); +// redisTemplate.setHashKeySerializer(RedisSerializer.string()); +// +// redisTemplate.setValueSerializer(redisSerializer); +// redisTemplate.setHashValueSerializer(redisSerializer); +// +// redisTemplate.afterPropertiesSet(); +// return redisTemplate; +// } +// +// @Bean +// public RedisSerializer redisSerializer(){ +// ObjectMapper objectMapper = new ObjectMapper(); +// +// DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); +// DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); +// +// Module timeModule = new JavaTimeModule() +// .addDeserializer(LocalDate.class,new LocalDateDeserializer(dateFormatter)) +// .addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(dateTimeFormatter)) +// .addSerializer(LocalDate.class,new LocalDateSerializer(dateFormatter)) +// .addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(dateTimeFormatter)); +// objectMapper.registerModules(timeModule); +// +// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); +// +// jackson2JsonRedisSerializer.setObjectMapper(objectMapper); +// +// return jackson2JsonRedisSerializer; +// +// +// } + +} + + + diff --git a/beacon-cache/src/main/java/com/mashibing/cache/contorller/TestController.java b/beacon-cache/src/main/java/com/mashibing/cache/contorller/TestController.java new file mode 100644 index 0000000..7772fb9 --- /dev/null +++ b/beacon-cache/src/main/java/com/mashibing/cache/contorller/TestController.java @@ -0,0 +1,37 @@ +package com.mashibing.cache.contorller; + +import com.msb.framework.redis.RedisClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.Map; +import java.util.Objects; + +/** + * @author kezhen + * @date 2022/12/25 + * @description + */ +@RestController +public class TestController { +// @Autowired +// @Resource +// private RedisTemplate redisTemplate; + + @Resource + private RedisClient redisClient; + @PostMapping("/test/set/{key}") + public String set(@PathVariable String key, @RequestBody Map map){ + System.out.println("TestController.set"); + redisClient.putMap(key,map); + return "ok"; + } + + @GetMapping("/text/get/{key}") + public Map get(@PathVariable String key) { + Map result = redisClient.getMap(key); + return result; + } +} diff --git a/beacon-cache/src/main/resources/bootstrap.yml b/beacon-cache/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..e802b24 --- /dev/null +++ b/beacon-cache/src/main/resources/bootstrap.yml @@ -0,0 +1,13 @@ +spring: + application: + name: beacon-cache + profiles: + active: dev + cloud: + nacos: + config: + server-addr: 192.168.1.8:8848 + file-extension: yml +# beacon-cache-dev.yml + discovery: + server-addr: 192.168.1.8:8848 \ No newline at end of file diff --git a/beacon-common/pom.xml b/beacon-common/pom.xml new file mode 100644 index 0000000..b489014 --- /dev/null +++ b/beacon-common/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.mashibing + beacon-cloud + 1.0-SNAPSHOT + + + beacon-common + + + 8 + 8 + UTF-8 + + + + + org.projectlombok + lombok + + + + \ No newline at end of file diff --git a/beacon-common/src/main/java/com/mashibing/common/model/StandardSubmit.java b/beacon-common/src/main/java/com/mashibing/common/model/StandardSubmit.java new file mode 100644 index 0000000..628a9fc --- /dev/null +++ b/beacon-common/src/main/java/com/mashibing/common/model/StandardSubmit.java @@ -0,0 +1,56 @@ +package com.mashibing.common.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import java.time.LocalDateTime; + +/** + * @author kezhen + * @date 2022/12/24 + * @description 在策略模块-短信网关模块 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +@ToString +public class StandardSubmit { + //针对当前短信的唯一标识,雪花算法 + private Long sequenceId; + //客户端ID + private Long cliendId; + //客户的ip白名单 + private String ip; + //客户业务内的uid,客户请求传递的 + private String uid; + //目标手机号 + private String mobile; + //短信内容的签名 + private String sign; + //短信内容 + private String text; + //短信的发送时间,当前系统时间 + private LocalDateTime sendTime; + //当前短信的费用,计算短信内容的文字,70个字一条,超过部分,67字一条 + private Long fee; + // 目标手机号的运营商(策略模块) + private Integer operatorId; + //目标手机号的归属区号(策略模块) + private Integer areaCode; + //区域名称,目标手机号的归属地(策略模块) + private String area; + //通道下发的源号码 eg:1069123412341234 (策略模块) + private String srcNumber; + /** + * 通道的id信息 (策略模块) + */ + private Long channelId; + /** + * 短信的发送状态,0-等待/发送ing,1-成功,2-失败,默认情况就是0 + */ + private int reportState; + + //后续再做封装 +} diff --git a/beancon-api/pom.xml b/beancon-api/pom.xml index 6480e16..a0172bd 100644 --- a/beancon-api/pom.xml +++ b/beancon-api/pom.xml @@ -52,6 +52,12 @@ org.springframework.boot spring-boot-starter-validation + + + com.mashibing + beacon-common + 1.0-SNAPSHOT + diff --git a/beancon-api/src/main/java/com/mashibing/api/controller/SmsController.java b/beancon-api/src/main/java/com/mashibing/api/controller/SmsController.java index 343c76c..0eac82f 100644 --- a/beancon-api/src/main/java/com/mashibing/api/controller/SmsController.java +++ b/beancon-api/src/main/java/com/mashibing/api/controller/SmsController.java @@ -5,6 +5,7 @@ import com.mashibing.api.from.SingleSendForm; import com.mashibing.api.util.R; import com.mashibing.api.vo.ResultVO; import lombok.extern.slf4j.Slf4j; +import org.springframework.util.StringUtils; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; @@ -12,6 +13,8 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.http.HttpServletRequest; + /** * @author kezhen * @date 2022/12/23 @@ -27,14 +30,55 @@ public class SmsController { * @return */ @PostMapping(value = "/single_send", produces = "application/json;charset=utf-8") - public ResultVO signleSend(@RequestBody @Validated SingleSendForm singleSendForm, BindingResult bindingResult) { + public ResultVO signleSend(@RequestBody @Validated SingleSendForm singleSendForm, BindingResult bindingResult, HttpServletRequest request) { System.out.println(singleSendForm); System.out.println("SmsController.signleSend"); if (bindingResult.hasErrors()) { String message = bindingResult.getFieldError().getDefaultMessage(); log.info("【接口模块-单条短信Contorller】 参数不合法,msg = {}", message); - return R.error(SmsCodeEnum.PARAMETER_ERROR.getCode(),message); + return R.error(SmsCodeEnum.PARAMETER_ERROR.getCode(), message); } + //===============构建StandardSubmit================== + + String ip = this.getRealIP(request); + System.out.println("ip = " + ip); + + + //===============发送到MQ,交给策略模块处理================== + return R.ok(); } + + /** + * 获取客户端真实的IP地址 + * + * @param request + * @return + */ + private String getRealIP(HttpServletRequest request) { + String ip; + String ips = request.getHeader("x-forwarded-for"); + if (StringUtils.isEmpty(ips) || "unknow".equalsIgnoreCase(ips)) { + return ips.substring(0, ips.indexOf(",")); + } + + ip = request.getHeader("x-real-ip"); + if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) { + ip = request.getHeader("Proxy-client-ip"); + + } + if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) { + ip = request.getHeader("wl-proxy-client-ip"); + } + if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) { + //7. 基于其他代理服务器的方式获取请求头的IP地址 + ip = request.getHeader("http_client_ip"); + } + if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) { + //8. 基于传统方式获取IP + ip = request.getRemoteAddr(); + } + + return ip; + } } diff --git a/pom.xml b/pom.xml index 185cb85..b27b633 100644 --- a/pom.xml +++ b/pom.xml @@ -17,14 +17,14 @@ pom beancon-api + beacon-common + beacon-cache Hoxton.SR12 2.2.6.RELEASE - 8 - 8 - UTF-8 + 1.0.0 @@ -43,6 +43,13 @@ pom import + + com.msb.cloud + horse-framework-starters + ${horse-version} + pom + import +