当前位置:网站首页>Thinking and precipitation after docking with hundreds of third-party APIs
Thinking and precipitation after docking with hundreds of third-party APIs
2022-06-10 16:04:00 【InfoQ】
One 、 Chat with third parties
One ) Why is there such a supplier of third-party functions ?
Two ) Provided by the supplier Api Ability
Two 、 Third party concerns and considerations
One ) concerns
1、 Service provider
2、 Service access provider
Two ) Pay attention to item
1、 Pull away code that interacts with third parties
1) Customize a AliApiServerException It's abnormal
/**
* AliAPIServerException
*
* @author AFlymamba
*/
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class AliApiServerException extends Exception {
private static final long serialVersionUID = -3064220748468350413L;
String message;
}
2) Construct a AliApiServerService
/**
* AliApiServerService
* <p>
* bad response:result=0&errMsg=xxx
*
* @author AFlymamba
*/
public interface AliApiServerService {
/**
* Send Single Message
*
* @param phone Mobile
* @param content content
* @return send result
* @throws AliApiServerException Server Exception
*/
JSONObject sendSingleMessage(String phone, String content) throws AliApiServerException;
}
3) structure AliApiServiceImpl
/**
* AliApiServerServiceImpl
*
* @author AFlymamba
*/
@Slf4j
@Service
public class AliApiServerServiceImpl implements AliApiServerService {
@Override
public JSONObject sendSingleMessage(final String phone, final String content) throws AliApiServerException {
// ignore phone、content check
String requestUrl = StrBuilder.create("http://xxx").append("/api/v1/xxx").toString();
JSONObject requestBody = JSONUtil.createObj().putOnce("phone", phone).putOnce("content", content);
// ignore header、encrypt、sign
try {
String responseBody = HttpRequest.post(requestUrl).body(requestBody.toString()).timeout(3000).execute().body();
log.info("[ AliApiServer ] sendSingleMessage,requestUrl is [{}],requestBody is [{}], origin response is [{}]", requestUrl, requestBody, responseBody);
if (JSONUtil.isJson(responseBody)) {
return JSONUtil.parseObj(responseBody);
}
} catch (Exception e) {
log.error("[ AliApiServer ] sendSingleMessage exception,requestUrl is [{}],requestBody is [{}],exception message is [{}],cause is [{}]", requestUrl, requestBody, e.getMessage(), e.getCause());
}
throw new AliApiServerException("AliApiServerException, please check log......");
}
}
2、 Interaction between business side and third-party interaction code
1) The business point and the third-party interaction point directly invoke
/**
* ActivityServiceImpl
*
* @author AFlymamba
*/
@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {
private final AliApiServerService aliApiServerService;
@Autowired
public ActivityServiceImpl(
AliApiServerService aliApiServerService
) {
this.aliApiServerService = aliApiServerService;
}
@Override
public void sendMessage(final String phone, final String content) {
// ignore
try {
JSONObject sendResult = aliApiServerService.sendSingleMessage(phone, content);
} catch (AliApiServerException e) {
// ignore
}
}
}
2) introduce MessageServiceImpl To decouple the coupling point of mode 1
@Slf4j
@Service
public class MessageServiceImpl extends MessageService {
private final AliApiServerService aliApiServerService;
@Autowired
public MessageServiceImpl(
AliApiServerService aliApiServerService
) {
this.aliApiServerService = aliApiServerService;
}
@Override
public void sendMessage(final String phone, final String content) {
// ignore
try {
JSONObject sendResult = aliApiServerService.sendSingleMessage(phone, content);
} catch (AliApiServerException e) {
// ignore
}
}
}
// Code adjustment in method 1 above
@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {
private final MessageService messageService;
@Autowired
public ActivityServiceImpl(
MessageService messageService
) {
this.messageService = messageService;
}
@Override
public void sendMessage(final String phone, final String content) {
// ignore
try {
JSONObject sendResult = messageService.sendSingleMessage(phone, content);
} catch (AliApiServerException e) {
// ignore
}
}
}
3) With the help of the event publishing mechanism
- Customize a MessageSendEvent
/**
* MessageSendEvent
*
* @author AFlymamba
*/
@Getter
public class MessageSendEvent extends ApplicationEvent {
private static final long serialVersionUID = 6456904953414622941L;
private JSONObject messageData;
public MessageSendEvent(final Object source, final JSONObject messageData) {
super(source);
this.messageData = messageData;
}
}
- modify activityServiceImpl Add event publishing logic
@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {
private final ApplicationContext applicationContext;
@Autowired
public ActivityServiceImpl(
ApplicationContext applicationContext
) {
this.applicationContext = applicationContext;
}
@Override
public void sendMessage(final String phone, final String content) {
// ignore
JSONObject messageData = JSONUtil.createObj().putOnce("phone", phone).putOnce("content", content);
MessageSendEvent messageSendEvent = new MessageSendEvent(this, messageData);
applicationContext.publishEvent(messageSendEvent);
}
}
- Consumer logic
@EventListener
public void consumeMessage(final MessageSendEvent messageSendEvent) {
// ignore
try {
JSONObject messageData = messageSendEvent.getMessageData();
JSONObject sendResult = aliApiServerService.sendSingleMessage(messageData.getStr("phone"), messageData.getStr("content"));
} catch (AliApiServerException e) {
// ignore
}
}
4) Open your brain
3、 ... and 、 It is recommended to write some reasons for docking with third-party code
Four 、 summary
边栏推荐
- 作用域和闭包
- Google x open source grabbing manipulator can find the target part at a glance without manual marking [turn]
- Code implementation of sorting and serializing cases in MapReduce
- The ultimate buff of smart grid - guanghetong module runs through the whole process of "generation, transmission, transformation, distribution and utilization"
- this和对象原型
- C # game prototype character map dual movement
- uniapp中常用到的方法(部分) - 時間戳問題及富文本解析圖片問題
- Application scenario introduction of nixie tube driver chip + voice chip, wt588e02b-24ss
- Save a window with a specific size, resolution, or background color
- Unified certification center oauth2 certification pit
猜你喜欢
![2D human pose estimation with residual log likelihood estimation (RLE) [link only]](/img/c7/9c25da07236ef0bd241b6023e82306.gif)
2D human pose estimation with residual log likelihood estimation (RLE) [link only]

