Zookeeper
Introduction to the
ZooKeeper is a centralized service used for configuration information maintenance, naming, distributed synchronization, and group services. All of these types of services are used by distributed applications in one form or another. Each time they are implemented, a lot of work is done to fix the inevitable bugs and race conditions. Because of the difficulty of implementing such services, applications often initially economize on them, making them vulnerable to change and difficult to manage. Even when executed correctly, different implementations of these services can cause administrative complexity when deploying the application.
Install Zookeeper in Windows
- Download url: archive.apache.org/dist/zookee…
- We chose version 3.6.3 because the official website says it is the most stable version
3. Double-click the file in the bin directoryzkServer.cmd
, you will find flash back. 4. At this point, you editzkServer.cmd
- After editing the run, you will find another error
6. At this point, go to the conf directory and make a copyzoo_sample.cfg
File, rename tozoo.cfg
, and then run, and may occur an error, the port number is occupied, outrageous.
7. Don’t be afraid. At this point, all we have to do iszoo.cfg
At the end of the fileAdmin. serverPort= No occupied port number
, such asadmin.serverPort=8001
Start again at this time and you’ll be fine.
To test this, start zkcli.cmd in the bin directory
- Ls / : Lists all nodes saved under the ZooKeeper root
- Create — e /example 111: Create a Kuangshen node with the value 123
- Get /example: Gets the value of the /example node
Dubbo
Introduction to the
I found a very detailed article on the Internet
An important figure:
Among them:
Provider: A service Provider that exposes a service. 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
Execution process:
- The 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.
- 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.
- The service consumer, from the provider address list, selects one provider to call based on the soft load balancing algorithm. If the call fails, selects another one to call.
- Service consumers and providers accumulate calls and call times in memory and regularly send statistics to the monitoring center every minute.
Install dubo-admin on Windows
- download
dubbo-admin
:Github.com/apache/dubb… - After downloading, decompress, and pass it in the project directory
mvn clean package -Dmaven.test.skip=true
Packaging dubbo – admin
3. When you’re done packing, open itZookeeper service!Execute under dubo-admin \targetDubbo - admin - 0.0.1 - the SNAPSHOT. The jar
4. Post-execution access:http://localhost:7001/, the user name and password are both defaultroot
SpringBoot + Dubbo + ZooKeeper integration
For example, buying and selling tickets
Frame structures,
- Start the zookeeper!
- IDEA creates an empty project
- Create two modules, Provider for the service provider and Consumer for the service consumer, both of which simply import web dependencies
- After the project is created, import the dependencies in both modules
Perhaps because of the new version of the pit, we need to mark the project’s Java directory as the source root, the resouerce as the resource root, and the spring-boot-starter-parent version will be 2.2.1.release
<! -- 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>
<! -- [new version of pit] ZooKeeper and its dependency package, resolve log conflict, also need to remove log dependency; -->
<! -- 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
- We are in
provider
Module writes a ticket selling service
package com.example.service;
public interface TicketService {
public String getTicket(a);
}
Copy the code
- We are in
provider
The module writes an implementation class for the ticket selling service
package com.example.service;
@DubboService
public class TicketServiceImpl implements TicketService{
@Override
public String getTicket(a) {
return "Here's a ticket."; }}Copy the code
- We are in
consumer
The module writes a user’s service
import org.springframework.stereotype.Service;
@Service// Let Spring host it
public class UserService {
@DubboReference// Reference remote objects
TicketService ticketService;
public void buyTicket(a){
String ticket =ticketService.getTicket();
System.out.println("Buy it at the registry"+ticket); }}Copy the code
- The configuration application. Properpites
Server port = 8001 # the service port dubbo. Application. The name = the provider - # server name dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181 Dubo.scan. Base-packages =com.boot.service # Scan for @dubboService and register it with ZooKeeperCopy the code
- Writing test classes
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerServerApplicationTests {
@Autowired
UserService userService;
@Test
public void contextLoads(a) { userService.bugTicket(); }}Copy the code
- This project, using the latest version of Zookeeper, seems to have a little problem, if you have any questions, please go to watch the video