diff --git a/beacon-api/pom.xml b/beacon-api/pom.xml
index 115408d..e89af55 100644
--- a/beacon-api/pom.xml
+++ b/beacon-api/pom.xml
@@ -59,6 +59,11 @@
beacon-common
1.0-SNAPSHOT
+
+
+ org.springframework.cloud
+ spring-cloud-starter-openfeign
+
\ No newline at end of file
diff --git a/beacon-api/src/main/java/com/mashibing/api/ApiStarterApp.java b/beacon-api/src/main/java/com/mashibing/api/ApiStarterApp.java
index 709637e..ddc220e 100644
--- a/beacon-api/src/main/java/com/mashibing/api/ApiStarterApp.java
+++ b/beacon-api/src/main/java/com/mashibing/api/ApiStarterApp.java
@@ -3,6 +3,7 @@ package com.mashibing.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author zjw
@@ -10,6 +11,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
*/
@SpringBootApplication
@EnableDiscoveryClient
+@EnableFeignClients
public class ApiStarterApp {
public static void main(String[] args) {
diff --git a/beacon-api/src/main/java/com/mashibing/api/client/BeaconCacheClient.java b/beacon-api/src/main/java/com/mashibing/api/client/BeaconCacheClient.java
new file mode 100644
index 0000000..fc9f74e
--- /dev/null
+++ b/beacon-api/src/main/java/com/mashibing/api/client/BeaconCacheClient.java
@@ -0,0 +1,14 @@
+package com.mashibing.api.client;
+
+import org.springframework.cloud.openfeign.FeignClient;
+
+/**
+ * @author zjw
+ * @description
+ */
+@FeignClient(value = "beacon-cache")
+public interface BeaconCacheClient {
+
+
+
+}
diff --git a/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java b/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java
index 7110097..0454c68 100644
--- a/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java
+++ b/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java
@@ -4,7 +4,11 @@ import com.mashibing.api.enums.SmsCodeEnum;
import com.mashibing.api.form.SingleSendForm;
import com.mashibing.api.util.R;
import com.mashibing.api.vo.ResultVO;
+import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
@@ -12,6 +16,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 zjw
* @description
@@ -19,20 +25,87 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/sms")
@Slf4j
+@RefreshScope
public class SmsController {
+ /**
+ * 客户端IP地址的请求头信息,多个用','隔开。
+ */
+ @Value("${headers}")
+ private String headers;
+
+ /**
+ * 基于请求头获取信息时,可能获取到的未知信息
+ */
+ private final String UNKNOW = "unknow";
+
+ /**
+ * 如果是当前请求头获取IP地址,需要截取到第一个','未知
+ */
+ private final String X_FORWARDED_FOR = "x-forwarded-for";
+
+ /**
+ * 单条验证短信接口
+ * @param singleSendForm
+ * @param bindingResult
+ * @param req
+ * @return
+ */
@PostMapping(value = "/single_send",produces = "application/json;charset=utf-8")
- public ResultVO singleSend(@RequestBody @Validated SingleSendForm singleSendForm, BindingResult bindingResult){
+ public ResultVO singleSend(@RequestBody @Validated SingleSendForm singleSendForm, BindingResult bindingResult, HttpServletRequest req){
//1. 校验参数
if (bindingResult.hasErrors()){
String msg = bindingResult.getFieldError().getDefaultMessage();
log.info("【接口模块-单条短信Controller】 参数不合法 msg = {}",msg);
return R.error(SmsCodeEnum.PARAMETER_ERROR.getCode(),msg);
}
+ //=========================获取真实的IP地址=========================================
+ String ip = this.getRealIP(req);
+
//=========================构建StandardSubmit,各种封装校验=========================================
+ StandardSubmit submit = new StandardSubmit();
+ submit.setRealIP(ip);
+ submit.setApikey(singleSendForm.getApikey());
+ submit.setMobile(singleSendForm.getMobile());
+ submit.setText(singleSendForm.getText());
+ submit.setState(singleSendForm.getState());
+ submit.setUid(singleSendForm.getUid());
//=========================发送到MQ,交给策略模块处理=========================================
return R.ok();
}
+
+ /**
+ * 获取客户端真实的IP地址
+ * @param req
+ * @return
+ */
+ private String getRealIP(HttpServletRequest req) {
+ //1. 声明返回的ip地址
+ String ip;
+
+ //2. 遍历请求头,并且通过req获取ip地址
+ for (String header : headers.split(",")) {
+ // 健壮性校验
+ if (!StringUtils.isEmpty(header)) {
+ // 基于req获取ip地址
+ ip = req.getHeader(header);
+ // 如果获取到的ip不为null,不为空串,并且不为unknow,就可以返回
+ if (!StringUtils.isEmpty(ip) && !UNKNOW.equalsIgnoreCase(ip)) {
+ // 判断请求头是否是x-forwarded-for
+ if (X_FORWARDED_FOR.equalsIgnoreCase(header) && ip.indexOf(",") > 0) {
+ ip = ip.substring(0,ip.indexOf(","));
+ }
+ // 返回IP地址
+ return ip;
+ }
+ }
+ }
+
+ //3. 如果请求头都没有获取到IP地址,直接基于传统的方式获取一个IP
+ return req.getRemoteAddr();
+ }
+
+
}
diff --git a/beacon-cache/pom.xml b/beacon-cache/pom.xml
index 87c4f92..6a5d0e6 100644
--- a/beacon-cache/pom.xml
+++ b/beacon-cache/pom.xml
@@ -28,9 +28,14 @@
spring-cloud-starter-alibaba-nacos-config
-
+
+
+
+ com.msb.cloud
+ horse-framework-starter-redis
diff --git a/beacon-cache/src/main/java/com/mashibing/cache/CacheStarterApp.java b/beacon-cache/src/main/java/com/mashibing/cache/CacheStarterApp.java
index 13a7602..8197c22 100644
--- a/beacon-cache/src/main/java/com/mashibing/cache/CacheStarterApp.java
+++ b/beacon-cache/src/main/java/com/mashibing/cache/CacheStarterApp.java
@@ -1,5 +1,7 @@
package com.mashibing.cache;
+import org.redisson.RedissonLock;
+import org.redisson.client.RedisClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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
index b025590..40e98e1 100644
--- a/beacon-cache/src/main/java/com/mashibing/cache/config/RedisConfig.java
+++ b/beacon-cache/src/main/java/com/mashibing/cache/config/RedisConfig.java
@@ -19,10 +19,12 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
+ * 因为添加了飞马框架的依赖,这些可以不用写啦。
* 设置RedisTemplate的序列化方式
* @author zjw
* @description
*/
+/*
@Configuration
public class RedisConfig {
@@ -76,3 +78,4 @@ public class RedisConfig {
}
+*/
diff --git a/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java b/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java
new file mode 100644
index 0000000..1c5a192
--- /dev/null
+++ b/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java
@@ -0,0 +1,41 @@
+package com.mashibing.cache.controller;
+
+import com.msb.framework.redis.RedisClient;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author zjw
+ * @description
+ */
+@RestController
+@Slf4j
+public class CacheController {
+
+ @Autowired
+ private RedisClient redisClient;
+
+ @PostMapping(value = "/cache/hmset/{key}")
+ public void hmset(@PathVariable(value = "key") String key, @RequestBody Map map) {
+ log.info("【缓存模块】 hmset方法,存储key = {},存储value = {}", key, map);
+ redisClient.putMap(key, map);
+ }
+
+ @PostMapping(value = "/cache/set/{key}")
+ public void set(@PathVariable(value = "key") String key, @RequestParam(value = "value") Object value) {
+ log.info("【缓存模块】 set方法,存储key = {},存储value = {}", key, value);
+ redisClient.set(key, value);
+ }
+
+ @PostMapping(value = "/cache/sadd/{key}")
+ public void sadd(@PathVariable(value = "key")String key, @RequestBody Map... value){
+ log.info("【缓存模块】 sadd方法,存储key = {},存储value = {}", key, value);
+ redisClient.sAdd(key,value);
+ }
+
+}
diff --git a/beacon-cache/src/main/java/com/mashibing/cache/controller/TestController.java b/beacon-cache/src/main/java/com/mashibing/cache/controller/TestController.java
index 47a1628..88ff3ba 100644
--- a/beacon-cache/src/main/java/com/mashibing/cache/controller/TestController.java
+++ b/beacon-cache/src/main/java/com/mashibing/cache/controller/TestController.java
@@ -1,5 +1,6 @@
package com.mashibing.cache.controller;
+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.*;
@@ -14,18 +15,18 @@ import java.util.Map;
public class TestController {
@Autowired
- private RedisTemplate redisTemplate;
+ private RedisClient redisClient;
// 写测试 hash结构
@PostMapping("/test/set/{key}")
public String set(@PathVariable String key, @RequestBody Map map){
- redisTemplate.opsForHash().putAll(key,map);
+ redisClient.putMap(key,map);
return "ok";
}
// 读测试
@GetMapping("/test/get/{key}")
public Map get(@PathVariable String key){
- Map
+
+ com.msb.cloud
+ horse-framework-starters
+ ${horse-version}
+ pom
+ import
+