之前几篇博客我们已经介绍过使用Eureka和Consul作为注册中心的实现及代码示例,这边博客我们学习了解一下Spring Cloud给我们提供的用Zookeeper作为注册中心的实现机制。

 Zookeeper

        Zookeeper是一个高性能,分布式的,开源分布式应用协调服务。它提供了简单原始的功能,分布式应用可以基于它实现更高级的服务,比如同步,配置管理,集群管理,名空间。它被设计为易于编程,使用文件系统目录树作为数据模型。

 1、Zookeeper安装启动

    官网下载:http://zookeeper.apache.org/releases.html#download

    解压运行:zkServer.cmd


服务提供者

    服务提供者将自己的地址信息暴露到Zookeeper中即可,简单来说就是一些IP、端口、实例名等基本信息

    1、pom.mxl配置

<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">
	<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.5.10.RELEASE</version>
	</parent>
	
	<dependencyManagement>
	    <dependencies>
	        <dependency>
	            <groupId>org.springframework.cloud</groupId>
	            <artifactId>spring-cloud-dependencies</artifactId>
	            <version>Edgware.SR3</version>
	            <type>pom</type>
	            <scope>import</scope>
	        </dependency>
	    </dependencies>
	</dependencyManagement>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.tianjunwei</groupId>
	<artifactId>TJWspringCloud-zookeeper</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>TJWspringCloud-zookeeper</name>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
	    </dependency>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		<dependency>  
            <groupId>ch.qos.logback</groupId>  
            <artifactId>logback-core</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>ch.qos.logback</groupId>  
            <artifactId>logback-classic</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>ch.qos.logback</groupId>  
            <artifactId>logback-access</artifactId>  
        </dependency>  
	
	</dependencies>
	<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>src/test/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.*</include>
				</includes>
			</resource>
		</resources>
		<plugins>  
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			
	        <plugin>  
	            <groupId>org.apache.maven.plugins</groupId>  
	            <artifactId>maven-compiler-plugin</artifactId>  
	            <version>2.0.2</version>  
	            <configuration>  
	                <source>1.8</source>  
	                <target>1.8</target>  
	            </configuration>  
	        </plugin>  
	    </plugins>  
	</build>
</project>

2、在application.properties中添加服务提供者自身及Zookeeper的相关配置

server.port=8822
spring.application.name=spring-cloud-zookeeper
spring.cloud.zookeeper.connectString=localhost:2181
spring.cloud.zookeeper.discovery.instanceHost=localhost
spring.cloud.zookeeper.discovery.instancePort=${server.port}

3、服务提供者相关代码:

    服务提供者暴露服务名为spring-cloud-zookeeper的服务。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@RestController
public class ZookeeperMain {

  @RequestMapping("/hi")
  public String home() {
    return "Hello zookeeper";
  }

  public static void main(String[] args) {
    SpringApplication.run(ZookeeperMain.class, args);
  }

}

服务消费者

    服务消费者同样注册到Zookeeper中,从zk中获取到所需要的服务信息。

 1、pom.xml中配置

<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">
	<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.5.10.RELEASE</version>
	</parent>
	
	<dependencyManagement>
	    <dependencies>
	        <dependency>
	            <groupId>org.springframework.cloud</groupId>
	            <artifactId>spring-cloud-dependencies</artifactId>
	            <version>Edgware.SR3</version>
	            <type>pom</type>
	            <scope>import</scope>
	        </dependency>
	    </dependencies>
	</dependencyManagement>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.tianjunwei</groupId>
	<artifactId>TJWspringCloud-zookeeper-client</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>TJWspringCloud-zookeeper</name>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	
	</properties>
	
	<dependencies>
		<dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
	    </dependency>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		<dependency>  
            <groupId>ch.qos.logback</groupId>  
            <artifactId>logback-core</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>ch.qos.logback</groupId>  
            <artifactId>logback-classic</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>ch.qos.logback</groupId>  
            <artifactId>logback-access</artifactId>  
        </dependency>  
	
	</dependencies>
	<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>src/test/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.*</include>
				</includes>
			</resource>
		</resources>
		<plugins>  
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			
	        <plugin>  
	            <groupId>org.apache.maven.plugins</groupId>  
	            <artifactId>maven-compiler-plugin</artifactId>  
	            <version>2.0.2</version>  
	            <configuration>  
	                <source>1.8</source>  
	                <target>1.8</target>  
	            </configuration>  
	        </plugin>  
	    </plugins>  
	</build>
</project>

2、application.properties中添加自身及zk相关的配置

server.port=8833
spring.application.name=spring-cloud-zookeeper-client
spring.cloud.zookeeper.connectString=localhost:2181
spring.cloud.zookeeper.discovery.instanceHost=localhost
spring.cloud.zookeeper.discovery.instancePort=${server.port}

3、服务调用

    在服务消费者通过服务名称来找到对应调用服务提供者的信息。

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

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@RestController
public class ZookeeperClientMain {

	@Autowired
	RestTemplate restTemplate;
	
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

	@RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String add() {
        return restTemplate.getForEntity("http://spring-cloud-zookeeper/hi", String.class).getBody();
    }

  public static void main(String[] args) {
    SpringApplication.run(ZookeeperClientMain.class, args);
  }

}

服务调用结果:



总结:总体来说使用zk作为注册中心与其他注册中心(consul和eureka)类似,对于开发人员来说几乎可以不用在意注册中心到底是哪种类型,因为在服务进行调用的代码都是一致的。

    
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