I. Distributed
What is a distributed system?
A collection of computers that appear to the user as a single related system. A system in which a group of computers communicate over a network in order to accomplish a common task.
Note: Distribution is considered only when a single computer cannot complete the task.
Remote Procedure Call (RPC)
Remote procedure call, a method of communication between processes, a technical idea, not a specification, that allows a program to call a procedure or function in another space. The two core modules of RPC are communication and serialization serialization: data is required for conversion
What is Dubbo?
Microservices Open Source Framework, a high-performance, lightweight, open source Java RPC framework, provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance, load balancing, and automatic service registration and discovery
What is the ZooKeeper service
ZooKeeper is a distributed, open source distributed application coordination service. It is an open source implementation of Google’s Chubby and an important component of Hadoop and Hbase. It provides consistency services for distributed applications, including configuration and maintenance, domain name service, distributed synchronization, and group service.
Second, the Dubbo
1. What is dubo-admin
Dubbo-admin is a monitoring management background to check registered services and which services have been consumed. Zookeeper is a monitoring background.
2. Dubbo installation steps:
1>, GitHub official website download dubo-admin download address
2>, decompress the file
3> Run idea import service is divided into two parts: front end and back end, respectively start back end: Dubbo-admin-server is a springboot project that provides the main method in the DubboAdminApplication class. You can right-click to run the front end directly: NPM install NPM run dev
Third, they are
1. Zookeeper download
Zookeeper download address
Choose the version that is suitable for stability. I chose 3.4.14 and selected the file download ending with.tar.gz. Decompress the downloaded package to the bin, conf, and lib directories.
Run scripts are stored in the bin directory. Configuration files are stored in the conf directory. The lib directory contains the third-party libraries needed to run.
2. Single-node Configuration of Zookeeper
2.1 Configuration File In the conf directory, create a zoo. CFG file with the following contents:
# Basic unit of time (ms) for interaction between server and client tickTime=2000# Number of clients allowed by ZooKeeper initLimit=10# Time interval between request and reply between server and client syncLimit=5# Basic unit of time used in ZooKeeper: tickTime=2000DataDir =/tmp/The zookeeper/data # log directory can also be any directory. If this parameter is not set, the same setting as #dataDir is used. DataLogDir =/tmp/Zookeeper /log # t Specifies the port number used to monitor client connections2181
Copy the code
2.2 Running the ZooKeeper Server To Switch to the Directory containing the decompressed zooKeeper-3.4.14 package, and run the following command:
Sh start Stops zookeeper. /bin/zkserver. sh stop
If the following information is displayed, the ZooKeeper Server process is running successfully in the background.
zhangfandeMacBook-Pro:zookeeper-3.414. zhangfan$ ./bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/Zhangfan/Documents/test/dubbo zookeeper -3.414./bin/.. /conf/zoo.cfg Starting zookeeper ... STARTED zhangfandeMacBook-Pro:zookeeper-3.414. zhangfan$
Copy the code
Four, SpringBoot+Dubbo+ ZooKeeper combat
(1) Preparation
Create an empty idea project. 3. Create the provider-server module. 4
(2) the provider – server
1. Register the service provider in the registry, integrate Dubbo and ZooKeeper, guide the package
<! -- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<! -- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
Copy the code
New version of the pit: ZooKeeper and its dependencies, resolve log conflicts, also need to remove log dependencies<! -- Introducing 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>
Copy the code
2. Configure Dubbo properties in the SpringBoot configuration file
Dubo.application. name=provider-server dubo.registry =zookeeper:/ / 127.0.0.1:2181# scanning under the specified package service dubbo. Scan. Base - packages = com. Kuang. The provider. The serviceCopy the code
3. Implement the service annotations in the service class and publish the service! Pay attention to the packet guide problem
import org.apache.dubbo.config.annotation.Service; // The emphasis is on dubbo to package
import org.springframework.stereotype.Component;
@Service // Publish the service
@Component // Put it in the container
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket(a) {
return "Crazy God says Java."; }}Copy the code
(3) consumer – server
- Import dependencies, just like provider-server
- Configure the parameter application.properties
# current application name dubbo. Application. Name = consumer - # server registry address dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181Copy the code
- The normal step is to package the interface of the service provider and import it with poM file. Here, we use a simple way to directly bring the interface of the service, and ensure that the path is correct, that is, the same as the service provider.
- Improve consumer – the provider
package com.kuang.consumer.service;
import com.kuang.provider.service.TicketService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service // Inject into the container
public class UserService {
@Reference // Remotely references the specified service, which matches the full class name to see who registered the full class name with the registry
TicketService ticketService;
public void bugTicket(a){
String ticket = ticketService.getTicket();
System.out.println("Buy it at the registry"+ticket); }}Copy the code
5. Write test classes
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerServerApplicationTests {
@Autowired
UserService userService;
@Test
public void contextLoads(a) { userService.bugTicket(); }}Copy the code
(4) Test
-
Open the zookeeper
-
Open dubo-admin for monitoring
-
Service starter
-
Consumer consumption test