Sringcloud 2.0 learning - 7-spring cloud integrates zookeeper as registry + ribbon load balancing

Use Zookeeper as a registry

1. Start the zookeeper server

Zookeeper is a distributed coordination tool, which can realize service registration and discovery, registration center, message middleware, distributed configuration center, etc.
                     ! Otherwise, the service cannot be registered in the registry.
   choose the version of zookeeper 3.5.3.beta here, because in the version of spring cloud finchley.release, the version of zookeeper that the spring cloud starter zookeeper discovery client depends on is 3.5.3.beta. Start zookeeper in win10 system:

2. Create service provider: project of member service

  • pom dependence:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lchtest</groupId>
  <artifactId>springcloud2.0-zookeeper-member</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>  
	</parent>
	<!-- Management dependence -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot integration Web assembly -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot integration zookeeper Client -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
		</dependency>

	</dependencies>
</project>
  • yaml profile
# Member service port number
server:
  port: 8002
#Member service alias
spring:
  application:
    name: zk-member
  # Address of member service registered to zookeeper Registration Center
  cloud:
    zookeeper:
      connect-string: localhost:2181
  • Startup class
    When the registry is zookeeper or consumer, use the @ EnableDiscoveryClient annotation to register the service with the registry
package com.lchtest.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
 * Service provider
 */
@SpringBootApplication
// If the registry is zookeeper or consumer, use the @ EnableDiscoveryClient annotation to register the service with the registry
// Note that in * * win10 system, the version numbers of zookeeper client and server should be the same, otherwise the service cannot be registered in the registry**
// Check the version number of the jar package of zookeeper that maven relies on, and then download the corresponding zookeeper server
@EnableDiscoveryClient 
public class ZKMemberApp {
	public static void main(String[] args) {
       SpringApplication.run(ZKMemberApp.class, args);
	}
}
  • MemberController
package com.lchtest.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ZKMemberController {
	
	@Value("${server.port}")
	private String port;

	@RequestMapping("/getmember")
	public String getmember() {
		return "this is member service.port=" + port;
	}
}

Start the member service and view the service information registered in the registry:

The node information is as follows:

{"name":"zk-member","id":"8c2a6380-aabd-43a0-be6c-fbbaf19cc02b","address":"windows10.microdone.cn",
"port":8002,"sslPort":null,"payload":
{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"zk-member","metadata":{}},
"registrationTimeUTC":1581469919682,"serviceType":"DYNAMIC","uriSpec":
{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}

There is a problem here. The value of "address" is "windows10.microdone.cn". This problem usually occurs only under windows. Add it to yaml configuration file
spring.cloud.zookeeper.discovery.instance-host configuration, the value is the local ip address, refer to http://blog.csdn.net/qq_18398239/article/details/100699959

3. Create a service consumer Order service project

  • pom dependency join zookeeper client: spring cloud starter zookeeper discovery
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lchtest</groupId>
  <artifactId>springcloud2.0-zookeeper-order</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>  
	</parent>
	<!-- Management dependence -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot integration Web assembly -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot integration zookeeper Client -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
		</dependency>

	</dependencies>
</project>
  • configuration file
# Service port number
server:
  port: 8003
# Server Alias
spring:
  application:
    name: zk-order
  # Address of the service registered to zookeeper registry
  cloud:
    zookeeper:
      connect-string: localhost:2181
  • OrderController
  • Add @ LoadBalanced annotation to RestTemplate to enable load balancing
package com.lchtest.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@SpringBootApplication
@EnableDiscoveryClient  // Register the service to the zookeeper registry with the @ EnableDiscoveryClient annotation
public class ZKOrderController {
	
	public static void main(String[] args) {
		SpringApplication.run(ZKOrderController.class, args);
	}
	
	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping("/orderTomember")
	public String orderTomember() {
		String memberUrl = "http://zk-member/getmember";
		return restTemplate.getForObject(memberUrl, String.class);
	}
}

5. Service call test

  • Start the zookeeper server, two instances of member service, and order service. The list of services in the registry is as follows:

    The browser has visited http://127.0.0.1:8003/orderTomember for many times and has realized load balancing
  • Delete the ZK member service and ZK order service in the temporary node of the registry through ZooInspector, and visit http://127.0.0.1:8003/orderTomember after 30 seconds. You can find that the access is not available
  • Restart the member and order services, make them re register to zookeeper, then manually stop the member service, and then check the zookeeper Registration Center: you can see that there is no temporary node information of the member service in zookeeper:

Code address of this example
Spring cloud 2.0-zookeeper-member project
springcloud2.0-zookeeper-order project

Problems encountered

If the zooInspector fails to connect to the zookeeper after the startup of zookeeper, an error will be reported: unable to connect to zookeeper. Delete and restart the data generated by the previous operation of zookeeper

Published 7 original articles, praised 0 and visited 31
Private letter follow

Tags: Zookeeper Spring Maven Apache

Posted on Sun, 16 Feb 2020 23:47:45 -0800 by Gokul