Spring Cloud之DiscoveryClient使用

SpringCloud实战【九】: SpringCloud服务间调用 1 服务间调用 微服务的特点是服务数量特别多,服务和服务之间也需要有交互,这就涉及到服务间的调用,即服务与服务之间如何通信。提到服务与服务之间的通信,最通用的莫过于HttpClient,在其它的通信架构中基本都使用HttpClient来作为底层的通讯模型。在SpringCloud中依然可以使用HttpClient进行服务与服务调用,只不过如果采用HttpClient调用的话,会有一些弊端,例如: 如果同一个服务有多个负载的话,采用HttpClient调用时,没有办法处理负载均衡的问题。还有... 阅读详情

Spring Cloud之DiscoveryClient使用

 

 主要修改zk order的代码:

package com.toov5.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@SpringBootApplication
@EnableDiscoveryClient //如果服务使用consul或者zk使用这个注解 向注册中心注册服务
public class zkOrderApiControllerRibbon {
    @Value("${server.port}")
    private String serverPort;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    
    @RequestMapping("/orderToMember")
  public String orderToMember() {
        String url ="http://zk-member/getMember"; 
        return restTemplate.getForObject(url, String.class);
  }
    
    @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;
    }
    
    public static void main(String[] args) {
        SpringApplication.run(zkOrderApiControllerRibbon.class, args);
    }
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

 结果:

 

 

SpringCloud笔记(三)使用DiscoveryClient手动实现客户端负载均衡 1、什么是客户端负载均衡(Ribbon)? Ribbon是从eureka注册中心服务器端上获取服务注册信息列表,缓存到本地,然后在本地实现轮训负载均衡策略。既在客户端实现负载均衡。 2、什么是服务端负载均衡(Nginx)?  Nginx是客户端所有请求统一交给Nginx,由Nginx进行实现负载均衡请求转发,属于服务器端负载均衡。 即请求由Nginx服务器端进行转发。 3、两者的应用场景?... 阅读详情

相关推荐

SpringCloud工作笔记033---找不到import org.springframework.cloud.client.discovery.EnableDiscoveryClient;无法导入

 JAVA技术交流QQ群:170933152 POM.XML中需要加入,这样就好了: &lt;dependencyManagement&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt; ...

添柴程序猿的专栏 1万+

已解决:org.springframework.cloud.client.discovery.EnableDiscoveryClient 不存在

PS:为什么清除缓存有用,我认为是这种的,创建项目索引,大型项目在创建索引的过程中可能会出现依赖没有正常加载的问题!并且,IDEA 的缓存和索引主要是用来加快文件查询的速度,但是如果你的缓存和索引在新加入依赖后没有更新或者这部分内容没有正常加载,这时候你启动项目就会出现第二种原因,依赖是正常的,但是就是报错!针对我遇到的导致问题的第二种原因,经过测试有两种解决问题的方式!将项目交给maven管理,但是有可能会导致其他的问题!错误,这个错误蛮奇怪的,有必要记录一下。在IDEA中,清空缓存。

qq_43408367的博客 7809

Spring Cloud Gateway:DiscoveryClient Route Definition Locator

使用DiscoveryClient Route Definition Locator,您可以配置网关根据注册在与DiscoveryClient兼容的服务注册中心上的服务创建路由。 要启用此功能,请设置spring.cloud.gateway.discovery.locator.enabled=true,并确保在类路径上存在并启用了DiscoveryClient实现(例如Netflix Eureka、Consul或Zookeeper)。

专注于深入研究多种编程语言,以实战为导向,逐步拓展开发技能,提升工程化编码和思维能力,展现无敌技术实力。 719

使用Spring Cloud Consul实现服务的注册和发现

首先安装consul环境,参照之前的文章:http://blog.csdn.net/mn960mn/article/details/51753893 项目规划,2个服务端,1个客户端 首先来看服务端, 一:服务端1: 项目依赖 [java] view plain copy

mark's technic world 1076

