1. 程式人生 > >SpringCloud微服務之間的通訊,Feign和RestTemplate概念例項詳解

SpringCloud微服務之間的通訊,Feign和RestTemplate概念例項詳解

前言:

  • 在springcloud微服務的實際專案開發中,多個微服務之間不僅是相對獨立的,而且也是相對關聯的;也就是說,微服務之間需要相互訪問,多個微服務之間的介面可能會被互相多次呼叫,我們稱之為微服務之間的通訊
  • 微服務之間的通訊方式有很多,一般使用以下兩種方法:一種是RestTemplate,另一種是Feign;

概念簡介:

  • RestTemplate,是spring中方便使用rest資源的一個物件,互動訪問的資源通過URL進行識別和定位,每次呼叫都使用模板方法的設計模式,模板方法依賴於具體介面的呼叫,從而實現了資源的互動和呼叫;  它的互動方法有30多種,大多數都是基於HTTP的方法,比如:delete()、getForEntity()、getForObject()、put()、headForHeaders()等; 
  •  Feign,一個宣告式的偽HTTP客戶端,使得編寫HTTP客戶端更加容易;    它只需要建立一個介面,並且使用註解的方式去配置,即可完成對服務提供方的介面繫結,大大簡化了程式碼的開發量;     同時,它還具有可拔插的註解特性,而且支援feign自定義的註解和springMvc的註解(預設);

RestTemplate程式碼實踐:

  •  搭建eureka服務註冊中心環境(這裡不做描述,可自行搭建;
  • 新增依賴;
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 在啟動類Application新增負載均衡標識;
@LoadBalanced
@Bean
public RestTemplate getRestTemplate() {
	return new RestTemplate();
}
  • 服務提供類,服務名稱:SERVICE1,埠:8082;
@RestController
@RequestMapping("/service1")
public class TestController {
		
	@RequestMapping(value = "test", method = {RequestMethod.POST,RequestMethod.GET})
	public String test(@RequestParam(value = "testParam") String testParam) {
		System.println.out(testParam);
		return "success";
	}
		
}
  • 服務消費類;
@RestController
@RequestMapping("/serviceFront")
public class ServiceFrontController {

	private final static String SERVICE1_URL = "http://SERVICE1:8082";

	private final static String SERVICE1 = "SERVICE1";

	@Autowired	
	LoadBalancerClient loadBalancerClient;

	@Autowired
	RestTemplate restTemplate;

	@RequestMapping(value = "testFront", method = RequestMethod.POST)
	public HashMap<String,Object> testFront(@RequestParam String testParam) {
		this.loadBalancerClient.choose(SERVICE1);// 隨機訪問策略
		String result = restTemplate.getForObject(SERVICE1_URL + "/service1/test?testParam={1}", String.class, testParam);
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("result", "測試結果!"+result);
		return map;
	}
}

Feign程式碼實踐:

  •  搭建eureka服務註冊中心環境(這裡不做描述,可自行搭建);
  • 新增依賴;
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
  • 在啟動類Application新增feign註解,宣告啟動feign客戶端;
@EnableFeignClients
  • 服務提供類,服務名稱:SERVICE2,埠:8083;
@RestController
@RequestMapping("/service2")
public class TestController2 {
	
	@RequestMapping(value = "test2", method = {RequestMethod.POST,RequestMethod.GET})
	public String test2(@RequestParam(value = "testParam2") String testParam2) {
		System.println.out(testParam2);
		return "success";
	}
	
}
  • 服務消費介面類;
/**
 * 封裝呼叫服務介面
 */
@FeignClient(name = "SERVICE2")
public interface TestFeignClient {

	//@RequestLine("GET /service2/test2")
	@RequestMapping(value="/service2/test2",method = RequestMethod.GET)
	public String test2(@RequestParam("testParam2") String testParam2);
		
}
  • 服務消費控制層;
@RestController
@RefreshScope
@RequestMapping("/serviceFront2")
public class TestFeignController {
	
	@Autowired
	private TestFeignClient testFeignClient;

	@RequestMapping(value = "test2", method = { RequestMethod.POST })
	public HashMap<String,Object> test2(@RequestParam String testParam2) {		
		String result = testFeignClient.test2(testParam2);
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("result", "測試結果!"+result);
		return map;
	}
}

總結:

微服務之間的通訊方式可以多種並存,各有優勢,在專案實踐中可具體情況具體分析,程式猿可以在開發系統的過程中,積累總結封裝通訊的最佳方式,開發效率事半功倍~