spring cloud微服务之间的调用

SpringCloud中为了解决服务与服务调用的问题,提供了两种方式。RestTemplate和Feign。虽然这两种调用的方式不同,但在底层还是和HttpClient一样,采用http的方式进行调用的。对HttpClient进行的封装。下面我们来详细的介绍一下这两种方式的区别,我们首先看一下RestTemplate的方式。

RestTemplate方式调用

检测注册中心是是否将服务注册到服务中心。

直接上代码:

package com.mt.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

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

@Bean
public RestTemplate initRestTemplate() {
return new RestTemplate();
}
}

RestTemplate调用方式一

package com.mt.feign.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/client")
public class Controller {
@Autowired
private RestTemplate template;
@GetMapping("/get")
public Object get() {
String result = template.getForObject("http://127.0.0.1:8085/server/get", String.class);
return result;
}
}

RestTemplate调用方式二

package com.mt.feign.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/client")
public class Controller {
@Autowired
private RestTemplate template;
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/get")
public Object get() {
ServiceInstance serviceInstance = loadBalancerClient.choose("jilinwula-springcloud-feign-server");
String url = String.format("http://%s:%s/server/get", serviceInstance.getHost(), serviceInstance.getPort());
String result = template.getForObject(url, String.class);
return result;
}
}

在SpringClourd中提供了LoadBalancerClient接口。通过这个接口我们可以通过用户中心的Application的名字来获取该服务的地址和端口。

RestTemplate调用方式三

启动类更改:

package com..feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class MtSpringcloudFeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(MtSpringcloudFeignClientApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate initRestTemplate() {
return new RestTemplate();
}
}

在RestTemplate实例化的地方添加了@LoadBalanced注解,我们使用RestTemplate时就该注解就会自动将调用接口的地址替换成真正的服务地址。下面我们看一下Controller中的改动:

package com.mt.feign.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/client")
public class Controller {
@Autowired
private RestTemplate template;
@GetMapping("/get")
public Object get() {
String url = String.format("http://%s/server/get", "tcmp-measure-service");
String result = template.getForObject(url, String.class);
return result;
}
}

代码和第一次的代码基本一样,唯一的区别就是获取服务地址和端口的地方替换成了注册中心中的Application的名字,并且我们的RestTemplate在使用上和第一次没有任何区别,只是在url中不同。

上述内容就是全部内容,在实际的项目开发中,这两种方式均可实现服务与服务间的调用,并且这两种方式都有弊端,所以并没有特别推荐的方式。

spring cloud微服务之间的调用

转载于:https://www.cnblogs.com/mengtaoadmin/p/11184041.html

