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微服務之間的調用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM