Because the cause of the outbreak, I here, assignments, also not many, so I will spare a little time, usually at this time I will do my own thing, such as blogs, and then look at the source code, flip CSDN is start a blog, just as I was addicted in the source cannot extricate oneself, director suddenly came to my side, should be know I’m not very busy recently, He said to me in a low voice, “We don’t have too many tasks these days, so we have enough free time. Can you share some technology? On the one hand, we can connect with each other, and on the other hand, we can increase the learning atmosphere among colleagues.
After hearing this, I knew that I might not be able to do my own work, after all, technology sharing can also increase the feelings between colleagues, so I agreed, but I was worried, what should I share? If you’re talking about Java, it’s going to be hard for anyone other than Your Java colleagues to hear, but I don’t know what else to say…
Individual services
I remember that when I was an intern, I used a single service. At that time, the architecture was very simple, and there was no separation of the front and back ends. The whole process was quite simple, even nginx was not used.
The introduction of nginx
Yes, it’s as simple as that, the browser through the interface to access the server, the server through the user’s request database operation, and then corresponding to the browser, which is one of the most simple single service process, then leaders felt port directly to expose out pretty dangerous, we need to do a layer of the agent, let the user directly through the domain name. The process is then designed to look like this:
Introduce redis monomer
After I’m trying to find the cause of programming card is the database is the bottleneck, pressure is too great, bear so big of the request, since the problems found, the solution is very simple, I increase a middleware between the application and the database: redis, use it to reduce the database access, this performance will naturally get promoted.
Introduce mysql read/write separation
Importing a Server Cluster
We had been idle for a week or so, our CTO spoke, and at 3pm today, all the back-end developers met in the conference room, and when I heard that, I knew I had a job to do, was it a new project?
At 3 o ‘clock in the afternoon, we came to the conference room, I saw the technical director has arrived in advance, and write five words on the screen, see these five words, I think, the to or will come, can not hide the past, that is: distributed architecture.
Distributed architecture
Because I know that the number of registered users of our product has reached hundreds of thousands, our current architecture will surely not survive one day, and the product will be reconstructed, and it will be reconstructed in a distributed architecture. Sure enough, the technical director held this meeting today.
Technical director: our product is hot now, whether it’s enrollment or active number are relatively high, in order to let programs can have a better robustness, I hope we can refactor about this project, in a distributed architecture, the meeting today is we together to make a technology selection, are you familiar with distributed?
Colleague A: A distributed system is A software system built on the network. Because of the characteristics of software, distributed systems have a high degree of cohesion and transparency. As a result, the difference between networks and distributed systems is more about high-level software (especially operating systems) than hardware.
Cto: Well, that’s good, but that’s more of an official explanation. Are there any more simple explanations?
Colleague B: Distribution divides a large system into numerous sub-systems, each of which is responsible for certain responsibilities. They are independent of each other, but related to each other.
Technical director: oh, not bad, this colleague can, evening add chicken leg, that someone can cite an example?
Me: Once upon a time, there was a restaurant with only the owner and one employee. This employee was responsible for everything in the restaurant, including but not limited to: Cooks, waiters, cashiers, cleaners and so on, can’t go to accept money when I was in cooking, at the time of cleaning can’t cooking, the staff did a month later, suddenly one day, he fell ill, restaurant business is stagnant, the boss is not thought of a person, that I hire a few more people, a person is responsible for a job, In this way, even if someone asks for leave or leaves his job, it will not have a great impact on my business. For example, the cleaner leaves his job. Although no one cleans it, it does not affect my business.
Cto: This is a good example, you don’t have to work overtime tonight, if you can draw a distributed basic map, I will invite everyone to wank tonight.
Me: I deal with all kinds of disobedience, so I gave the CTO a “lesson”, see the picture:
Why use a distributed architecture
Cto: Yes, that’s a good drawing, but do you know why we’re refactoring a single service into a distributed one? That’s right. Chicken legs.
Colleague: C?
- Spring Cloud focuses on providing coverage of typical use cases and extensibility mechanisms with good out-of-the-box experience.
- Distributed/versioned configuration
- Service registration and discovery
- routing
- Service-to-service invocation
- Load balancing
- The circuit breaker
- Distributed messaging
This is the advantage of distributed, this may seem more abstract, for example, for single service, if I want to update the order of a function, if I need to restart the entire service, this time will cause the entire project is in a state is not available, or when processing orders due to write program code has a problem, lead to a deadlock, This time can also lead to the whole service outage designed, fault-tolerant rate is very poor, but distributed different, as shown in the above, the order service, after-sales service, users are independent service, if you need to update the order service or order a deadlock occurs, the affected orders can only be service, after-sale service and customer service can still work normally, This is one of the biggest advantages of distribution over singleness.
Technical director: Unexpectedly, we have a large number of talents in this team, good, good, it seems that I have no need to go on ah, everyone is quite familiar with distributed ah, the distributed architecture has shortcomings or shortcomings?
Colleague D:
What is a distributed system! And the advantages and disadvantages of distributed system architecture!
Distributed infrastructure components
Technical director: Since everyone is so familiar with distributed, that I will not say more, we will directly say about the selection of distributed components, we have any opinions can be put forward, first of all, who say about distributed components are what? Please begin your performance.
- Spring Cloud Config: A configuration management toolkit that allows you to centrally manage cluster configurations by putting them on a remote server. It currently supports local storage, Git, and Subversion.
- Spring Cloud Bus: An event, message Bus for propagating state changes in clusters (for example, configuration change events), which can be combined with Spring Cloud Config for hot deployment.
- Eureka: Cloud service discovery, a REST-based service for locating services to enable cloud middle-tier service discovery and failover.
- Hystrix: Fuse, a fault tolerant management tool designed to provide greater fault tolerance for delays and failures by controlling the nodes of services and third-party libraries through a circuit breaker mechanism.
- Zuul: Zuul is a framework for providing dynamic routing, monitoring, resilience, security and other edge services on cloud platforms. Zuul is the front door for all requests from the backend of the Web site for devices and Netflix streaming applications.
- Archaius: Configuration management API. It contains a series of configuration management apis that provide dynamic typed properties, thread-safe configuration operations, polling framework, callback mechanism, and more.
- Consul: Encapsulates Consul operations. Consul is a service discovery and configuration tool that seamlessly integrates with Docker containers.
- Spring Cloud for CloudFoundry: Bind services to CloudFoundry, VMware’s open source PaaS Cloud platform, via the Oauth2 protocol.
- SpringCloud Sleuth: Log collection toolkit that encapsulates Dapper and log-based tracing as well as Zipkin and HTrace operations to implement a distributed tracing solution for SpringCloud applications.
- Spring Cloud Data Flow: The big Data manipulation tool, an alternative to Spring XD, is a hybrid computing model that combines the processing of streaming Data with batch Data.
- Spring Cloud Security: The Spring Security-based Security toolkit adds Security controls to your applications.
- Spring Cloud Zookeeper: A tool package that operates Zookeeper and is used for service discovery and configuration management using Zookeeper.
- Spring Cloud Stream: A data Stream package that encapsulates sending and receiving messages with Redis,Rabbit, Kafka, etc.
- Spring Cloud CLI: Based on the Spring Boot CLI, it allows you to quickly build Cloud components from the command line.
- Ribbon: Provides cloud load balancing. Multiple load balancing policies are available for service discovery and circuit breakers.
- Turbine: Turbine is a tool for aggregation servers to send event stream data to monitor Hystrix metrics under the cluster.
- Feign: Feign is a declarative, templated HTTP client.
- Spring Cloud Task: Provides Cloud planning Task management and Task scheduling.
- Spring Cloud Connectors: Make it easy for Cloud applications to connect to the back end on various PaaS platforms, such as databases and message brokering services.
- Spring Cloud Cluster: Provides abstraction and implementation of common state patterns such as Zookeeper, Redis, Hazelcast, Consul, etc.
- Spring Cloud Starters: Spring Boot-style startup project that provides out-of-the-box dependency management for Spring Cloud.
Common components: Spring Cloud Config, Spring Cloud Bus, Hystrix, Zuul, Ribbon, Feign.
Technical Director:
Eureka
Eureka is one of the components under Spring Cloud Netflix, which is mainly responsible for service registration and discovery. What is registration and discovery? One problem with the distribution we just analyzed is that the order service and the user service are separated, so how do they communicate? For example, how to obtain the basic information of users in the order service? If we follow the above architecture diagram, we can directly go to the database to obtain, because although the service is independent, but the database is still shared, so we can directly query the database to get results, what if we split the database? What should we do at this point? Some people think, service invocation, service invocation does not need IP and port, then the question comes, for the order service, how do I know the IP and port of the user service? Write dead in order service? What if the port that the user serves changes? Each service can register its own information in Eureka, such as IP, port, service name and other information. At this time, if the order service wants to obtain the information of user service, it only needs to go to Eureka to obtain it, please see the following figure:
Code implementation: SpringCloud (a) registry Eureka
Spring Cloud Config
Spring Cloud Config provides server and client support for external configuration in distributed systems. Using Config Server, you can manage the external properties of your application in all environments. The concept mappings on the client and server are the same as the Spring Environment and PropertySource abstractions, so they fit well with Spring applications, but can be used with any application running in any language. As the application moves through the deployment process from developer to test and production, you can manage the configuration between these environments and determine that the application has everything it needs to run at migration time. The default implementation of the server storage back uses Git, so it easily supports a tabbed version of the configuration environment, as well as access to a variety of tools for managing content. Alternative implementations can easily be added and plugged in using Spring configuration.
To put it more simply, centralize the management of the configuration files for each service, separating the configuration files from the services. What is the purpose of all this? A simple chestnuts, we are bound to exist in the configuration file of the database connection information, redis connection information, our environment is varied, have the development environment, test environment, pre-release environment, production environment, each environment corresponding connection information is certainly not the same, every time don’t release the configuration file to modify the service? Can I centralize the management of these highly changeable configurations, so that managers of different environments can modify them separately, so that there is no need to make changes in the service? Config has been done.
This is the general structure of config. All configuration files are centralized to config. How can config manage these configuration files? You can store the configuration files of each environment in another location, such as Lgitlab, SVN, local, etc. Config will read the configuration files for management according to the location you set. Then when other services are started, you can directly go to the config configuration center to obtain the corresponding configuration files. So developers only need to focus on the -dev profile, and testers only need to focus on the -test profile, completely decoupled from the service, and you deserve it.
Code implementation: SpringCloud (two) configuration center config
Netflix Zuul (Gateway)
At this point, the technical director suddenly raised a question. He said: Since we split a service into many micro-services, won’t that expose a lot of interfaces to the browser? Does this pose a security risk? Who can say something about this?
Colleague A: We can open the secondary domain name through nginx reverse proxy, and then map the domain name to the micro service.
Cto: This scheme is also possible and unnecessary, but it is not the most perfect one. Is there a better scheme? Although nginx hides the port, if our services require some permission verification, Nginx will not be able to do it for us. At this time, we should not add a set of permission verification logic to each service?
Colleague B: I think we can use gateway, which can do both shunt forwarding and permission control. I think it is a better solution to use Nginx + gateway. The following is the introduction of gateway Zuul.
Routing is an integral part of the microservices architecture. For example, / can map to your Web application, / API/Users to a user service, and/API /shop to a store service. Zuul is Netflix’s JVM-based router and server-side load balancer. Netflix uses Zuul for the following:
- Certification – Insight
- Pressure test
- Canary test
- Dynamic routing
- Service migration
- Load shedding
- security
- Static response processing
- Active/Active traffic management
In fact, we do not use so much in the daily development process, which is basically authentication, dynamic routing, security and so on. I have drawn a diagram of gateway architecture, please see:
Technical director: You are really too good, yes, Nginx can only do reverse proxy for us, can not do permission authentication, gateway can not only do proxy, can also do permission authentication, and even do flow limiting, so we have to do distributed projects, without him can not.
Code implementation: SpringCloud (three) gateway Zuul
Spring Cloud Bus
application.yml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test
driver-class-name: com.mysql.cj.jdbc.Driver
Copy the code
Technical director: For example, you should be familiar with the configuration above. This is the connection information of the database. What if it changes? As we all know, when the service is started, it will go to the config configuration center to pull the configuration information, but after the startup, the configuration file is modified, what should we do? Restart the server?
Colleague C: We can solve this problem with the Spring Cloud Bus, which links lightweight message brokers to nodes of distributed systems. It can then be used to broadcast state changes (for example, configuration changes) or other administrative instructions. The project includes AMQP and Kafka broker implementation. In addition, any Spring Cloud Stream binder found on the classpath can be used as a transport tool.
This requires a bit of mq knowledge, whether rabbitMQ or Kafka. The basic principle of bus is: Configuration file is changed, the config will send a mq, tell service, change the configuration file, and also the change of what information, when the service you just need to according to the real-time information of mq be amended, this is a very simple principle, it probably won’t understand how difficult, draw a diagram to understand it
Code implementation: SpringCloud (four) message Bus
Feign
Cto: Beautiful, and you will be technology is easy, just now we said that the registry can facilitate the communication between services, but how they communicate do you know?
Colleague D: Since the distributed architecture we just talked about is SpringCloud, Feign is recommended
Feign is a declarative Web services client. This makes it easier for Web service clients to write to create and annotate an interface using Feign. It has pluggable annotation support, including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and uses the default HttpMessageConverters used in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load-balancing HTTP client when using Feign.
Cto: Good, but what if there is a problem with the invocation of the service? For example, when the call times out, how do we handle it?
Code implementation: SpringCloud (five) remote call Feign(including source tracking)
Netflix Hystrix (Circuit breaker)
Colleague E: The cloud has also been taken into consideration for us. We only need to introduce fuse breaker.
Hystrix supports the concept of fallback: the default code path to execute when a circuit breaks or an error occurs. To enable fallback for a given @FeignClient, set the fallback property to the name of the class implementing the fallback.
We can modify the call architecture we just described
Here I have deployed a standby server that can be accessed when the order service makes a remote call when the user service is down so that the system does not crash.
Technical Director: Distributed architecture roughly about the same, there are some components here also don’t do is introduced, when using can learn about ourselves, not very hard, we then go ahead, I now there is a demand, change passwords, modify the password need to send a text message authentication code, send text messages to SMS, change the password to the user service, would be a service call at this time, And we know that, send a text message is commonly called third-party interface, that such as ali, now that involved in the call, then there are many uncertain factors, such as network problems, if the user then click send message authentication code and user service invocation message service, but in the short message service execution call ali’s interface has spent a long time, At this time, it will cause the user service to adjust the SMS service timeout, will return to the user failed, but the SMS finally sent out, how to solve this problem?
MQ (Messaging Middleware)
Colleague B: We can achieve this by using message middleware to separate asynchronous feedback to the user from sending SMS. As long as the user clicks send SMS, it will return success directly, and then start sending verification code again within 60 seconds. Even if sending fails, the user can choose to send again.
Distributed transaction
Technical Director: Beautiful, mq can not only decoupling service, it can also be used to cut power, improve the performance of the system is a good choice, since we use the distributed architecture, so one thing is we must pay attention to, that is the transaction problem, if a service changes dependent on another service operation, this time if the careless operation, will lead to dire consequences, For example, there are two services: wallet service (used for top-up and withdrawal) and transaction wallet service (used for transaction). Now I want to transfer 1000 yuan from wallet service to transaction wallet service. How should we ensure the consistency of their data?
Colleague C: I have two solutions. The first is to ensure consistency through MQ, and the other is to ensure consistency through distributed transactions.
Mq ensures consistency
Wallet service: Step 1: Generate an order table that records the status of roll-in and roll-out. Step 2: Send a confirmation message to MQ. Step 3: Enable the local transaction, perform the roll-out operation, and commit the transaction.
Transaction Wallet service: Receives messages from MQ and performs the forward operation (this operation requires the SUPPORT of the ACK acknowledgement mechanism).
The system will always periodically scan the status of the order. If there is no success, compensation mechanism or retry mechanism will be implemented. This is not the only requirement.
Seata distributed Transactions
Seata has three basic components:
- Transaction Coordinator (TC) : Maintains the state of global and branch transactions and drives global commit or rollback.
- Transaction manager TM: Defines the scope of a global transaction: starts a global transaction, commits or rolls back a global transaction.
- Resource Manager (RM) : Manages the resources being processed by branch transactions, talks to TCS to register branch transactions and report the status of branch transactions, and drives commit or rollback of branch transactions.
A typical lifecycle for distributed transactions managed by Seata:
- TM asked TC to start a new global transaction. TC generates xids that represent global transactions.
- Xids propagate through the invocation chain of microservices.
- RM registers the local transaction as a branch of the corresponding global transaction from XID to TC.
- TM requires the TC to commit or roll back the corresponding global transaction for XID.
- TC drives all branch transactions under the global transaction corresponding to the XID to complete the branch commit or rollback.
Complete distributed architecture
conclusion
Comparison between individual services and distributed services
When to use distributed/clustered
- When a single machine is not supported.
- Want better isolation (function versus function).
- When you want a better user experience.
- You want better scalability.
- You want faster response, more throughput.
Use distributed considerations
Although distributed technology is now very mature, but there are various pits, such as: = = how to ensure the consistency of distributed transactions, how to ensure the service call idempotence, how to guarantee the idempotency of news, how to set up the fuse (downgrade), how to guarantee the robustness of service, etc., = = these are always need to pay attention to the problem, only solve these problems, your distributed architecture can be in an impregnable position.
Stop message about the component
The registry Eureka, gateway Zuul, and Gateway Feign have all been suspended. This does not mean they cannot be used, but bugs may not be actively fixed, so we may need to choose another component. Registries can use Consul, Nacos, Zookeeper and Fiegn can be replaced by Gateway and openFeign can be replaced by FiegN, so there is no need to worry about whether Cloud will be cold after hearing the news of component downtime. Rest assured, it will not be cold for the next few years at least.
After the technical director concluded, he left the meeting room, and it was time to go off work. Because of my excellent performance in the meeting, I didn’t have to work overtime tonight, and finally I could make my hair fall a little slower.