本文主要是介绍【SpringCloud】(八):认识Feign及使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这篇文章我们来认识一下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) //只能是RequestMappingpublic 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); }
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 {@Autowiredprivate 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完成调用。
这篇关于【SpringCloud】(八):认识Feign及使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!