Thrift builds distributed microservices (1) and thrift builds distributed microservices

Source: Internet
Author: User

Thrift builds distributed microservices (1) and thrift builds distributed microservices
I. What is Thrift?

For a basic introduction to Thrift, see the Thrift introduction in Zhang Shanyou's article.

 

Ii. Why use microservices?

 

In the rapid development of the company, with the growth of business, more and more subsystems. Different systems have different degrees of overlap in some logic scenarios. For efficient development, the implementation code of these logic must be reused. The common practice is to directly reference the related DLL. Different subsystems are developed by different teams. directly referencing DLL may lead to duplicate namespaces, as well as confusion caused by unclear Use Cases of methods. Another solution is to deploy a unified interface to encapsulate access to the underlying database and some common logic. The implementation of this solution either takes SOA into account or microservice into consideration. Considering the cost, microservices should be easier to implement.

 

Iii. Design Ideas

 

Thrift uses Socket for communication and uses Thrift to build microservices. It should be able to establish TCP connections with multiple IP addresses or ports. How can we manage these connections in a unified manner and make it easy to use them? Configure the connection using XML, and use the connection pool to Manage TCP Socket connections.

The data structures naturally supported by Thrift may be too adequate for. net. How can we use them to communicate with complicated data structures? Consider that all communication data is transmitted using a Json string.

How can I notify the client when an exception occurs on the server?

Authentication issues.

How can I monitor the running status of the connection pool?

 

Article 1 connection Configuration

To establish a TCP Socket connection, Thrift must first have an IP address and a port. Because the connection pool is used to manage connections, you must set the maximum number of activated connections, the maximum number of idle connections, and the minimum number of idle connections. When the number of activated connections reaches the maximum number of connections, the request for obtaining the Socket connection is in the waiting state. In this case, you need to set a maximum waiting time. When the waiting time-out period expires, the corresponding action should be taken, whether to log or notify the connection pool manager to modify the connection pool configuration, which is implemented by the developer.

1 [Serializable] 2 public class ServiceConfig 3 {4 [XmlAttribute] 5 public string Name {get; set;} 6 7 [XmlAttribute] 8 public string IP {get; set ;} 9 10 [XmlAttribute] 11 public int Port {get; set;} 12 13 [XmlAttribute] 14 public int MaxActive {get; set ;} 15 16 [XmlAttribute] 17 public int MaxIdle {get; set;} 18 19 [XmlAttribute] 20 public int MinIdle {get; set ;} 21 22 /// <summary> 23 // connection pool waiting for connection time 24 // unit: millisecond 25 // timeout log or notification who needs to change connection pool configuration 26 // </summary> 27 [XmlElement, defaultValue (1000)] 28 public int WaitingTimeout {get; set;} 29}

 

Obviously, the service of a node cannot be called a microservice. Therefore, to manage these connection nodes, a configuration is required:

1 [Serializable] 2 public class ThriftConfig 3 {4 /// <summary> 5 /// monitor type 6 /// used to monitor the connection pool running status 7 /// inherited from the ITriftFactoryMonitor class 8 /// </summary> 9 [XmlElement] 10 public string MonitorType {get; set;} 11 12 [XmlArrayItem ("Service")] 13 public List <ServiceConfig> ServiceArray {get; set;} 14}

 

How can I read these configurations so that they are used by the connection pool?

1 public static List <ServiceConfig> GetServiceConfigs () 2 {3 List <ServiceConfig> services = new List <ServiceConfig> (ThriftConfig. serviceArray. count); 4 foreach (var SC in ThriftConfig. serviceArray) 5 {6 if (! Services. exists (service => service. name. toUpper () = SC. name. toUpper () 7 {8 // IP verification 9 if (is00004address (SC. IP) 10 {11 services. add (SC); 12} 13 else14 {15 throw new thrifle texception (string. format ("The Service Config Named \" {0} \ ", Which's IP ({1}) Is Not Valid! ", SC. name, SC. IP); 16} 17} 18 else19 {20 throw new thrifle texception (string. format ("There Is A Service Config Named \" {0} \ ", Please Check Service Config File! ", SC. Name); 21} 22} 23 if (services. Count = 0) 24 {25 throw new thritexception (" There Is No Specific Service! "); 26} 27 return services; 28} 29 30 private static ThriftConfig LoadThriftConfig () 31 {32 string path = Path. combine (AppDomain. currentDomain. setupInformation. applicationBase, ThriftConfigFilePath); 33 if (File. exists (path) 34 {35 return SerializeHelper. loadFromXml <ThriftConfig> (path); 36} 37 throw new thritexception (string. format ("Not Found Thrift Config File \" {0} \ "", path); 38}

 

The preparation is complete. The next article explains how to use these configurations to create a connection pool.

 

Thrift microservice code download Thrift. Utility

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.