首先我们在名为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. SpringBoot整合ElasticSearch:基于Jest技术

    1.给pom.xml添加依赖 <!--SpringBoot默认使用SpringData ElasticSearch模块进行操作 <dependency> <groupId> ...

  2. jackson实现json转换

    第一步.导包.导入jar包或者在maven项目中导入坐标(jackson-annotations:jackson-core:jackson-databind) 第二步.通过无参构造创建核心类Objec ...

  3. mysql group by 报错 ,only_full_group_by 三种解决方案

    报错信息  Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'data ...

  4. mysql 连接服务器报ERROR 1130 ,mysql服务正常运行

    今天办公电脑的mysql罢工了,折腾了半天,记录一下. 错误如下: ERROR 1130 (HY000): Host 'localhost' is not allowed to connect to ...

  5. HTML5+和MUI页面操作

    最近总是碰到针对页面的一些操作,以下是针对webview的一些简单方法以及个人理解.更多详尽的内容请参考标准文档:http://www.html5plus.org/doc/zh_cn/webview. ...

  6. 网关 apache APISIX

    网关 apache - 国内版 Binghttps://cn.bing.com/search?q=%E7%BD%91%E5%85%B3+apache&qs=n&form=QBRE&am ...

  7. Linux ps -ef vs. ps aux(ps -aux)

    ps aux.ps -aux.ps -ef之间的区别 - wynter_的博客 - CSDN博客 https://blog.csdn.net/wynter_/article/details/73825 ...

  8. Leetcode: Stream of Characters

    Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data struc ...

  9. realsense SDK编译 debug

    1>------ 已启动全部重新生成: 项目: ZERO_CHECK, 配置: Debug x64 ------1> Checking Build System1> CMake do ...

  10. 123457123456#0#----com.DoraGame.ErTongFanPai97--前拼后广--记忆翻牌-doraX

    com.DoraGame.ErTongFanPai97--前拼后广--记忆翻牌-doraX