【SpringCloud】(八):認識Feign及使用

00潤物無聲00發表於2017-08-13

  這篇文章我們來認識一下Feign。

  官網連結:http://cloud.spring.io/spring-cloud-static/Camden.SR7/#spring-cloud-feign




1.宣告式的客戶端web服務,讓寫web服務客戶端更加簡單

2.使用的時候建立一個介面,加上註解。

3.可插拔的註解,Feign註解,編碼,解碼,Spring MVC的支援。

4.Feign整合Ribbon和Eureka,提供負載均衡。


拷貝並修改前面所描述的電影微服務microservice-comsumer-movie,修改為:microservice-comsumer-movie-feign


1.POM.xml加入依賴

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>

2.在啟動類上新增註解:@EnableFeignClients

package com.dynamic.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ComsumerMovieFeignApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(ComsumerMovieFeignApplication.class, args);
	}
}

3.建立一個介面UserFeignClient

在介面上加入了註解@FeignClient。配置了使用者為服務的ServicId :microservice-provider-user


package com.dynamic.cloud.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.dynamic.cloud.entity.User;

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
	
	@RequestMapping(value="/simple/{id}",method = RequestMethod.GET) //只能是RequestMapping
	public User findById(@PathVariable("id") Long id); //@PathVariable需要設定括號中的名稱
	
	@RequestMapping(value="/user",method = RequestMethod.POST)
	public User postUser(@RequestBody User user);
	
	//如果引數是物件,請求就不會成功,即使指定了Get方法,feign依然會以Post方式進行傳送請求。
	//解決方案:@RequestParam("id") Long id,@RequestParam("name") Long name
	@RequestMapping(value="/get-user",method = RequestMethod.GET)
	public User getUser( @RequestParam("id") Long id,@RequestParam("name") String name); 

}


Controller

package com.dynamic.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dynamic.cloud.entity.User;
import com.dynamic.cloud.feign.UserFeignClient;

@RestController
public class MovieController {

	@Autowired
	private UserFeignClient userFeignClient;
	
	@GetMapping("/movie/{id}")
	public User findById(@PathVariable Long id)
	{
		return this.userFeignClient.findById(id);
	}
	
	
	@GetMapping("/test")
	public User testPost(User user)
	{
		return this.userFeignClient.postUser(user);
	}

	@GetMapping("/get-user")
	public User getUser(@RequestParam("id") Long id,@RequestParam("name")String name)
	{
		return this.userFeignClient.getUser(id,name);
	}
}


使用者微服務:microservice-provider-user

在Controller中加入響應呼叫方式,其它不做修改

	@PostMapping("/user")
	public User postUser(@RequestBody User user)
	{
		return user;
	}
	
	@GetMapping("/get-user")
	public User getUser( User user)
	{
		return user;
	}

通過Feign完成呼叫。



相關文章