spring cloud微服务之间的调用

时间:2022-07-24
本文章向大家介绍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中不同。

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