Dubbo and Zookeeper integration
A, distributed theory
1.1. What is distributed system?
In the book “Distributed System Principle and Paradigm”, there is the following definition: “Distributed system is a collection of several independent computers, which are like a single related system for users”;
A distributed system is a system composed of a group of computer nodes that communicate through a network and coordinate their work to accomplish a common task. The emergence of distributed systems is to use cheap, ordinary machines to complete a single computer can not complete the computation, storage tasks. The aim is to use more machines to process more data.
A distributed system is a software system built on a network.
First of all to be clear, only when the processing capacity of a single node cannot meet the increasing computation, storage, task, and the ascension of the hardware (plus memory, and disk, using better CPU) high to do more harm than good, the application cannot be further optimized, we only need to consider a distributed system. Because, the distributed system to solve the problem itself is the same as the stand-alone system, and due to the distributed system multi-node, network communication topology, will introduce a lot of problems that the stand-alone system does not have, in order to solve these problems will introduce more mechanisms, protocols, bring more problems.
1.2. Dubbo Documentation
With the development of the Internet, the scale of website applications keeps expanding, and the conventional vertical application architecture has been unable to cope with it. The distributed service architecture and mobile computing architecture are imperative, and a governance system is urgently needed to ensure the orderly evolution of the architecture.
There’s a picture of this on Dubbo’s official documentation
1.3. Single application Architecture
When site traffic is low, only one application is needed to deploy all functions together to reduce deployment nodes and costs. At this point, data access frameworks (ORM) that simplify the work of adding, deleting, modifying and reviewing are key.
Suitable for small websites, small management systems, all functions are deployed into one function, easy to use.
Disadvantages:
1. Performance scaling is difficult
2. Collaborative development
3. It is not conducive to upgrade and maintenance
1.4. Vertical application architecture
When the volume of traffic gradually increases, the acceleration caused by the increase of a single application machine is getting smaller and smaller. The application is divided into several unrelated applications to improve efficiency. At this point, a Web framework (MVC) for accelerating front-end page development is key.
By dividing services, each module can be independently deployed, reducing the difficulty of maintenance and deployment, making it easier for teams to perform their own duties and expanding performance more convenient and targeted.
Disadvantages: Common modules can not be reused, wasteful development
1.5. Distributed Service Architecture
With the increasing number of vertical applications, the interaction between applications is inevitable. Core businesses are extracted as independent services, gradually forming a stable service center, so that front-end applications can respond to changing market demands more quickly. At this point, the ** distributed Services framework (RPC)** for improving business reuse and integration is key.
1.6. Mobile computing Architecture
As the number of services increases, problems such as capacity evaluation and waste of small service resources gradually emerge. In this case, a scheduling center needs to be added to manage cluster capacity in real time based on access pressure to improve cluster utilization. In this case, forA resource scheduling and governance center that improves machine utilization(SOA)[Service Oriented Architecture] is key.
What is the RPC?
Remote Procedure Call (RPC) refers to Remote Procedure Call, which is an inter-process communication method. It is a technical idea, rather than a specification. It allows a program to call a procedure or function in another address space, usually on another machine on a shared network, without the programmer explicitly coding the details of this remote call. That is, programmers write essentially the same calling code whether they call local or remote functions.
That is to say, two servers A and B have one application deployed on server A. If they want to call the function/method provided by the application on server B, they cannot call it directly because they do not have the same memory space. Therefore, they need to express the call semantics and transfer the call data over the network. Why use RPC? It is the requirement that cannot be completed in a process, or even in a computer through local call, such as communication between different systems, or even communication between different organizations. Due to the horizontal expansion of computing capacity, applications need to be deployed in a cluster composed of multiple machines. RPC calls remote functions as if they were local;
Recommended reading articles:www.jianshu.com/p/2accc2840…
Basic RPC Principles
Step analysis:
Two core modules of RPC:Communication, serialization.
Second, the Dubbo
2.1, concepts,
2.1.1 What is Dubbo?
Apache Dubbo | ‘dAbau | is a high-performance, lightweight open source Java RPC framework, it provides three main core competencies: an interface of remote method invocation (rmi), intelligent fault tolerance and load balancing, and automatic registration and discovery of services.
Dubbo website: dubbo.apache.org/zh/
1. Understand Dubbo’s features
2. Check official documentation
2.1.2 Basic Concepts of Dubbo Service provider(Provider) : Exposes the service Provider. At startup, the service Provider registers its services with the registry.
Service Consumer: the service Consumer who invokes the remote service. When the service Consumer starts up, it subscribes to the registry for the service it needs. The service Consumer selects one provider from the provider address list and invokes it based on the soft load balancing algorithm.
Registry: The Registry returns a list of service provider addresses to the consumer. If there are changes, the Registry pushes the change data to the consumer based on the long connection
Monitor: Service consumers and providers accumulate call times and call time in memory and regularly send statistics to the monitoring center once a minute
The l service container is responsible for starting, loading, and running the service provider.
At startup, service providers register their services with the registry.
At startup, service consumers subscribe to the registry for the services they need.
L The registry returns a list of service provider addresses to the consumer, and if there are changes, the registry pushes the change data to the consumer based on the long connection.
L Service consumer, from the provider address list, based on the soft load balancing algorithm, chooses one provider to call, if the call fails, then chooses another call.
L Service consumers and providers accumulate the number of calls and call time in memory, and regularly send statistical data to the monitoring center once a minute.
2.1 Dubbo environment construction
The Zookeeper registry is recommended by clicking on dubbo’s official documentation
What is ZooKeeper? Check out the official documentation
2.2. Install ZooKeeper in Windows
Zookeeper official website: zookeeper.apache.org/
1, download the zookeeper: zookeeper.apache.org/releases.ht… Let’s download the latest version of 3.6.2! Unpack the zookeeper
CMD, the first run will be an error, zoo. CFG configuration file is not available.
Possible problems: flash back!
Solution: Edit the end of zkserver. CMD to add pause. This operation error will not exit, will prompt error information, easy to find the cause. Modify the zoo. CFG configuration file
CFG file in the conf folder and rename it zoo_sample. CFG.
A few important points to note:
DataDir =./ Directory for temporary data store (writable relative path)
ClientPort =2181 Port number of ZooKeeper
After the modification, start ZooKeeper again4, use zkcli.cmd test
Ls / : Lists all nodes saved under the ZooKeeper root
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
Copy the code
Create — e /qingfeng123: Create a qingfeng node with a value of 123Get /qingfeng123: gets the value of /qingfeng123Let’s look at the nodes again
Install dubo-admin on Windows
Dubbo itself is not a service. Zookeeper is a JAR package that allows your Java application to connect to ZooKeeper and use ZooKeeper to consume and provide services.
However, in order to help users better manage and monitor the many dubbo services, the official provides a visual monitoring program dubbo-admin, but this monitoring program will not affect the use of even if it is not installed.
Dubo-admin: download dubo-admin
Address: github.com/apache/dubb…
2. Decompress and go to the directory
Modify dubo-admin \ SRC \main\resources \application.properties to specify the ZooKeeper address
server.port=7001
spring.velocity.cache=false
spring.velocity.charset=UTF-8
spring.velocity.layout-url=/templates/default.vm
spring.messages.fallback-to-system-locale=falseBasename =i18n/message spring.root.password=root spring.guest. Password =guest # registry address dubbo.registry.address=zookeeper:/ / 127.0.0.1:2181
Copy the code
3. Package dubo-admin in the project directory
mvn clean package -Dmaven.test.skip=true
Copy the code
The first packing process is a bit slow and requires patience! Until success!
Dubbo-admin-0.0.1 – snapshot.jar under dubbo-admin\target
java -jar dubbo-admin-0.01.-SNAPSHOT.jar
Copy the code
Note: The ZooKeeper service must be turned on!
We need to enter the login account and password, we are the default root-root;
After the login is successful, view the pageInstallation complete!
SpringBoot + Dubbo + zookeeper
Third, frame building
3.1 Starting ZooKeeper!
3.2 IDEA Creates an empty project
dubbo+zookeeper
3.3 Creating a Module
Create a module that implements the service provider: provider-Server and select Web dependency
3.4 Write a ticket service
After the project is created, we write a service, such as selling tickets;
Write the interface
package com.cy.provider.service;
public interface TicketService {
public String getTicket(a);
}
Copy the code
Writing an implementation class
package com.cy.provider.service;
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket(a) {
return "Light as the wind."; }}Copy the code
3.5 Creating a Module
Implement service consumer: consumer-server, select Web dependency
3.6 The project is created
Let’s write a service, like a user’s service;
Write the service
package com.cy.consumer.service;
public class UserService {
// We need to get the registry service
}
Copy the code
Demand: Now our users want to buy tickets for the service, how to do this?