feign介绍
feign整合了ribbon和hystrix。除了整合两者的强大功能外,还提供了一种声明式的web服务客户端定义方式。feign简化了ribbon中template的开发,提供更高层次的接口.feign自身也提供了一下可插拔的组件,如编码器和解码器。等等
快速入门
spring-cloud-consumer-01
步骤:
- 添加feign的依赖
在pom.xml中添加下面的maven配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
启动类中添加@EnableFeignClients注解
开发FeignClient程序
详细代码实现
SpringCloudConsumer01Application.java
package com.example.demo;
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;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import com.example.config.LoadBarentConfig;
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class SpringCloudConsumer01Application {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConsumer01Application.class, args);
}
}
UserFeignClient.java
package com.example.demo.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 com.example.demo.model.User;
import feign.Param;
@FeignClient("spring-cloud-provider")
public interface UserFeignClient {
@RequestMapping(value="/user/get?id={id}",method=RequestMethod.GET)
public User get(@PathVariable("id") String id);
}
UserConstroller.java
package com.example.demo.controller;
@RestController
@RequestMapping(value="/user")
public class UserConstroller {
@Autowired
private UserFeignClient userFeignClient;
@ResponseBody
@GetMapping("/feign_get")
public User feign(String id){
User u = userFeignClient.get(id);
return u;
}
}
服务提供端
spring-cloud-provider-01和spring-cloud-provider-02
这两个微服务注册到eureka中
spring-cloud-provider-01
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.User;
@RestController
@RequestMapping(value="/user")
public class UserController {
private Map<String, User> umap = new HashMap();
public UserController(){
umap.put("1", new User("1", "xiaoming1", 30));
umap.put("2", new User("2", "xiaohong1", 20));
umap.put("3", new User("3", "zhangsan1", 80));
umap.put("4", new User("4", "lisi1", 10));
umap.put("5", new User("5", "wangwu1", 22));
}
@RequestMapping(value="/get")
@ResponseBody
public User get(String id){
return umap.get(id);
}
}
spring-cloud-provider-02
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.User;
@RestController
@RequestMapping(value="/user")
public class UserController {
private Map<String, User> umap = new HashMap();
public UserController(){
umap.put("1", new User("1", "xiaoming", 30));
umap.put("2", new User("2", "xiaohong", 20));
umap.put("3", new User("3", "zhangsan", 80));
umap.put("4", new User("4", "lisi", 10));
umap.put("5", new User("5", "wangwu", 22));
}
@RequestMapping(value="/get")
@ResponseBody
public User get(String id){
return umap.get(id);
}
}
验证
在浏览器中打开:http://192.168.88.101:9000/user/feign_get?id=1
反复刷新浏览器会交替显示下面的json数据
{"id":"1","userName":"xiaoming1","age":30,"birthday":null}
{"id":"1","userName":"xiaoming","age":30,"birthday":null}
问题
在调试feign程序的过程中,经常会出现timeout的问题,可以使用下面的3中方法之一来解决:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
hystrix.command.default.execution.timeout.enabled: false
feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持