SpringBoot中Feign使用
使用前提:
已经完成注册中心(Eureka/zookeeper等)搭建,因为Feign依赖注册中心注册表进行负载
应用客户端配置
1)添加pom依赖
<!-- feign和zuul默认需要依赖ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- 負載均衡 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2)配置文件-application.properties
spring.application.name=tp-cust #配置网关映射端口 server.port=18702 #是否显示主机ip eureka.instance.preferIpAddress=true #指定eureka服务端地址 eureka.client.serviceUrl.defaultZone=http://localhost:18700/eureka/ #leaseRenewalIntervalInSeconds,表示eureka client发送心跳给server端的频率 eureka.instance.leaseRenewalIntervalInSeconds=50 #leaseExpirationDurationInSeconds,表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。 #默认为90秒 #如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。 #如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。 #该值至少应该大于leaseRenewalIntervalInSeconds eureka.instance.leaseExpirationDurationInSeconds=30 #表示eureka client间隔多久去拉取服务注册信息,默认为30秒,对于api-gateway,如果要迅速获取服务注册状态,可以缩小该值,比如5秒 eureka.client.registryFetchIntervalSeconds=50 #feign使用ribbon所以使用feign时候需要配置ribbon #读取时间 ribbon.ReadTimeout=60000 #链接时间 ribbon.ConnectTimeout=60000 #开启ribbon使用eureka注册表功能 ribbon.eureka.enable=true
3)启动类添加@EnableFeignClients注解
package com.threepicture;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
//import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
/**
* 启动加载,必须放在项目根目录
* @author 杏仁拌饭
*
*/
@SpringBootApplication
//eureka客户端注册(此注解属于组合注解包含@EnableDiscoveryClient)
@EnableEurekaClient
//开启hystrix断路功能-需要在contrller中配置HystrixCommand
@EnableCircuitBreaker
//Feign负载
@EnableFeignClients
//定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中
//@ComponentScan( basePackages = "com.threepicture.frame.*")
//将项目中对应的mapper类的路径加进来就可以了
@MapperScan("com.threepicture.dao.mapper")
public class StartApplication {
private static Logger logger = LoggerFactory.getLogger(StartApplication.class);
public static void main(String[] args) {
logger.info("进入cust....");
SpringApplication.run(StartApplication.class, args);
}
}
4)创建调用接口
package com.threepicture.feignClient;
import java.util.Map;
import org.springframework.cloud.netflix.feign.FeignClient;
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;
/**
* 请求产品工程客户端
* @author 杏仁拌饭
*
*/
//调用注册中心tp-product应用
@FeignClient(name="tp-product")
public interface ProductFeignClient {
/**
* 使用post方式调用带参构造方法
* @param requestMap
* @param id
* @return
*/
@RequestMapping(value="/query-product/ProductInfoById", method=RequestMethod.POST)
public Object getProductInfoById(@RequestBody Map<String, Object> requestMap, @RequestParam("id") String id);
/**
* 使用get方式调用方法
* @return
*/
@RequestMapping(value="/query-product/queryAllInfo", method=RequestMethod.GET)
public Object getproductAllInfo();
}
5)在controller中使用
package com.threepicture.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.threepicture.dao.model.User;
import com.threepicture.feignClient.ProductFeignClient;
import com.threepicture.server.impl.ProductService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@RestController
@RequestMapping("/query-cust")
public class ProductController {
private Logger logger = LoggerFactory.getLogger(ProductController.class);
//注入服务
@Autowired
private ProductService productService;
//注入调用tp-product应用接口
@Autowired
private ProductFeignClient productFeignClient;
@RequestMapping("/queryAllUser")
public List<User> queryAllUser(){
logger.info("进入controller...");
return this.productService.queryAllUser();
}
//使用feign调用tp-product
@RequestMapping("/queryProductAllInfo")
public Object getProductAllInfo(){
return this.productFeignClient.getproductAllInfo();
}
@RequestMapping("/test")
@HystrixCommand(fallbackMethod = "fallBackInfo")
public String getException() throws Exception{
throw new Exception("测试hystrix断路功能...");
}
public String fallBackInfo(){
return "服务暂时不可用,请稍后再试";
}
}

浙公网安备 33010602011771号