-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from adorabled4/feature/call-async
新增支持异步接口调用
- Loading branch information
Showing
39 changed files
with
607 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
api-common/src/main/java/com/dhx/apicommon/common/MQConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.dhx.apicommon.common; | ||
|
||
/** | ||
* @author adorabled4 | ||
* @className MQConstant | ||
* @date : 2024/01/02/ 09:22 | ||
**/ | ||
public class MQConstant { | ||
|
||
public static final String CALL_BACK_RESULT_EXCHANGE = "TurboAPI-call-back"; | ||
|
||
public static final String CALL_RESULT_QUEUE = "call.result"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
api-common/src/main/java/com/dhx/apicommon/model/v2/param/TranslateParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.dhx.apicommon.model.v2.param; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.validator.constraints.Length; | ||
|
||
|
||
/** | ||
* @author adorabled4 | ||
* @className TranslateParam | ||
* @date : 2024-1-2 07:58:12 | ||
**/ | ||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class TranslateParam { | ||
|
||
/** | ||
* 菜品 | ||
*/ | ||
@Length(min = 1,max = 3600) | ||
private String text; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../java/com/dhx/apicore/util/ThrowUtil.java → ...ava/com/dhx/apicommon/util/ThrowUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
api-core/src/main/java/com/dhx/apicore/controller/CallbackController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.dhx.apicore.controller; | ||
|
||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
import com.dhx.apicommon.common.BaseResponse; | ||
import com.dhx.apicommon.util.ResultUtil; | ||
import com.dhx.apicore.model.DO.CallBack; | ||
import com.dhx.apicore.model.query.AddCalLBackQuery; | ||
import com.dhx.apicore.model.query.PageQuery; | ||
import com.dhx.apicore.service.CallBackService; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.annotation.Resource; | ||
|
||
/** | ||
* @author adorabled4 | ||
* @className CallbackController | ||
* @date : 2024/01/02/ 00:07 | ||
**/ | ||
@RestController | ||
@Api(value = "回调配置") | ||
public class CallbackController { | ||
|
||
@Resource | ||
CallBackService callBackService; | ||
|
||
@PostMapping("/config/add") | ||
@ApiOperation(value = "添加接口回调地址配置") | ||
public BaseResponse<String> addCallBackConfig(AddCalLBackQuery query){ | ||
callBackService.addCallBackConfig(query); | ||
return ResultUtil.success(""); | ||
} | ||
|
||
|
||
@GetMapping("/config/delete") | ||
@ApiOperation(value = "删除接口回调地址配置") | ||
public BaseResponse<String> delCallBackConfig(@RequestParam("id")Long id){ | ||
callBackService.removeById(id); | ||
return ResultUtil.success("删除成功!"); | ||
} | ||
|
||
@GetMapping("/config/list") | ||
@ApiOperation(value = "查询接口回调地址配置") | ||
public BaseResponse<Page<CallBack>> listCallBackConfig(PageQuery query){ | ||
return ResultUtil.success(callBackService.listConfigs(query)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
api-core/src/main/java/com/dhx/apicore/listener/CallResultListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.dhx.apicore.listener; | ||
|
||
import cn.hutool.http.HttpRequest; | ||
import cn.hutool.json.JSONUtil; | ||
import com.dhx.apicommon.common.MQConstant; | ||
import com.dhx.apicommon.common.exception.BusinessException; | ||
import com.dhx.apicommon.common.exception.ErrorCode; | ||
import com.dhx.apicore.model.DO.CallBack; | ||
import com.dhx.apicore.model.DO.CallResult; | ||
import com.dhx.apicore.model.enums.CallApiStatusEnum; | ||
import com.dhx.apicore.service.CallBackService; | ||
import com.dhx.apicore.service.CallResultService; | ||
import com.rabbitmq.client.Channel; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.MDC; | ||
import org.springframework.amqp.core.ExchangeTypes; | ||
import org.springframework.amqp.rabbit.annotation.Exchange; | ||
import org.springframework.amqp.rabbit.annotation.Queue; | ||
import org.springframework.amqp.rabbit.annotation.QueueBinding; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.amqp.support.AmqpHeaders; | ||
import org.springframework.messaging.handler.annotation.Header; | ||
import org.springframework.stereotype.Component; | ||
import org.yaml.snakeyaml.representer.BaseRepresenter; | ||
|
||
import javax.annotation.Resource; | ||
|
||
import java.io.IOException; | ||
|
||
import static com.dhx.apicommon.common.BaseResponse.TRACE_ID; | ||
|
||
/** | ||
* @author adorabled4 | ||
* @className CallResultListener | ||
* @date : 2024/01/02/ 09:26 | ||
**/ | ||
@Component | ||
@Slf4j | ||
public class CallResultListener { | ||
|
||
@Resource | ||
CallResultService callResultService; | ||
|
||
@Resource | ||
CallBackService callBackService; | ||
|
||
/** | ||
* 接收异步接口执行结果 | ||
*/ | ||
@RabbitListener(bindings = @QueueBinding(value = @Queue(name = MQConstant.CALL_RESULT_QUEUE), | ||
exchange = @Exchange(name = MQConstant.CALL_BACK_RESULT_EXCHANGE, type = ExchangeTypes.DIRECT), | ||
key = MQConstant.CALL_RESULT_QUEUE)) | ||
public void callResultSendListener(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliverTag) throws IOException { | ||
String traceId = MDC.get(TRACE_ID); | ||
try{ | ||
log.info("receive message :{}", message); | ||
if (StringUtils.isBlank(message)) { | ||
channel.basicNack(deliverTag, false, false); | ||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "接收到的消息为空!"); | ||
} | ||
// 查询config | ||
BaseRepresenter invokeResult = JSONUtil.toBean(message, BaseRepresenter.class); | ||
CallResult callResult = callResultService.findByTraceId(traceId); | ||
CallBack callBack = callBackService.findCallBackConfig(callResult.getInterfaceId(),callResult.getUserId()); | ||
String callBackUrl = callBack.getCallBackUrl(); | ||
// 发送结果 | ||
HttpRequest.post(callBackUrl).body(invokeResult.toString()).executeAsync(); | ||
// 更新调用状态 | ||
callResultService.updateCallStatus(traceId, CallApiStatusEnum.SUCCEED); | ||
}catch (Exception e){ | ||
callResultService.updateCallStatus(traceId, CallApiStatusEnum.FAILED); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
api-core/src/main/java/com/dhx/apicore/model/query/AddCalLBackQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.dhx.apicore.model.query; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.validation.constraints.Pattern; | ||
|
||
/** | ||
* @author adorabled4 | ||
* @className AddCalLBackQuery | ||
* @date : 2024/01/02/ 00:08 | ||
**/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class AddCalLBackQuery { | ||
|
||
@Pattern(regexp ="^(http|https)://.*$", message = "非法的回调消息接收地址!") | ||
private String callBackUrl; | ||
|
||
private Long interfaceId; | ||
|
||
} |
Oops, something went wrong.