当前位置: 首页 > news >正文

Spring Cloud使用ZooKeeper作为注册中心的示例

简单的Spring Cloud应用程序使用ZooKeeper作为注册中心的示例:
1.新建模块:
在这里插入图片描述
2.勾选依赖:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3.在pom.xml文件中做出部分修改及添加Spring Cloud Zookeeper 依赖版本

在这里插入图片描述
完整pom文件

<?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 https://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>3.2.2</version><relativePath/> <!-- lookup parent from repository --></parent><!-- Generated by https://start.springboot.io --><!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn --><groupId>com.example</groupId><artifactId>ZooKeeper</artifactId><version>0.0.1-SNAPSHOT</version><name>ZooKeeper</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><zookeeper.version>2.2.5.RELEASE</zookeeper.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zookeeper-config</artifactId><version>${zookeeper.version}</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zookeeper-discovery</artifactId><version>${zookeeper.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>io.projectreactor</groupId><artifactId>reactor-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

4.配置ZooKeeper连接:在application.yml文件中配置ZooKeeper的连接字符串。

spring:application:name: productcloud:zookeeper:connect-string: localhost:2181
server:port: 8101

5.新增Controller:

package com.example.zookeeperserver;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping
public class ProductController {@GetMapping("product")public String product(){return "this is product";}
}
package com.example.zookeeper;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class ServiceController {@Autowiredprivate DiscoveryClient discoveryClient;/*** @Description:通过注入DiscoveryClient实例来获取服务实例的信息* @Author: * @Date: 2024/2/7 15:46* @return: java.util.List<java.lang.String>*     访问地址:http://localhost:8101/services**/@GetMapping("/services")public List<String> getServices(){//返回所有注册到服务发现中心的服务名称return discoveryClient.getServices();}/*** @Description:* @Author: * @Date: 2024/2/7 15:52* @param serviceName:* @return: java.util.List<org.springframework.cloud.client.ServiceInstance>*     访问地址:http://localhost:8101/serviceInstances/product**/@GetMapping("/serviceInstances/{serviceName}")public List<ServiceInstance> getServiceInstance(@PathVariable String serviceName){//getServiceInstances方法接受一个服务名称作为参数,返回该服务的所有实例。//每个ServiceInstance对象包含了服务实例的详细信息,如主机名、端口号等return discoveryClient.getInstances(serviceName);}}

6.启动本地的服务端:
在这里插入图片描述
7.启动服务注册和发现:在主应用类中,通过@EnableDiscoveryClient注解启用服务注册和发现
在这里插入图片描述

8.访问:localhost:8101/product
在这里插入图片描述
完整项目代码

相关文章:

  • Nginx实战:1-安装搭建
  • 鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之Rating组件
  • 前端入门:(五)JavaScript 续
  • C语言的字符函数的使用与模拟实现
  • 表单标记(html)
  • 设计模式2-对象池模式
  • [职场] 如何通过运营面试_1 #笔记#媒体#经验分享
  • Stable Diffusion 模型下载:majicMIX reverie 麦橘梦幻
  • 【Linux】信号概念与信号产生
  • WebSocketServer+redis实时更新页面数据
  • 梯度提升树系列7——深入理解GBDT的参数调优
  • GPT-4登场:多模态能力革新,提升ChatGPT与必应体验,开放API助力游戏革新
  • c++ 子进程交互 逻辑
  • Zabbix6.x配置中文界面 解决乱码问题
  • Redis中内存淘汰算法实现
  • 08.Android之View事件问题
  • Material Design
  • Node 版本管理
  • Protobuf3语言指南
  • Solarized Scheme
  • SpiderData 2019年2月16日 DApp数据排行榜
  • Vue UI框架库开发介绍
  • Webpack 4 学习01(基础配置)
  • 分享一份非常强势的Android面试题
  • 区块链共识机制优缺点对比都是什么
  • 三分钟教你同步 Visual Studio Code 设置
  • 新版博客前端前瞻
  •  一套莫尔斯电报听写、翻译系统
  • 优秀架构师必须掌握的架构思维
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • #微信小程序:微信小程序常见的配置传旨
  • (C#)一个最简单的链表类
  • (C语言)逆序输出字符串
  • (二)PySpark3:SparkSQL编程
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • *2 echo、printf、mkdir命令的应用
  • .net core 6 redis操作类
  • .NET 服务 ServiceController
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .NET/C# 中设置当发生某个特定异常时进入断点(不借助 Visual Studio 的纯代码实现)
  • .NET学习全景图
  • .pyc文件还原.py文件_Python什么情况下会生成pyc文件?
  • .sh
  • @DependsOn:解析 Spring 中的依赖关系之艺术
  • @ModelAttribute注解使用
  • [2013AAA]On a fractional nonlinear hyperbolic equation arising from relative theory
  • [ActionScript][AS3]小小笔记
  • [Angular 基础] - 数据绑定(databinding)
  • [C#小技巧]如何捕捉上升沿和下降沿
  • [CISCN 2019华东南]Web11
  • [Docker]十一.Docker Swarm集群raft算法,Docker Swarm Web管理工具