首先我们在名为MSG的服务中定义一个简单的方法

@RestController
public class ServerController { @GetMapping("/msg")
public String msg() {
return "this is a msg";
}
}

我们需要在另一个服务中调用这个服务的方法,除了使用httpclient之外,我们还能用RestTemplate(RestTemplate是Spring提供的用于访问Rest服务的客户端)

第一种方式,这种方式只要指定url和返回类型即可调用,但是url是写死的,非常不方便。

@GetMapping("/getMsg")
public String getMsg(){
RestTemplate restTemplate=new RestTemplate();
String response=restTemplate.getForObject("http://localhost:8080/msg",String.class);
return response;
}

当我们的服务有多个URL的时候,第一种方式肯定是不行的,所以第二种方式是使用Spring Cloud提供的LoadBalancerClient

@GetMapping("/getMsg")
public String getMsg(){
RestTemplate restTemplate=new RestTemplate();
ServiceInstance serviceInstance=loadBalancerClient.choose("MSG");
String url=String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort());
String response=restTemplate.getForObject(url,String.class);
return response;
}

第三种方法我们可以使用注解的方式,先把RestTemplate配置成bean,@LoadBalanced是开启负载均衡的注解

@Component
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

然后把这个RestTemplate注入进来调用

@Autowired
private RestTemplate restTemplate; @GetMapping("/getMsg")
public String getMsg(){
String response=restTemplate.getForObject("http://MSG/msg",String.class);
return response;
}

使用RestTemplate进行服务调用的几种方式的更多相关文章

  1. springcloud 服务调用的两种方式

    spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign.Ribbon是一个基于HTTP和TCP客户端的负载均衡器,其实feign也使用了rib ...

  2. spring boot2X整合Consul一使用RestTemplate实现服务调用

    Consul可以用于实现分布式系统的服务发现与配置 服务调用有两种方式: A.使用RestTemplate 进行服务调用 负载均衡——通过Ribbon注解RestTemplate B.使用Feign ...

  3. Linux中设置服务自启动的三种方式

    有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务 主要用三种方式进行这一操作: ln -s                       在/etc/rc.d/rc*.d目录中建立/e ...

  4. [转]Linux中设置服务自启动的三种方式

    from:http://www.cnblogs.com/nerxious/archive/2013/01/18/2866548.html 有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统 ...

  5. dubbo服务运行的三种方式

    dubbo服务运行,也就是让生产服务的进程一直启动.如果生产者进程挂掉,也就不存在生产者,消费者不能进行消费. Dubbo服务运行的三种方式如下:1.使用Servlet容器运行(Tomcat.Jett ...

  6. (转)Linux中设置服务自启动的三种方式

    有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务 主要用三种方式进行这一操作: ln -s                       在/etc/rc.d/rc*.d目录中建立/e ...

  7. 【转发】Linux中设置服务自启动的三种方式

    有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务 主要用三种方式进行这一操作: ln -s                       在/etc/rc.d/rc*.d目录中建立/e ...

  8. DLL调用的两种方式(IDE:VC6.0,C++)

    原文:http://www.cnblogs.com/Pickuper/articles/2050409.html DLL调用有两种方式,一种是静态调用,另外一种是动态调用 (一)静态调用 静态调用是一 ...

  9. Eureka 中服务下线的几种方式

    原文:https://blog.csdn.net/qq_15071263/article/details/85276486#1_6 Eureka 中服务下线的几种方式1.直接停掉服务根据默认的策略,如 ...

随机推荐

  1. Java Redis缓存穿透/缓存雪崩/缓存击穿,Redis分布式锁实现秒杀,限购等

    package com.example.redisdistlock.controller; import com.example.redisdistlock.util.RedisUtil; impor ...

  2. k8s概念入门

    k8s是一个编排容器的工具,其实也是管理应用的全生命周期的一个工具,从创建应用,应用的部署,应用提供服务,扩容缩容应用,应用更新,都非常的方便,而且可以做到故障自愈,例如一个服务器挂了,可以自动将这个 ...

  3. 为什么要监控sql语句,以及如何监控,都有哪几种方式可以监控。

    快速阅读 为什么要监控sql语句,以及如何监控,都有哪几种方式可以监控. 我们知道sql server 中有个工具叫sql profile ,可以实时监控sql server中 执行的sql 语句,以 ...

  4. [转]MyEclipse基础学习:Java EE Learning Center

    我就不翻译了,直接给出Java EE学习中心的原文链接: Java EE Learning Center 另外,给出MyEclipse IDE 环境中Apache Tomcat server服务器正常 ...

  5. 【json/regex】将嵌套对象生成的json文进行内部整形排序后再输出

    下载地址:https://files.cnblogs.com/files/xiandedanteng/jsonformat20191126-2.zip 注意:本文仅为draft1版本,还有待完善. 先 ...

  6. Spring AOP Log

    spring aop action中验证用户登录状态 - CASER_HDMI的博客 - CSDN博客https://blog.csdn.net/CASER_HDMI/article/details/ ...

  7. GitHub上优秀的开源资源

    (1)整理了所有跟编程相关的免费书籍 https://github.com/EbookFoundation/free-programming-books/blob/master/free-progra ...

  8. C++11智能指针之std::unique_ptr

    C++11智能指针之std::unique_ptr   uniqut_ptr是一种对资源具有排他性拥有权的智能指针,即一个对象资源只能同时被一个unique_ptr指向. 一.初始化方式 通过new云 ...

  9. 算法习题---5-1代码对齐(UVa1593)

    一:题目 将不规范的若干行代码进行对齐.对齐按照最长字符串进行操作.见样例输入 (一)样例输入 ␣␣start:␣␣integer;␣␣␣␣//␣begins␣here stop:␣integer;␣ ...

  10. 【FreeMarker】Spring MVC与FreeMarker整合(二)

    前一篇介绍了FreeMarker的基本使用,本例介绍Spring MVC与FreeMarker整合 不熟悉项目搭建,可参考 [FreeMarker]FreeMarker快速入门(一) 整合 1.新建S ...