feat: openFeign微服务之间远程调用
This commit is contained in:
@@ -1,17 +1,32 @@
|
||||
package com.schisandra.auth.application.config;
|
||||
|
||||
import com.schisandra.auth.common.constants.SmsConfigConstant;
|
||||
import com.schisandra.auth.common.redis.RedisUtil;
|
||||
import com.schisandra.auth.infra.entity.SmsConfigInfo;
|
||||
import com.schisandra.auth.infra.rpc.SmsConfigRpc;
|
||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@Configuration
|
||||
|
||||
//@Configuration
|
||||
public class SmsInitConfig {
|
||||
@Resource
|
||||
SmsReadConfig smsReadConfig;
|
||||
@Resource
|
||||
SmsConfigRpc smsConfigRpc;
|
||||
|
||||
@Resource
|
||||
RedisUtil redisUtil;
|
||||
|
||||
|
||||
@EventListener
|
||||
public void init(ContextRefreshedEvent event) {
|
||||
SmsFactory.createSmsBlend(smsReadConfig, "alibaba");
|
||||
SmsConfigInfo configInfo = smsConfigRpc.getConfigInfo(SmsConfigConstant.SMS_CONFIG_KEY);
|
||||
redisUtil.set(SmsConfigConstant.SMS_CONFIG_KEY, configInfo.getConfigValue());
|
||||
// 初始化短信配置
|
||||
SmsFactory.createSmsBlend(smsReadConfig, configInfo.getConfigValue());
|
||||
}
|
||||
}
|
||||
|
@@ -4,9 +4,12 @@ package com.schisandra.auth.application.controller;
|
||||
import com.schisandra.auth.application.config.SmsReadConfig;
|
||||
import com.schisandra.auth.application.factory.SmsTypeHandlerFactory;
|
||||
import com.schisandra.auth.application.handler.SchisandraSmsTypeHandler;
|
||||
import com.schisandra.auth.common.constants.SmsConfigConstant;
|
||||
import com.schisandra.auth.common.entity.Result;
|
||||
import com.schisandra.auth.common.redis.RedisUtil;
|
||||
import com.schisandra.auth.common.utils.SmsCodeUtils;
|
||||
import com.schisandra.auth.infra.entity.SmsConfigInfo;
|
||||
import com.schisandra.auth.infra.rpc.SmsConfigRpc;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
@@ -28,7 +31,11 @@ public class SchisandraSmsController {
|
||||
@Resource
|
||||
private SmsReadConfig smsReadConfig;
|
||||
|
||||
@Resource
|
||||
SmsConfigRpc smsConfigRpc;
|
||||
|
||||
private final String authPhonePrefix="auth.phone";
|
||||
private final String smsConfigPrefix="sys.config.sms";
|
||||
/**
|
||||
* @description: 发送短信验证码
|
||||
* @param: [phone]
|
||||
@@ -41,7 +48,12 @@ public class SchisandraSmsController {
|
||||
String prefix = redisUtil.buildKey(authPhonePrefix, phone);
|
||||
String code = SmsCodeUtils.generateValidateCode(4).toString();
|
||||
if (!redisUtil.exist(prefix)){
|
||||
SmsResponse smsResponse=SmsFactory.getSmsBlend("alibaba").sendMessage(phone,code);
|
||||
SmsConfigInfo configInfo = smsConfigRpc.getConfigInfo(SmsConfigConstant.SMS_CONFIG_KEY);
|
||||
String key = redisUtil.buildKey(smsConfigPrefix, SmsConfigConstant.SMS_CONFIG_KEY);
|
||||
redisUtil.set(key, configInfo.getConfigValue());
|
||||
SmsFactory.createSmsBlend(smsReadConfig, configInfo.getConfigValue());
|
||||
String configId = redisUtil.get(SmsConfigConstant.SMS_CONFIG_KEY);
|
||||
SmsResponse smsResponse=SmsFactory.getSmsBlend(configId).sendMessage(phone,code);
|
||||
if (smsResponse.isSuccess()){
|
||||
redisUtil.setNx(prefix, code, 60L,SECONDS);
|
||||
return Result.ok();
|
||||
|
@@ -0,0 +1,14 @@
|
||||
package com.schisandra.auth.common.constants;
|
||||
|
||||
/**
|
||||
* @Classname SmsConfigConstant
|
||||
* @BelongsProject: schisandra-cloud-storage
|
||||
* @BelongsPackage: com.schisandra.auth.common.constants
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-05-12 00:16
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class SmsConfigConstant {
|
||||
public static final String SMS_CONFIG_KEY = "sms_config_id";
|
||||
}
|
@@ -47,5 +47,10 @@
|
||||
<artifactId>schisandra-cloud-storage-auth-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.schisandra.system</groupId>
|
||||
<artifactId>schisandra-cloud-storage-system-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@@ -0,0 +1,44 @@
|
||||
package com.schisandra.auth.infra.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Classname SmsConfigInfo
|
||||
* @BelongsProject: schisandra-cloud-storage
|
||||
* @BelongsPackage: com.schisandra.auth.application.rpc.entity
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-05-11 23:50
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Data
|
||||
public class SmsConfigInfo implements Serializable {
|
||||
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private String configName;
|
||||
|
||||
/**
|
||||
* 参数键
|
||||
*/
|
||||
private String configKey;
|
||||
|
||||
/**
|
||||
* 参数值
|
||||
*/
|
||||
private String configValue;
|
||||
|
||||
/**
|
||||
* 系统内置(0是 1否)
|
||||
*/
|
||||
private Integer configType;
|
||||
|
||||
/**
|
||||
* 备注信息
|
||||
*/
|
||||
private String remarks;
|
||||
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package com.schisandra.auth.infra.rpc;
|
||||
|
||||
|
||||
import com.schisandra.auth.infra.entity.SmsConfigInfo;
|
||||
import com.schisandra.system.api.SmsConfigFeignService;
|
||||
import com.schisandra.system.entity.Result;
|
||||
import com.schisandra.system.entity.SchisandraSysConfigDTO;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @Classname SmsConfigRpc
|
||||
* @BelongsProject: schisandra-cloud-storage
|
||||
* @BelongsPackage: com.schisandra.auth.application.rpc
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-05-11 23:44
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
public class SmsConfigRpc {
|
||||
@Resource
|
||||
private SmsConfigFeignService smsConfigFeignService;
|
||||
|
||||
public SmsConfigInfo getConfigInfo(String key) {
|
||||
Result<SchisandraSysConfigDTO> result = smsConfigFeignService.getConfigByKey(key);
|
||||
SmsConfigInfo smsConfigInfo = new SmsConfigInfo();
|
||||
if (!result.getSuccess()) {
|
||||
return smsConfigInfo;
|
||||
}
|
||||
SchisandraSysConfigDTO data = result.getData();
|
||||
smsConfigInfo.setConfigKey(data.getConfigKey());
|
||||
smsConfigInfo.setConfigName(data.getConfigName());
|
||||
smsConfigInfo.setConfigValue(data.getConfigValue());
|
||||
smsConfigInfo.setRemarks(data.getRemarks());
|
||||
smsConfigInfo.setConfigType(data.getConfigType());
|
||||
return smsConfigInfo;
|
||||
}
|
||||
|
||||
}
|
@@ -1 +0,0 @@
|
||||
rpc
|
@@ -36,7 +36,14 @@
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.schisandra</groupId>
|
||||
<artifactId>schisandra-cloud-storage-auth-application-controller</artifactId>
|
||||
|
@@ -3,6 +3,7 @@ package com.schisandra.auth;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
@@ -14,6 +15,7 @@ import org.springframework.context.annotation.ComponentScan;
|
||||
@SpringBootApplication
|
||||
@ComponentScan("com.schisandra")
|
||||
@MapperScan("com.schisandra.**.dao")
|
||||
@EnableFeignClients(basePackages = "com.schisandra")
|
||||
public class AuthApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AuthApplication.class);
|
||||
|
@@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 5000
|
||||
port: 6000
|
||||
spring:
|
||||
cloud:
|
||||
gateway:
|
||||
@@ -21,7 +21,7 @@ spring:
|
||||
# Redis数据库索引(默认为0)
|
||||
database: 1
|
||||
# Redis服务器地址
|
||||
host: 116.196.80.239
|
||||
host: 1.95.0.111
|
||||
# Redis服务器连接端口
|
||||
port: 6379
|
||||
# Redis服务器连接密码(默认为空)
|
||||
|
@@ -0,0 +1,16 @@
|
||||
package com.schisandra.system.api;
|
||||
|
||||
import com.schisandra.system.entity.Result;
|
||||
import com.schisandra.system.entity.SchisandraSysConfigDTO;
|
||||
import feign.Param;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@FeignClient("schisandra-cloud-storage-system-dev")
|
||||
public interface SmsConfigFeignService {
|
||||
@RequestMapping(value = "/system/getConfigByKey",method = RequestMethod.GET)
|
||||
Result<SchisandraSysConfigDTO> getConfigByKey(@RequestParam(value = "key") String key);
|
||||
}
|
@@ -1 +0,0 @@
|
||||
api 对外接口
|
@@ -0,0 +1,50 @@
|
||||
package com.schisandra.system.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Result<T> {
|
||||
|
||||
private Boolean success;
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String message;
|
||||
|
||||
private T data;
|
||||
|
||||
public static Result ok(){
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
result.setCode(ResultCodeEnum.SUCCESS.getCode());
|
||||
result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result ok(T data){
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
result.setCode(ResultCodeEnum.SUCCESS.getCode());
|
||||
result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Result fail(){
|
||||
Result result = new Result();
|
||||
result.setSuccess(false);
|
||||
result.setCode(ResultCodeEnum.FAIL.getCode());
|
||||
result.setMessage(ResultCodeEnum.FAIL.getDesc());
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result fail(T data){
|
||||
Result result = new Result();
|
||||
result.setSuccess(false);
|
||||
result.setCode(ResultCodeEnum.FAIL.getCode());
|
||||
result.setMessage(ResultCodeEnum.FAIL.getDesc());
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.schisandra.system.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum ResultCodeEnum {
|
||||
|
||||
SUCCESS(200,"成功"),
|
||||
FAIL(500,"失败");
|
||||
|
||||
public int code;
|
||||
|
||||
public String desc;
|
||||
|
||||
ResultCodeEnum(int code,String desc){
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public static ResultCodeEnum getByCode(int codeVal){
|
||||
for(ResultCodeEnum resultCodeEnum : ResultCodeEnum.values()){
|
||||
if(resultCodeEnum.code == codeVal){
|
||||
return resultCodeEnum;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,73 @@
|
||||
package com.schisandra.system.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* dto
|
||||
*
|
||||
* @author landaiqing
|
||||
* @since 2024-05-11 22:45:55
|
||||
*/
|
||||
@Data
|
||||
public class SchisandraSysConfigDTO implements Serializable {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private String configName;
|
||||
|
||||
/**
|
||||
* 参数键
|
||||
*/
|
||||
private String configKey;
|
||||
|
||||
/**
|
||||
* 参数值
|
||||
*/
|
||||
private String configValue;
|
||||
|
||||
/**
|
||||
* 创建者
|
||||
*/
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 系统内置(0是 1否)
|
||||
*/
|
||||
private Integer configType;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createDate;
|
||||
|
||||
/**
|
||||
* 更新者
|
||||
*/
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateDate;
|
||||
|
||||
/**
|
||||
* 备注信息
|
||||
*/
|
||||
private String remarks;
|
||||
|
||||
/**
|
||||
* 是否删除 0未删除 1已删除
|
||||
*/
|
||||
private Integer isDeleted;
|
||||
|
||||
}
|
||||
|
@@ -1 +0,0 @@
|
||||
api 实体
|
@@ -8,15 +8,15 @@ import com.schisandra.system.application.dto.SchisandraSysConfigDTO;
|
||||
import com.schisandra.system.common.entity.Result;
|
||||
import com.schisandra.system.domain.entity.SchisandraSysConfigBO;
|
||||
import com.schisandra.system.domain.service.SchisandraSysConfigDomainService;
|
||||
import feign.Param;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* controller
|
||||
* controller
|
||||
*
|
||||
* @author landaiqing
|
||||
* @since 2024-05-11 22:45:55
|
||||
@@ -119,4 +119,16 @@ public class SchisandraSysConfigController {
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping(value = "getConfigByKey",method = RequestMethod.GET)
|
||||
public Result<SchisandraSysConfigDTO> getConfigByKey(@RequestParam(value = "key") String key) {
|
||||
try {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.info("SchisandraSysConfigController.getConfigByKey.key:{}", key);
|
||||
}
|
||||
return Result.ok(schisandraSysConfigDomainService.getConfigByKey(key));
|
||||
} catch (Exception e) {
|
||||
return Result.fail("配置信息获取失败");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -19,4 +19,5 @@ public interface SchisandraSysConfigDTOConverter {
|
||||
|
||||
SchisandraSysConfigBO convertDTOToBO(SchisandraSysConfigDTO schisandraSysConfigDTO);
|
||||
|
||||
|
||||
}
|
||||
|
@@ -17,5 +17,6 @@ public interface SchisandraSysConfigBOConverter {
|
||||
SchisandraSysConfigBOConverter INSTANCE = Mappers.getMapper(SchisandraSysConfigBOConverter.class);
|
||||
|
||||
SchisandraSysConfig convertBOToEntity(SchisandraSysConfigBO schisandraSysConfigBO);
|
||||
SchisandraSysConfigBO convertEntityToBO(SchisandraSysConfig schisandraSysConfig);
|
||||
|
||||
}
|
||||
|
@@ -25,4 +25,5 @@ public interface SchisandraSysConfigDomainService {
|
||||
*/
|
||||
Boolean delete(SchisandraSysConfigBO schisandraSysConfigBO);
|
||||
|
||||
Object getConfigByKey(String key);
|
||||
}
|
||||
|
@@ -46,4 +46,14 @@ public class SchisandraSysConfigDomainServiceImpl implements SchisandraSysConfig
|
||||
return schisandraSysConfigService.update(schisandraSysConfig) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SchisandraSysConfigBO getConfigByKey(String key) {
|
||||
SchisandraSysConfig schisandraSysConfig= schisandraSysConfigService.getConfigByKey(key);
|
||||
SchisandraSysConfigBO schisandraSysConfigBO = SchisandraSysConfigBOConverter.INSTANCE.convertEntityToBO(schisandraSysConfig);
|
||||
if (schisandraSysConfigBO == null){
|
||||
return null;
|
||||
}
|
||||
return schisandraSysConfigBO;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -48,4 +48,5 @@ public interface SchisandraSysConfigService {
|
||||
*/
|
||||
SchisandraSysConfig queryByCondition(SchisandraSysConfig schisandraSysConfig);
|
||||
|
||||
SchisandraSysConfig getConfigByKey(String key);
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package com.schisandra.system.infra.basic.service.impl;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.schisandra.system.infra.basic.entity.SchisandraSysConfig;
|
||||
import com.schisandra.system.infra.basic.dao.SchisandraSysConfigDao;
|
||||
@@ -93,4 +94,10 @@ public class SchisandraSysConfigServiceImpl implements SchisandraSysConfigServic
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public SchisandraSysConfig getConfigByKey(String key) {
|
||||
return schisandraSysConfigDao.selectOne(new QueryWrapper<SchisandraSysConfig>().eq("config_key", key)
|
||||
.eq("is_deleted", 0));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -39,6 +39,14 @@
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.schisandra.system</groupId>
|
||||
|
@@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 4000
|
||||
port: 5000
|
||||
spring:
|
||||
datasource:
|
||||
username: root
|
||||
|
@@ -1,6 +1,6 @@
|
||||
spring:
|
||||
application:
|
||||
name: schisandra-cloud-storage-oss-dev
|
||||
name: schisandra-cloud-storage-system-dev
|
||||
profiles:
|
||||
active: dev
|
||||
cloud:
|
||||
|
Reference in New Issue
Block a user