「技术干货」工业触摸屏之驱动开发及异常分析(连载)

Sm59 remote connection. If you are prompted that there is no host, add host to the server and restart SAP_ SAP LIUMENG

Driver development and abnormal analysis of "technical dry goods" industrial touch screen (serial)

Guanghetong high computing power intelligent module injects intelligence into 5g c-v2x in the trillion market
![姿态估计之2D人体姿态估计 - Human Pose Regression with Residual Log-likelihood Estimation(RLE)[仅链接]](/img/c7/9c25da07236ef0bd241b6023e82306.gif)
姿态估计之2D人体姿态估计 - Human Pose Regression with Residual Log-likelihood Estimation(RLE)[仅链接]

广和通携手中国移动、惠普、联发科、英特尔合作打造5G全互联PC泛终端系列产品

ORB_ Slam2 visual inertial tight coupling positioning technology route and code explanation 1 - IMU flow pattern pre integration

AEC of the three swordsmen in audio and video processing: the cause of echo generation and the principle of echo cancellation

MapReduce案例之排序
随机推荐
Aggregate sum of MapReduce cases
2D human posture estimation for posture estimation - numerical coordinate progression with revolutionary neural networks (dsnt)
[section 7 function]
统一认证中心 Oauth2 认证坑
The product design software figma cannot be used. What software with similar functions is available in China
MapReduce之Word Count案例代码实现
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
Vins theory and code explanation 0 -- theoretical basis in vernacular
Anba cv2fs/cv22fs obtained ASIL C chip function safety certification, surpassing the level of similar chips in the market
VINS理論與代碼詳解4——初始化
Detailed installation steps of mysql8
uniapp中常用到的方法(部分) - 时间戳问题及富文本解析图片问题
Yuntu says that every successful business system cannot be separated from apig
2D human posture estimation for posture estimation - simple baseline (SBL)
[untitled]
Tensorflow actual combat Google deep learning framework second edition learning summary tensorflow introduction
作用域和闭包
torch. utils. data. Dataloader() details [pytoch getting started manual]
Summary of methods for point projection onto a plane
运行mapreduce任务缺失setJarByClass()报错找不到类