Although the system is increasingly complex, and the new distributed architecture design is popular, more and more systems use distributed architectures, especially HTTP interface calls, mobile terminals and PC side pair distributed architecture Brought a lot of promotion. A wide range of service interfaces, there are some common problems outside the business process, face design and solve these problems, greatly improve the availability, scaifery and maintainability of the program.
The following summary is a summary of the distributed design problem in the work of the author, as follows:
1, date format
Time is a factor that is easy to ignore but impact, in ancient, whether there is your own calendar is an important sign to assess whether a ruler has to establish a notice. There are a large number of date data in the application, such as the creation time, update time, business processing time, etc. of the data. Interface interactions about date transmission requires two problems: format and accuracy.
There are two main ways in the format of the date: readable ways and unreadable (not easy to read). For example, for the current time (the author writes this article), you can use 2017-03-01 20:30:30 or 2017/3/1 20:30:30, etc. The format is represented; another mode is a long-type count method based on “1970-1-1 08:00:00”. The readable format is easy to understand when debugging, troubleshooting, but may need to do data parsing during the development process, converted to development language identification types for the date of operation, increassed some development efforts. The long format is efficient in performing efficiency, but it needs to be converted during the development of debugging. Although I agree with the LONG type format, if it is returned in a readable manner, there is no difference in most applications. However, there is a need to a unified manner, and multiple service providers in the system will give the client call to the development and error introduction of the client call to the client.
Common computers or development languages ??about time’s minimum units of microseconds, but most of the business use seconds have been met, so you can define universal units in seconds, if you need to interface, you can customize, you can accurately to microseconds or even lower . If there is no definition of a universal unit, it will cause a difference between the difference in a thousand miles when transmitting in LONG type.
2, decimal transmission
Let’s first look at a simple floating point calculation applet, take Java language as an example:
Doublea 1; doubleb 0.99; system.out.println (a – b);
The result is: 0.010000000000000009
The world’s world is only 0 and 1, the decimal integer in the real realism, the decimal is converted to the secondary system, which will result in the loss of precision.
The format conversion process of floating point is stored inside the computer is as follows:
Most programming languages ??will have higher precision ways to process floating point numbers, to ensure the accuracy of the calculation, such as the BigDecimal of the Java language, we use BigDecimal to test again 1-0.99:
BigDecimal abd1 newbigdecimal (a); BigDecimal Bbd1 newbigDecimal (b); system.out.println (abd1.subtract (bbd1));
The result is:
Say good to solve the problem?
It turns out that the Double type has been defined to initialize A, B. When we use A, B to initialize the BigDecimal object, the accuracy is lost, so although the BigDecimal class with higher precision is used to float, it is still not correct. Difference.
The correct way is:
BigDecimal abd newbigdecimal (“1”); BigDecimal BBD NewbigDecimal (“0.99”); System.out.Println (abd.subtract (bbd));
The result is: 0.01
Therefore, in the data interaction, the floating point number is preferably transmitted in a string, ensuring that the caller can initialize and operate in a manner that does not lose accuracy when performing floating point numbers.
3, the format of the result value
If your service provider will often hear such complaints: the result value returns the format, the property name of the result value is different, the number of attributes is different, the meaning of the value is different, and so on. If there is a general meaning called state, error code, error message prompt, business results return mode, the caller will have to spend more when making a package interface call, error retry, resulting value resolution, etc. Energy, as well as sacrificial program readability, scalability.
It is recommended to use the following format as the return value of the interface: STATUS: Interface call status, the status of the return operation result, such as 200: Success 400: Parameter error 500: server side errors
ErrorCode: The reason for the failure status is encoded, and the client interface returns a failed process judgment or result display.
Message: Message prompt for success or failure, used to show
DATA: Used to indicate the attribute of returning interface business data
Analyze some well-known company’s development platform interface, there is a status of status and errorcode synthesizes an attribute indicating the interface, but the author thinks that the separation represents more friendly, because the status status is limited, such as success, parameter error, failure, etc., is a business independent, But the reason for failure (errorcode) is a business-related, and can be unlimited, and the error codes of multiple servers exist, it is recommended to use two attributes to represent result status and error coding.
For returned business data, it is recommended to use the DATA as the MAP format, put the result value as the key value of the MAP, which is convenient to return multiple business data.
4, power, etc.
Power and the Minor: Refers to a resource with a number of resources to have the same side effects. Power equivalence is a very important concept in distributed system design, network jitter in distributed systems (unpredictable short-term unreachable), business timeout, etc., can cause the caller without receiving the return value of the server or not the expected return. Value (such as: You should receive a JSON string, but return a network exception), in order to ensure the high availability of the system, try the method that is often used, so the server side may receive multiple calls, guaranteed multiple times The call has the same side effects is an important attribute of the interface.
As shown in the above 5 steps, any one of the above five steps will cause the client to not receive the expected result, may perform retry, starting from step 3, the service number of the server may have been executed, if the retry means that the service party will receive Two identical calls. The service party recognizes repetition calls, and when the business logic has been performed, the repetition is not executed again and the correct result is the power.
How to achieve power?
1. The business is naturally equivalent, such as most queries, whenever calling will not change to the server data, therefore natural is power equivalent.
2, logic judgment based on the key field of the income parade, such as the scenario at which points is deducted, and the business specifies that each order can only be deducted once, so the order number can be used as a key field as the key field. Whether the inspection has been deducted, if the order number of the current deduction points has been executed, the result is directly returned, otherwise the integration deduction is executed and returned.
3, there are some cases where the call does not have a key field, such as the user who purchases the item, whether it is the product ID, the number of goods, the price, and the member ID can be used as a key field to check if it is executed. Some people say that can be used Whether it is exactly the same as judgment conditions, this is the basis for judgment, but in theory we cannot fully conclude that the user’s active behavior is still a systematic retry. For this scenario, introduce a business-independent key field can solve the problem, whether naming this field is Requestid, Ticketid or other, as required: a, this field is generated by the client, B, the only one (At least one time range or spatial range that may have retry) is unique), the server side can be based on the basis of whether or not it is duplicated.
How to determine if there is a key field, there is also the data that is previously used as a key field as a key field, so all the interfaces are added to all the interfaces when designing development (Suppose we use the requestID this name), use the requestID As a power equalization, it is possible to improve the availability of the interface and the reuse of the retestable verification.
5, interface security
As an interface provider, sometimes some scenes: Error income, resulting in an exception, error prompt, but cannot determine where the data is generated; interface upgrade does not know which calorificers need to notify, because the caller can pass the documentation Access; some of the callbacks require special business processing, but there is no key field to distinguish it with other caller; data statistics lack key field judgment source, etc.
Therefore, the participating parameters contain accurate representation of the caller field, and can identify whether it is a caller identity, and can improve the security, scalability of the system.
1. Assign the identifier AppCode, each call passed the field as a basis for judgment: Use simple, disadvantages: But AppCode can be reused, resulting in accurate judgment.
2, allocate the identifier Appcode, and assign a secret key, use symmetrical encryption to enter the signature, advantage: easy to use, compared to highly encrypted efficiency, shortcomings: Secretary requires at least two or more parties while saving, security and The accuracy is reduced.
3, assign identifier Appcode, and assign asymmetric encrypted key pairs, signing into a way for entering the parametric encryption. Advantages: high security and accuracy. Disadvantages: low efficiency. In contrast the advantages and disadvantages of the above common way, it is recommended to use asymmetric encryption when the performance is not sensitive (asymmetric and symmetric encryption, the efficiency difference is not very efficient after the signature of Hash after the introduction of Hash), because In a multi-interactive scenario, an asymmetric encryption is higher than the encrypted signature.
Symmetrical encryption interacts in many-multi-interaction scenarios:
The above figure shows: The public password will cause the password to spread, and the non-public password causes the caller to save multiple sets of passwords.
Using asymmetric encrypted multiple interactions:
The following figure shows: The CS secret key is saved in the caller and the server, ensuring security and accuracy.
6, data dictionary
Distributed architecture makes different modules in the system to develop maintenance in different teams, so attribute meanings and naming consistency requirements have risen sharply, and countless parameter name translation is a tombstone named inconsistency.
Naming does not consistent mainly contains 2 aspects:
1. The meaning is inconsistent, and a system that has been involved in the development, which produces at least two differences. a, indicating that the user purchases when the goods purchased, B. After the transaction is completed, the abstract naming of the service is used, and the transaction may modify the same service, and the conversion of this data is flying over the system.
2, naming is inconsistent, because various development languages ??naturally support alphabet name naming, so common class names, method names, variables, etc. are named in English, while the programmer’s English level is uneven, so the same meaning naming Targe, such as points, Point, and score, and named Integral.
If there is no good communication and negotiation, it will cause various conversion of the caller, increasing a large number of useless attribute conversion code, and is easy to introduce an error. Therefore, a data datagical management (Wiki, document, git, application, etc.) that meets team conditions can provide development efficiency and availability.
The above is the experience of the author in the development of the project. If you can bring a little help when you design a distributed system, you are deeply pleased. Of course, distributed design, not only can be discussed, not only, such as transactions, timeout processing, retry mechanisms, etc.
If you have any mistakes, please don’t know.