SpringCloud学习二:zookeeper注册中心(代替eureka)

一、下载安装zookeeper

1.1、下载

安装教程:https://blog.csdn.net/ring300/article/details/80446918

官方下载地址:https://zookeeper.apache.org/releases.html#download

1.2、推荐下载3.4.12版本

1.3、查看jdk环境变量是否正常(cmd→java -version)

1.4、修改zkEnv.cmd中的路径(这步可以跳过直接进入1.5 || 两部二选一)

1.5、修改路径

1.6、启动zkServer.cmd

Windows下Zookeeper启动zkServer.cmd闪退问题的解决方案

注:1.zkServer.cmd闪退可以在zkServer.cmd里面添加 pause

注:2.zookeeper-3.5.5安装报错:找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain

解决方法:在目录中添加zookeeper-3.4.12.jar包

二、可视化工具(ZooInspector)下载和配置

2.1、下载地址:https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip 

2.2、配置:https://blog.csdn.net/wang1988081309/article/details/90723345

2.3、启动:在可视化工具中cmd  →  java -jar zookeeper-dev-ZooInspector.jar

========== 以上服务端 ==========

========== 以下客户端(项目中) ==========

三、springcloud配置zookeeper

SpringCloud 整合zookeeper搭建服务注册中心

SpringCloud搭建zookeeper jar包冲突

3.1、注意jar包的版本对应

3.2、创建项目,导入jar包

                <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>
			<exclusions>
				<!--先排除自带的zookeeper3.5.3-->
				<exclusion>
					<groupId>org.apache.zookeeper</groupId>
					<artifactId>zookeeper</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!--添加zookeeper3.4.9版本-->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
		</dependency>

3.3、xml文件配置

server.port=8010
spring.application.name=zk-register
spring.cloud.zookeeper.connect-string=127.0.0.1:2181

3.4、启动类

package com.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudZookeepreApplication {

	@Value("${server.port}")
	private String serverPort;

	@GetMapping("/getZookeeper")
	public String getZookeeper(){
		return "zookeeper输出结果: "+serverPort+"  "+new Date().getTime();
	}

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

}

 

四、zookeeper中的DiscoveryClient

4.1、在controller中添加以下代码,

@RequestMapping("/discoveryMember")
    public List<ServiceInstance> discoveryMember() {
        List<ServiceInstance> instances = discoveryClient.getInstances("zk-member");
        for (ServiceInstance serviceInstance : instances) {
            System.out.println("url"+serviceInstance.getUri());
        }
        return instances;
    }

4.2、访问后

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值