Spring Cloud 学习(七)——使用DiscoveryClient模拟负载均衡操作

一、测试前的准备 1、创建eureka注册中心 2、创建端口为8001的member子模块和端口为8000的member子模块,分别注册到指定的注册中心上 二、创建服务的消费者 1、引入依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sprin...

专注写bug 684

数通基础(基本网络通信原理、IP地址如何配置、DHCP、DNS域名系统、路由技术基础、静态路由 、TCP和UDP、vlan虚拟局域网、trunk、三层交换)

我们知道了1、电脑开机,通过dhcp获取ip地址2、电脑输入域名,www.baidu.com电脑把www.baidu.com发到自己的dns服务器3、dns服务器把域名解析成对应的ip地址4、电脑向百度服务器发送一个请求。

qq_53235556的博客 6481

spring-cloud-commons之DiscoveryClient

DiscoveryClient接口 public interface DiscoveryClient extends Ordered { int DEFAULT_ORDER = 0; String description(); List<ServiceInstance> getInstances(String serviceId); List<String> getServices(); default int getOrder() {

ystyaoshengting的专栏 4816

从报错到优化:一次Gauss JDBC参数限制排查实录(32767参数上限避坑指南)

本文详细记录了排查GaussDB JDBC驱动因绑定参数数量超过32767上限而抛出`PSQLException`(报错信息为`Tried to send an out-of-range integer as a 2-byte value`)的全过程。文章深入分析了该限制的底层协议原理,并提供了应用层分批查询、使用临时表以及优化业务逻辑等多种解决方案,旨在帮助开发者规避此参数上限陷阱,构建更稳健的数据访问层。

rgv23456789的博客 218

Spring Cloud Gateway 中的 DiscoveryClient 路由定义解析器详解

在现代微服务架构中,服务实例的动态注册和发现是核心特性。传统的手动配置路由规则在面对频繁的服务扩缩容时显得力不从心。Spring Cloud Gateway 的 `DiscoveryClientRouteDefinitionLocator` 正是为了解决这一痛点而生,它能够自动从服务注册中心发现服务并动态创建路由规则,实现真正的服务自动化路由。 通过本文,你将深入理解: - DiscoveryC...

gitblog_00206的博客 417

SAP UI5 主题设置全解析:从启动参数到运行时切换、跨域白名单与事件监听

SAP UI5主题体系深度解析:从配置到切换的完整指南 摘要:SAP UI5主题机制远不止CSS换肤,其工程化实践涵盖启动配置、运行时API切换、跨库路径映射及安全管控等关键环节。通过bootstrap标签或URL参数设置初始主题,运行时可用Theming.setTheme无刷新切换。自定义主题需配置theme-roots路径,跨域加载需声明sap-allowed-theme-origins白名单。主题切换完成后会触发applied事件,适合执行视觉同步操作。该体系支持多品牌适配、集中式主题管理,在Fior

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 327

【人脸识别/轻量级模型】MobileFaceNet:适用于人脸识别的轻量级网络模型(文末附pytorch代码)

MobileFaceNet:用于人脸识别的轻量级网络模型

Roaddd的博客 6988

成功解决:com.netflix.discovery.DiscoveryClient没有找到错误(使用spring cloud框架)解决方案,建议看看

“Field discoveryClient in (报错的类名) required a bean of type ‘com.netflix.discovery.DiscoveryClient’ that could not be found.” 原因:Discovery的包有两个,导错了。 解决:删除之前错误的包,导入这个org.springframework.cloud.client.disc...

Jav 1万+

【Claude】API Error 通用错误:错误码含义与标准排查流程 bug报错已解决

Claude API 各类错误码速查与标准排查流程。

博客 8245

运行spring cloud出现Completed shut down of DiscoveryClient

错误信息: 2019-04-23 20:59:18.837 WARN 9044 --- [ main] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with message: java.net.ConnectException: Connection refused 2019-04-...

qq_40551367的博客 3332

SKUA-GOCAD入门教程-第七节 点的创建与编辑3

本文介绍了四种从井数据创建点集的方法

江洋大葱的博客 963

Android SystemUI——初始化流程(三)

1、Android 系统在启动之后,首先会Zygote进程,Zygote进程又会进一步启动 SystemServer进程,它会启动各种系统所需要的服务,在监听AMS启动完毕之后,还会启动SystemUIService。2、在启动SystemUIService服务之前,系统首先会创建SystemUI对应的进程,获取SystemUIAppComponentFactory实例对象,调用instantiateApplicationCompat创建SystemUIApplication实例对象并设置回调方法。

小旭的专栏 1158

【源码】Spring Cloud —— Eureka Client 2 DiscoveryClient

【源码】Spring Cloud —— Eureka Client 2 DiscoveryClient前言版本DiscoveryClient注册表信息拉取服务注册定时任务定时拉取注册列表信息心跳续约按需注册服务下线总结参考 前言 上一章节对 Spring Cloud Netflix Eureka Client 提供的核心组件类做了大致的了解,本章节结合部分源码解读核心类 DiscoveryClient 该系列章节需要相关的内容做铺垫,传送门: 关于延时、周期任务调度 —— ScheduledExecuto

小水牛的博客 321

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

启动spring cloud项目报错Completed shut down of DiscoveryClient

在项目中添加如下依赖解决: &lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt; &lt;/depe...

qq_34745957的博客 1099

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Spring Cloud Eureka(七):DiscoveryClient 源码分析

1、本节概要 上一节文章主要介绍了Eureka Client 的服务注册的流程,没有对服务治理进行介绍,本文目的就是从源码角度来学习服务实例的治理机制,主要包括以下内容: 服务注册(register) 服务续约(renew) 服务下线(unregister) 服务拉取(fetchRegistry) 缓存刷新(refreshRegistry) eureka client 与 eureka ser...

码客 1892

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

如何定制自己的 DiscoveryClient

已经接触过的 Spring Cloud 类 DiscoveryClient EurekaDiscoveryClient ZookeeperDiscoveryClient ConsulDiscoveryClient NacosDiscoveryClient LoadBalancerClient RibbonLoadBalancerClient 实现自己的 DiscoveryClient 需要...

weixin_43790623的博客 974

Spring Cloud Eureka - Client启动后直接运行结束:Completed shut down of DiscoveryClient

报错如下图: Eureka Client一般要导入: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> 导入这个依赖后,我就启

kaven 2669

Spring Cloud Commons完全指南:一站式掌握微服务核心抽象与通用组件

Spring Cloud Commons是Spring Cloud生态系统中的核心组件,提供了一套微服务开发的通用抽象和基础类库,帮助开发者构建灵活、可移植的分布式系统。本文将全面介绍Spring Cloud Commons的核心功能、使用方法和最佳实践,让你轻松掌握微服务开发的关键技术。 ## 什么是Spring Cloud Commons? Spring Cloud Commons是Spr

gitblog_00966的博客 449

DiscoveryClient 和 NamingService

SpringCloud的DiscoveryClient与Nacos的NamingService是两种服务发现接口。DiscoveryClient是SpringCloud的通用抽象接口,支持多平台集成(如Nacos/Eureka),适用于SpringBoot应用,提供统一的服务查询方法。NamingService是NacosSDK专用接口,提供更细粒度的服务注册/发现功能,支持实例属性配置等底层操作。开发中可根据需求选择:SpringCloud项目优先使用DiscoveryClient,需要深度定制Nacos

chenxuefeng_accp的专栏 420

DiscoveryClient导包出错

3 DiscoveryClient导包出错 错误导入了 com.netflix.discovery 应导入 org.springframework.cloud.client.discovery.DiscoveryClient;

张佳佳 240
上一篇: Spring Cloud2.0之整合Consul作为注册中心
下一篇: Spring Cloud之Ribbon与Nginx区别
Art&Code
博客等级 码龄7年 77粉丝 554原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值