Spring Boot和Spring Cloud来实现微服务之间调用 以上示例展示了如何使用Spring Boot和Spring Cloud Feign来实现微服务之间调用。你可以根据需求进一步扩展,比如添加负载均衡、熔断器、配置中心等高级功能。学习教程(传送门)1、掌握JAVA入门到进阶知识(持续写作中……2、学会Oracle数据库用法(创作中……3、手把手教你vbs脚本制作(完善中……4、牛逼哄哄的IDEA编程利器(编写中……5、吐血整理的面试技巧(更新中……往期文章第一章:日常_JAVA_面试题集15(含答案) 阅读详情

相关推荐

Spring Cloud多个微服务之间调用代码实例

主要介绍了Spring Cloud多个微服务之间调用代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

springcloud--微服务服务之间调用

springcloud服务之间调用 http客户端工具 HttpClient OKHttp URLConnection Spring对http的客户端进行封装,提供了工具类叫RestTemplate Spring的RestTemplate Spring提供了一个RestTemplate模板工具类,对基于Http的客户端进行了封装,并且实现了对象与json的序列化和 反序列化,非常方便。RestTemplate并没有限定Http的客户端类型,而是进行了抽象,目前常用的3种都有支持: HttpCl..

别浪 795

别再羡慕专业版!用这个批处理脚本,一键给Win11家庭版装上官方沙盒

本文详细介绍了如何通过批处理脚本在Windows 11家庭版上安装官方Windows沙盒(Windows Sandbox),无需复杂操作即可享受专业版功能。脚本自动激活系统内置组件,提供安全隔离的虚拟化环境,适用于软件测试、安全浏览等场景。

weixin_26757925的博客 463

SpringCloud入门(微服务调用 RestTemplate)——微服务调用的方式 & RestTemplate的使用 & 使用nacos的服务名初步(Ribbon负载均衡)

1.微服务调用的几种方式,异步消息传递,http调用,服务网关调用,服务发现调用nacos; 2.spring提供的restTemplate,发送HTTP请求的客户端工具类; 3.nacos使用服务名报错,需要加Ribbon负载均衡;

Arya的博客,专注后端领域 4903

SpringCloud微服务调用方式(RestTemplate)

微服务是一种架构方式,最终肯定需要技术架构去实施。 微服务的实现方式很多,目前Spring Cloud比较流行。为什么? 后台硬:作为Spring家族的一员,有整个Spring全家桶靠山,背景十分强大。技术强:Spring作为Java领域的前辈,可以说是功力深厚,有强力的技术团队支撑。群众基础好:大多数程序员的成长都伴随着Spring框架,现在有几家公司开发不用SpringSpringCloudSpring的各个框架无缝整合,对大家来说一切都是熟悉的配方,熟悉的味道。

阿水的博客 2019

SpringCloud实战【九】 SpringCloud服务间调用

这也就是我们接下来要介绍的服务间调用的前提条件。上面的代码比较简单,就不详细的介绍了,主要是RestTemplate中提供了getForObject方法(实际上RestTemplate提供了很多种调用的方法,主要分为Get或者Post),可以指定要调用接口的地址,指定返回的值的类型。代码和第一次的代码基本一样,唯一的区别就是获取服务地址和端口的地方替换成了注册中心中的Application的名字,并且我们的RestTemplate在使用上和第一次没有任何区别,只是在url中不同。下面我们看一下返回的结果。

李白 1383

SpringCloud服务间调用

SpringCloud 服务间调用

GS_10408120的博客 3357

spring cloud微服务之间调用Feign

微服务之间调用Feign 1.在调用方添加feign依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>sp...

苏凯的博客 5683

Spring Cloud微服务(7)之Feign服务之间调用

1.简介 微服务架构服务实例众多,服务与服务之间如何调用Spring Cloud提供了解决方案:伪装者 Feign。 Feign 是 Spring Cloud 的一个组件,也是一个WebService客户端,用于服务之间的调动。 如何使用 第一步:服务之间调用 本例需要创建三个工程: eureka-server 注册中心服务(项目创建参照第三节网关) product-service 产品服务...

weixin_43343423的博客 252

spring cloud微服务之间调用以及eureka的自我保护机制

这篇主要讲一下服务和服务之间是怎样调用的 如果想学习Java工程化、高性能及分布式、深入浅出。微服务Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。 我自己搭建了一个客户端微服务: 所以现在有两个微服务,我们所实现的就是微服务1和微服务2之间调用 注册中...

javaQQ561487941的博客 2230

Spring Cloud微服务之间调用Dubbo

是阿里巴巴开源的与的深度集成方案。它将 Dubbo 的高性能 RPC 能力与 Spring Cloud微服务治理、配置管理和服务注册发现功能有机结合,帮助开发者快速构建可扩展、可维护的微服务系统。

Demon_Hao的博客 901

Spring Cloud中如何保证各个微服务之间调用的安全性

一.背景微服务架构下,我们的系统根据业务被拆分成了多个职责单一的微服务。每个服务都有自己的一套API提供给别的服务调用,那么如何保证安全性呢?不是说你想调用就可以调用,一定要有认证机制,是我们内部服务发出的请求,才可以调用我们的接口。需要注意的是我们这边讲的是微服务之间调用的安全认证,不是统一的在API官网认证,需求不一样,API网关处的统一认证是和业务挂钩的,我们这边是为了防止接口被别人随便调用

u010889990的专栏 3万+

Spring Cloud微服务之间服务的调用-Spring Cloud微服务使用 Feign调用另一个服务 及 Feign的使用

Feign简介 微服务架构服务实例众多,服务与服务之间如何调用Spring Cloud提供了解决方案:伪装者 Feign。 Feign 是 Spring Cloud 的一个组件,也是一个WebService客户端,用于服务之间的调动。 详细了解Feign可以查看:https://blog.csdn.net/wo18237095579/article/details/83343915 创建好两个服...

Mou_O的博客 4036

controller中调用2个service符合规范吗_Spring Cloud微服务之间相互调用的常见方式...

服务调用场景:(1)订单微服务调用商品微服务,完成订单创建业务(2)涉及的微服务:mall-goods商品微服务、mall-order订单微服务(3)微服务调用关系(依赖关系)如下图所示:1、使用Feign调用(1)在mall-order订单微服务中,定义一个GoodsFeignpackage cn.org.xcore.mall.order.feign;import org.springfram...

weixin_39567169的博客 768

Spring Cloud微服务之间相互调用、如何让一个微服务调用另外一个微服务

在使用微服务架构中,可能遇到一些业务情况会涉及服务之间相互调用,下面通过一个简单的demo给大家演示下,演示的是oms服务需要调用ump服务。 代码如下: 1、oms服务提供者 主要是这个注解: @EnableFeignClients("com.omsserver.*") 完整代码: package com.omsserver.service; //import org.s...

zjh_746140129的博客 4万+

汽车电子MES选型指南:车规级SN追溯能力才是核心命门

在数字化工厂与智能制造的浪潮下,MES系统已成为制造企业实现生产过程透明化、质量可追溯的关键基础设施。对于汽车电子这类高安全、高复杂度行业而言,MES的价值并不仅限于排产与看板,更体现在车规级追溯能力上。所谓车规追溯,是指从单板SN出发,向上关联物料批次、设备参数、工艺窗口,向下打通测试数据与出货履历的全链路数据模型。相比通用型MES,汽车电子MES必须支持器件级谱系追溯、防错防呆、测试数据闭环等硬性能力,才能应对客户审核与召回风险。在实际选型中,企业需重点考察供应商的设备集成能力、异常处理流程及底层数据架

weixin_34246551的博客 320

Spring Cloud实战 | 第五篇:Spring Cloud整合OpenFeign实现微服务之间调用

如果用的是RestTemplate ,需要在bean上面配置。

养牛娃学编程的博客 261
0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值