我是靠谱客的博主 老实发带,最近开发中收集的这篇文章主要介绍springCloud zookeeper整合,Java Zookeeper微服务注册中心整合,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

springCloud zookeeper整合,Java Zookeeper微服务注册中心整合

springCloud整合zookeeper代替Eureka

================================

©Copyright 蕃薯耀 2021-03-08

https://www.cnblogs.com/fanshuyao/

Eureka当前已经停止更新,部分项目只存在维护的状态,以后应该就是停止维护,所以有些项目使用了zookeeper作为服务注册中心。这只是其中的一种方式,还可以使用Consul或者Nacos代替

zookeeper将服务SPRINGCLOUD-ZK-CLIENT-SERVICE以节点的形式注册进来,zookeeper的服务节点是临时的并非持久的。

eureka和zookeeper上注册的服务,服务关闭了不会立马消失,会存在一个心跳。

但是eureka注册的服务停止,并不会删除,会在注册中心保留。而zookeeper上的服务停止,zookeeper会删除服务的节点。

服务再次启动注册到zookeeper中,会生成一个新的服务节点。从某种意义上可以说eureka是持久的,zookeeper是临时的

一、Zookeeper的安装和使用
详情见:

https://www.cnblogs.com/fanshuyao/p/13845076.html

二、springCloud zookeeper整合-服务提供者
1、pom.xml引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    <version>2.2.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.4.4</version>
</dependency>

2、application.properties文件配置

server.port=8611

#zookeeper的服务区分大小写
spring.application.name=SPRINGCLOUD-ZK-CLIENT-SERVICE
#zookeeper连接地址
spring.cloud.zookeeper.connect-string=192.168.170.14:2181
#服务实例ID
spring.cloud.zookeeper.discovery.instance-id=${spring.application.name}

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringCloudZkClient8611Application {

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

}

4、服务提供类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ZkClientController {

    @Value("${server.port}")
    private String serverPort;
    
    @RequestMapping("/zk")
    public Result zk() {
        return Result.ok("端口:" + serverPort);
    }
}


5、返回结果类

import java.util.Date;

import cn.hutool.core.date.DateUtil;

public class Result {
    
    public static final String SUCCESS = "操作成功。";
    public static final String FAILURE = "操作失败!";
    
    private boolean result;
    private String timestamp;
    private String msg;
    private Object datas;
    
    private Result() {}
    
    public static Result ok() {
        return Result.ok(SUCCESS, null);
    }
    
    
    public static Result ok(Object datas) {
        return Result.ok(SUCCESS, datas);
    }
    
    
    public static Result okMsg(String msg) {
        return Result.ok(msg, null);
    }
    
    
    public static Result ok(String msg, Object datas) {
        Result result = new Result();
        result.setResult(true);
        result.setTimestamp(DateUtil.formatDateTime(new Date()));
        
        if(msg == null || msg == "" || msg.trim() == "") {
            result.setMsg(SUCCESS);
        }else {
            result.setMsg(msg);
        }
        
        result.setDatas(datas);
        return result;
    }
    
    public static Result fail() {
        return Result.fail(FAILURE, null);
    }
    
    
    public static Result failMsg(String msg) {
        return Result.fail(msg, null);
    }
    
    
    public static Result fail(Object datas) {
        return Result.fail(FAILURE, datas);
    }
    
    public static Result fail(String msg, Object datas) {
        Result result = new Result();
        result.setResult(false);
        result.setTimestamp(DateUtil.formatDateTime(new Date()));
        
        if(msg == null || msg == "" || msg.trim() == "") {
            result.setMsg(FAILURE);
        }else {
            result.setMsg(msg);
        }
        result.setDatas(datas);
        return result;
    }
    
    public boolean isResult() {
        return result;
    }
    public void setResult(boolean result) {
        this.result = result;
    }
    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getDatas() {
        return datas;
    }
    public void setDatas(Object datas) {
        this.datas = datas;
    }
    

    public static void main(String[] args) {
        
    }

}

三、springCloud zookeeper整合-服务消费者
1、pom.xml引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    <version>2.2.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.4.4</version>
</dependency>


2、application.properties配置

server.port=8621

spring.application.name=springCloud-zk-web
spring.cloud.zookeeper.connect-string=192.168.170.14:2181
spring.cloud.zookeeper.discovery.instance-id=${spring.application.name}

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringCloudZkWeb8621Application {

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

}

4、RestTemplate配置类

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestConfig {

    //@LoadBalanced实现负载均衡
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

5、Controller请求类

注意:zookeeper的服务名是区分大小写的

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ZkController {

    //zookeeper的服务区分大小写:No instances available for SPRINGCLOUD-ZK-CLIENT-SERVICE
    private String serviceUrl = "http://SPRINGCLOUD-ZK-CLIENT-SERVICE";
    
    @Autowired
    private RestTemplate restTemplate;
    
    @RequestMapping(value="/getzk", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result getzk() {
        Result result = restTemplate.getForObject(serviceUrl + "/zk", Result.class);
        return result;
    }
}


6、结果返回类(省略,见上面服务提供者结果返回类)

(时间宝贵,分享不易,捐赠回馈,^_^)

================================

©Copyright 蕃薯耀 2021-03-08

https://www.cnblogs.com/fanshuyao/

最后

以上就是老实发带为你收集整理的springCloud zookeeper整合,Java Zookeeper微服务注册中心整合的全部内容,希望文章能够帮你解决springCloud zookeeper整合,Java Zookeeper微服务注册中心整合所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部