Article Directory
Dubbo and Zookeeper integration
1. Distributed theory
1. What is a distributed system?
In the book "Principles and Paradigms of Distributed Systems", there is the following definition: "A distributed system is a collection of several independent computers, which are like a single related system to users";
A distributed system is a system composed of a group of computer nodes that communicate through a network and coordinate their work in order to complete a common task. The emergence of distributed systems is to use cheap, ordinary machines to complete computing and storage tasks that a single computer cannot complete. The purpose is to use more machines and process more data.
A distributed system is a software system built on the network.
The first thing to be clear is that only when the processing power of a single node cannot meet the ever-increasing computing and storage tasks, and the increase in hardware (adding memory, adding disks, using better CPUs) is so high that the gains outweigh the losses. We only need to consider distributed systems when we cannot optimize further . Because the problem to be solved by a distributed system is the same as that of a stand-alone system, and due to the topology structure of the distributed system with multiple nodes and communication through the network, it will introduce many problems that the stand-alone system does not have. In order to solve these problems, more will be introduced. The mechanism and agreement of the United States have brought more problems. . .
With the development of the Internet, the scale of website applications continues to expand, and conventional vertical application architectures can no longer cope with it. Distributed service architecture and mobile computing architecture are imperative. A governance system is urgently needed to ensure an orderly evolution of the architecture.
2. Distributed service architecture
When there are more and more vertical applications, interaction between applications is inevitable. The core business is extracted as an independent service, and a stable service center is gradually formed, so that front-end applications can respond more quickly to changing market demands. At this time, the Distributed Service Framework (RPC) for improving business reuse and integration is the key.
3. What is RPC
RPC [Remote Procedure Call] refers to remote procedure call, which is a method of inter-process communication. It is a technical idea, not a specification. It allows the program to call a procedure or function in another address space (usually on another machine sharing the network) without the programmer explicitly coding the details of the remote call. That is, whether the programmer calls a local or remote function, the calling code written is basically the same.
That is to say, two servers A and B. One application is deployed on server A. If you want to call the functions/methods provided by the application on server B, they cannot be called directly because they are not in the same memory space. The semantics and semantics of the calls need to be expressed through the network. Convey the called data. Why use RPC? It is a requirement that cannot be completed in a process or even a computer by means of local calls, such as communication between different systems, or even communication between different organizations, because the computing power needs to be expanded horizontally, and it needs to be composed of multiple machines. Deploy applications on the cluster. RPC is to call remote functions like calling local functions;
Recommended reading article: https://www.jianshu.com/p/2accc2840a1b
RPC basic principle
step analysis:
RPC two core modules: communication, serialization.
Two, test environment construction
1.Dubbo
Apache Dubbo |ˈdʌbəʊ| is a high-performance, lightweight open source Java RPC framework, which provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery.
dubbo official website http://dubbo.apache.org/zh-cn/index.html
服务提供者(Provider)
: The service provider that exposes the service. When the service provider starts, it registers the service it provides with the registry.
服务消费者(Consumer)
: The service consumer who calls the remote service. When the service consumer starts, he subscribes to the registry for the service he needs. The service consumer selects a provider from the provider address list based on the soft load balancing algorithm. If the call fails, choose another call.
注册中心(Registry)
: The registry returns the list of service provider addresses to the consumer. If there is a change, the registry will push the change data to the consumer based on the persistent connection
监控中心(Monitor)
: Service consumers and providers, accumulate the number of calls and call time in the memory, and send statistical data to the monitoring center every minute.
Call relationship description
-
The service container is responsible for starting, loading, and running the service provider.
-
When the service provider starts, it registers the service it provides with the registration center.
-
When a service consumer is started, he subscribes to the registry for the service he needs.
-
The registration center returns the list of service provider addresses to the consumer. If there is a change, the registration center will push the change data to the consumer based on the long connection.
-
Service consumers, from the provider address list, based on the soft load balancing algorithm, select one provider to call, and if the call fails, select another to call.
-
Service consumers and providers accumulate the number of calls and call time in the memory, and send statistical data to the monitoring center every minute.
2. Install dubbo-admin under window
dubbo itself is not a service software. It is actually a jar package that can help your java program connect to zookeeper, and use zookeeper to consume and provide services.
However, in order to allow users to better manage and monitor many dubbo services, the official provides a visual monitoring program dubbo-admin, but this monitoring does not affect the use even if it is not installed.
Download link : https://github.com/apache/dubbo-admin/tree/master
Unzip and enter the directory,
modify dubbo-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=false
spring.messages.basename=i18n/message
spring.root.password=root
spring.guest.password=guest
dubbo.registry.address=zookeeper://127.0.0.1:2181
Package dubbo-admin in the project directory
mvn clean package -Dmaven.test.skip=true
Execute dubbo-admin-0.0.1-SNAPSHOT.jar under dubbo-admin\target
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
Note: execute the zookeeper server before executing the jar package
After the execution, let's visit it http://localhost:7001/
. At this time, we need to enter the login account and password. We are all the default root-root;
After successful login, check the interface
3.Zookeeper
ZooKeeper is a distributed, open source distributed application coordination service , an open source implementation of Google’s Chubby, and an important component of Hadoop and Hbase. It is a software that provides consistent services for distributed applications. The functions provided include: configuration maintenance, domain name services, distributed synchronization, group services, etc.
Download Zookeeper: https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.0/ and
choose to apache-zookeeper-3.7.0-bin.tar.gz
download. The one with x.x.x-bin
the compiled version will report an error.
Run /bin/zkServer.cmd
, the first run will report an error, there is no zoo.cfg
configuration file;
You may encounter a problem: crash!
Solution: Edit the zkServer.cmd
file and add it at the end pause
. In this way, the operation error will not exit, and an error message will be prompted to facilitate finding the cause.
Create a zoo.cfg
configuration file
Copy zoo_sample.cfg under the conf folder and rename it to zoo.cfg.
Pay attention to several important positions:
-
dataDir=./ Temporary data storage directory (writable relative path)
-
clientPort=2181 zookeeper port number
After the modification is complete, start zookeeper again
Use zkCli.cmd
test
-
ls /
: List all nodes saved under the zookeeper root -
create –e /kuangshen 123
: Create a kuangshen node with a value of 123 -
get /kuangshen
: Get the value of the /kuangshen node
Three, SpringBoot integrates Dubbo + zookeeper
Need to create one 服务提供者
and一个服务消费者
1. Guide package
<!--dubbo-->
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!--zookeeper-->
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 引入zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
2. Create an empty project, and then create服务提供者:provider-server
Write interface
package com.kuang.provider.service;
public interface TicketService {
public String getTicket();
}
Write implementation class
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
@Service //将服务发布出去
@Component //放在容器中
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket() {
return "《狂神说Java》";
}
}
Logical understanding: When the application starts, dubbo will scan the services annotated with @component under the specified package and publish it in the specified registry!
Configure dubbo related properties in the springboot configuration file!
#当前应用名字
dubbo.application.name=provider-server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#扫描指定包下服务
dubbo.scan.base-packages=com.kuang.provider.service
3. Create服务消费者:consumer-server
Consumer service
package com.kuang.consumer.service;
import com.kuang.provider.service.TicketService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service //注入到容器中
public class UserService {
@Reference //远程引用指定的服务,他会按照全类名进行匹配,看谁给注册中心注册了这个全类名
TicketService ticketService;
public void bugTicket(){
String ticket = ticketService.getTicket();
System.out.println("在注册中心买到"+ticket);
}
}
Configuration parameter
#当前应用名字
dubbo.application.name=consumer-server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
Originally, the normal procedure is to package the service provider's interface and import it with a pom file. We use a simple method here to directly take the service interface. The path must be correct, that is, the same
test class as the service provider.
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerServerApplicationTests {
@Autowired
UserService userService;
@Test
public void contextLoads() {
userService.bugTicket();
}
}
Test
1. Start the zookeeper server
2. Run the dubbo jar package
3. Run the springboot project (start the server)
4. Consumer consumption test, the result:
Monitoring center:
This is SpingBoot + dubbo + zookeeper
the application to achieve distributed development, in fact, it is a service demolition Points of thought.
Problem: After
starting the springboot project, the zookeeper server directly forcibly closes the connection, which cannot be solved temporarily.