-
Notifications
You must be signed in to change notification settings - Fork 994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Task] <Extern Alarm Manage API add Aliyun API> #1320
Comments
接入第三方告警API的代码重构一、现存编码的问题1、Controller层通用性比较差如下图 假设我们需要进行增加一个阿里云的API,我们需要写一个跟这个腾讯云API差不多的方法,调用方法的结构也是大同小异,不同之处只有 2、Service层的功能不明确如下图
二、优化的方案1、关于问题1,提出以下解决方案我们的目标是提高接口的复用性,那么首先第一个步就是,我们需要使用一个接口方法来承接不同第三方API的请求,所以我们需要进行对来源的判断。
综合考虑,暂选择方案二。 2、关于问题2,提出以下解决方案由于该Service功能不明显,所以以下方案提出的基础在于,取消该Service,并考虑将该转换能力放置在何处。
综上考虑,暂使用方案二。 三、优化的具体细节由于接口的占位符的字符串与对应的BO实体有强依赖关系,所以可以使用枚举进行强制绑定,也方便进行类型校验,同时如果用户使用API类型错误的话,还可以给用户发送消息,提示某某API使用错误,正确的API有xxx、xxx,进一步让用户更方便使用。 |
Code reconstruction for accessing third-party alarm API1. Problems with existing coding1. The Controller layer has poor versatilityAs shown below Suppose we need to add an Alibaba Cloud API. We need to write a method similar to the Tencent Cloud API. The structure of the calling method is also similar. The only difference is 2. The function of the Service layer is unclearAs shown below
2. Optimization plan1. Regarding question 1, the following solutions are proposedOur goal is to improve the reusability of the interface, so the first step is that we need to use an interface method to accept requests from different third-party APIs, so we need to judge the source.
After comprehensive consideration, we temporarily choose option two. 2. Regarding question 2, the following solutions are proposedSince the function of this Service is not obvious, the basis for the following proposal is to cancel the Service and consider where to place the conversion capability. The
Based on the above considerations, option 2 is temporarily used. 3. Specific details of optimizationSince the placeholder string of the interface has a strong dependency on the corresponding BO entity, enumeration can be used for forced binding, which is also convenient for type verification. At the same time, if the user uses the wrong API type, it can also be sent to the user. The message indicates that a certain API is used incorrectly. The correct APIs are xxx and xxx, further making it easier for users to use. |
LGTM👍,问题一:方案二+1,问题二:方案二+1
|
LGTM👍, Question 1: Option 2 + 1, Question 2: Option 2 + 1
|
how about this @zqr10159 |
LGTM👍🏻 |
以下注解属于jackson,使用的使用需要新建一个 @JsonProperty("msg_type")
private String msgType; 找了fastjson,也有类似的注解(如下),使用方式依旧是 @JSONField(name = "id")
private String id; 我想使用一下第二种方式,不过项目中是否有要求需要统一使用某种转换方式呢,另外这两种方式都会创建一个新的实体对象,这样直接放在BO里面倒是看起来怪怪的,因为该方法不是静态方法,只是BO的一个能力,所以不太应该出现这种BO里面新建一个BO对象的情况,而是应该填充属性。 解决思路:第一种是通过BeanUtil,可以将生成的新的对象复制到this对象中,第二种是看看有没有别的工具可以进行属性映射填充而不需要生成新对象。 后续我去实际编码测试一下这处的实现细节有没有问题:smile: |
The following annotations belong to jackson. To use them, you need to create a new @JsonProperty("msg_type")
private String msgType; I found fastjson and there are similar annotations (as follows). The usage method is still the static method of @JSONField(name = "id")
private String id; I want to use the second method, but is there a requirement in the project to use a certain conversion method? In addition, these two methods will create a new entity object, so it seems strange to put it directly in BO. Because this method is not a static method, but is just a capability of BO, it is unlikely that a new BO object will be created in BO. Instead, the properties should be filled in. Solution: The first is to copy the generated new object into this object through BeanUtil. The second is to see if there are other tools that can fill in attribute mapping without generating new objects. Later, I will go to the actual coding to test whether there are any problems with the implementation details here: smile: |
项目是统一使用jackson的,不需要new Objectmapper,直接注入即可,有统一配置 |
The project uses jackson uniformly, no new Objectmapper is needed, just inject it directly, with unified configuration |
建议统一使用 jackson, |
It is recommended to use jackson uniformly, |
设计思路是想把转换能力收敛到BO中,所以不好在BO中进行注入:joy: |
The design idea is to converge the conversion capabilities into BO, so it is not easy to inject into BO:joy: |
原来有写了对应的工具类呀,那就使用这个啦 |
It turns out that a corresponding tool class has been written, so use this one. |
优化1.0枚举:原项目没有枚举的包,我这里新建了一个: CloudServiceAlarmInformationEnum: 枚举这里将告警信息实体的Class与对应的云服务名称(URL中占位符)进行绑定,枚举获取是遍历方式的,虽然也可以不写枚举,直接用Map去做类似的操作,但是由于枚举类型不多,也并不耗时,所以使用枚举去获取,更便于代码管理一点 DTO改造:改造思路:由于JSON字符串最终是修改为 还需要处理的地方在于云服务告警实体如何转换为 CloudAlertReportAbstract(抽象父类): 代码解释:
需要实现父类的抽象方法,关于里面的一些可以商讨的点:
控制层的改造:只是使用枚举进行了一下校验,后面进行模板填充了一下,顺便优化了一下日志打印和原先的一个解析失败似乎还会进行一个空告警的小问题 代码: 还有一些商讨的地方:
测试:根据之前的测试类小小改了一下,然后debug了一下,构造出来的信息模板与之前一致 测试代码: 改造后如何接入一款新的云服务告警
然后就可以了 各位看看哪里还需要修改一下呢:grin: |
Optimization 1.0Enumeration:The original project does not have enumerated packages, so I created a new one here: CloudServiceAlarmInformationEnum: Enumeration here binds the Class of the alarm information entity with the corresponding cloud service name (placeholder in the URL). The enumeration is obtained through traversal. Although you can also directly use Map to perform similar operations without writing the enumeration. , but since there are not many enumeration types and it is not time-consuming, using enumeration to obtain it is more convenient for code management. DTO transformation:Transformation idea: Since the JSON string is eventually modified into the entity What still needs to be processed is how to convert the cloud service alarm entity into an CloudAlertReportAbstract (abstract parent class): Code explanation:
It is necessary to implement the abstract method of the parent class. There are some points that can be discussed:
Transformation of control layer:I just used enumeration for verification, and then filled in the template. By the way, I optimized the log printing and the original parsing failure seemed to cause an empty alarm. Code: There are some areas for discussion:
test:I made a slight modification based on the previous test class, and then debugged it. The constructed information template is consistent with the previous one. Test code: How to access a new cloud service alarm after transformation
Then it's ok Please take a look at where you still need to modify it: grin: |
👍👍👍 可以直接提交PR就行 我们在PR那边review |
👍👍👍 You can just submit the PR directly and we will review it on the PR side |
Description
Extern Alarm Manage API add Aliyun API and we will optimize the encoding of this section.
Task List
The text was updated successfully, but these errors were encountered: