当前位置:网站首页>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开源抓取机械臂,无需人工标注就能一眼找到目标零件[转]
- MapReduce案例之排序
- 数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
- Aperçu en direct | déconstruire OLAP! Le nouveau paradigme de l'architecture d'analyse multidimensionnelle est entièrement ouvert! Apache Doris va apporter cinq gros problèmes!
- 运行mapreduce任务缺失setJarByClass()报错找不到类
- torch. nn. utils. rnn. pad_ Detailed explanation of sequence() [pytoch getting started manual]
- 2D human posture estimation for posture estimation - associated embedding: end to end learning for joint detection and grouping
- json.load(s)与json.dump(s)
- CAP 6.1 版本发布通告
- MapReduce案例之聚合求和
猜你喜欢

初学pytorch踩坑

Interpretation of cube technology | past and present life of cube Rendering Design

Anti "internal roll", it is said that 360 enterprise security cloud will launch the "one click forced off duty" function, and the computer will automatically close the office software

sm59远程连接,如果提示没有host,则在服务器上加上host,然后重启sap_SAP刘梦

这几个垂直类小众导航网站,你绝对不会想错过

Rk3308--8 channels changed to dual channels + recording gain
![姿态估计之2D人体姿态估计 - Human Pose Regression with Residual Log-likelihood Estimation(RLE)[仅链接]](/img/c7/9c25da07236ef0bd241b6023e82306.gif)
姿态估计之2D人体姿态估计 - Human Pose Regression with Residual Log-likelihood Estimation(RLE)[仅链接]

2290. Minimum Obstacle Removal to Reach Corner

Rk3308-- firmware compilation

智能电网终极Buff | 广和通模组贯穿“发、输、变、配、用”全环节
随机推荐
CAP 6.1 版本发布通告
Tensorflow actual combat Google deep learning framework second edition learning summary tensorflow introduction
「技术干货」工业触摸屏之驱动开发及异常分析(连载)
【MySQL基础】
Beginner pytorch step pit
[section 7 function]
22. Generate Parentheses
Aperçu en direct | déconstruire OLAP! Le nouveau paradigme de l'architecture d'analyse multidimensionnelle est entièrement ouvert! Apache Doris va apporter cinq gros problèmes!
姿态估计之2D人体姿态估计 - Human Pose Regression with Residual Log-likelihood Estimation(RLE)[仅链接]
【历史上的今天】6 月 10 日:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生
Apple style Chinese: it seems to express the meaning clearly, and the Apple style propaganda copy that you don't fully understand
【無標題】
[sans titre]
Baidu open source ice-ba installation and operation summary
Anba cv2fs/cv22fs obtained ASIL C chip function safety certification, surpassing the level of similar chips in the market
运行mapreduce任务缺失setJarByClass()报错找不到类
Comply with medical reform and actively layout -- insight into the development of high-value medical consumables under the background of centralized purchase 2022
Aggregate sum of MapReduce cases
云图说|每个成功的业务系统都离不开APIG的保驾护航
统一认证中心 Oauth2 认证坑