SpringCloud整合Zookeeper注册中心

9 篇文章 0 订阅
2 篇文章 0 订阅

简介

        注册中心可以用Eureka等技术来实现,但是为了更好的扩展下知识层面,所以这次就使用Zookeeper作为注册中心,搭建一下以Zookeeper作为注册中心的微服务项目。

Zookeeper介绍

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

首先在Zookeeper官网下载,地址如下图:

这次我们下载3.5.5的版本,因为我是以springboot2.0以上版本整合的,必须整好版本不然会报错,下载成功解压。

进入conf文件夹中如图:

复制一份conf文件夹下的zoo_sample. cfg文件并重命名为zoo.cfg修改dataDir=(你的zookeeper安装目录)

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
# 你的zookeeper安装目录路径
dataDir=F:\java\apache-zookeeper-3.5.5-bin\data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

进入bin目录下,运行zkServer.cmd启动zookeeper服务。 

启动成功如下图:

在Eclipse中创建两个项目

pom.xml中的内容: 

<?xml version="1.0" encoding="UTF-8"?>
<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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<groupId>com.itmuch.cloud</groupId>
	<artifactId>springcloud-zookeeper</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	
	<properties>
		<java.version>1.8</java.version>
	</properties>
	
  <modules>
  	<module>springcloud-zookeeper-order</module>
  	<module>springcloud-zookeeper-user</module>
  </modules>
  
  <dependencies>
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-feign</artifactId>
      <version>1.4.1.RELEASE</version> 
   </dependency>
  </dependencies>
  
  <dependencyManagement>
  	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.1.4.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<!--Greenwich.RELEASE-->
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-dependencies</artifactId>
		    <version>Greenwich.RELEASE</version>
		    <type>pom</type>
		    <scope>import</scope>
		</dependency>
	</dependencies>
  </dependencyManagement>
  
  <build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
  </build>
</project>

 springcloud-zookeeper-user项目中的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">
  <modelVersion>4.0.0</modelVersion>
  
  <parent>
	 <groupId>com.itmuch.cloud</groupId>
	 <artifactId>springcloud-zookeeper</artifactId>
	 <version>0.0.1-SNAPSHOT</version>
  </parent>

  <artifactId>springcloud-zookeeper-user</artifactId>
  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

 <dependencies>
    <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
	</dependency>		
    <dependency>
	  <groupId>org.springframework.cloud</groupId>
	  <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
	</dependency>

 </dependencies>
</project>

 springcloud-zookeeper-user项目中的application.yml内容:

server:
  port: 9001
spring:
  application: 
    name: zookeeper-user
  cloud: 
    zookeeper: 
      connect-string: localhost:2181
    discovery:
        enabled: true
        register: true

 springcloud-zookeeper-user项目中的启动类内容:

@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperApplication {
	
    public static void main( String[] args ){
    	SpringApplication.run(ZookeeperApplication.class, args);
    }
    
}

 springcloud-zookeeper-order项目中的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">
  <modelVersion>4.0.0</modelVersion>
  
  <parent>
	 <groupId>com.itmuch.cloud</groupId>
	 <artifactId>springcloud-zookeeper</artifactId>
	 <version>0.0.1-SNAPSHOT</version>
  </parent>

  <artifactId>springcloud-zookeeper-order</artifactId>
  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

 <dependencies>
    <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
	</dependency>		
    <dependency>
	  <groupId>org.springframework.cloud</groupId>
	  <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
	</dependency>

 </dependencies>
</project>

 springcloud-zookeeper-order项目中的application.yml内容:

server:
  port: 9002
spring:
  application: 
    name: zookeeper-order
  cloud: 
    zookeeper: 
      connect-string: localhost:2181
    discovery:
        enabled: true
        register: true

 springcloud-zookeeper-order项目中的启动类内容:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ZookeeperApplication {
	
    public static void main( String[] args ){
    	SpringApplication.run(ZookeeperApplication.class, args);
    }
    
}

  springcloud-zookeeper-user项目中的Controller:

@RestController
public class UserController {
	
	@GetMapping("/getUserList")
	public Map<String,Object> getUserList() {
		Map<String,Object> map = new HashMap<String,Object>();
		List<String> userlist = new ArrayList<String>();
		userlist.add("孙林");
		userlist.add("王伟");
		userlist.add("关天棋");
		userlist.add("于晓野");
		userlist.add("王大龙");
		map.put("userlist", userlist);
		return map;
	}
}

 springcloud-zookeeper-order项目中的Controller:

@RestController
public class OrderController {
	
	@Autowired
	private FeginInterfase feginInterfase;
	
	@GetMapping("/getUserList")
	public Map<String,Object> getUserList() {		
		Map<String,Object> map = feginInterfase.getUserList();
		return map;
	}
}

springcloud-zookeeper-order项目中的Fegin接口:

@FeignClient(name="zookeeper-user")
public interface FeginInterfase {

	@RequestMapping(value="/getUserList",method=RequestMethod.GET)
	public Map<String,Object> getUserList();
	
	
}

成功启动2个项目后,打开zookeeper管理工具可以查看到2个节点 

在浏览器中输入请求地址可看到输出结果,至此SpringCloud整合Zookeeper注册中心以成功搭建完成。

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值