pom:
<!-- openfeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
application上添加对openfeign的支持(@EnableFeignClients):
package com.zy.fast.admin;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class FastAdminApplication {public static void main(String[] args) {SpringApplication.run(FastAdminApplication.class, args);}}
1.openfeign全局拦截器(+@Configuration注解)
package com.zy.fast.admin.config;import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;@Slf4j
@Configuration
public class FeignClientInterceptorConfig implements RequestInterceptor {@Overridepublic void apply(RequestTemplate requestTemplate) {log.info("feign interceptor entered");// 设置请求头信息requestTemplate.header("authKey","7777777");}
}
请求service
package com.zy.fast.admin.feign.yxks;import com.zy.fast.admin.config.FeignClientInterceptorConfig;
import com.zy.fast.admin.moudles.base.entity.ServerResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;import java.util.Map;/*** name:指定调用rest接口所对应的服务名* path:指定调用rest接口所在的controller中的@RequestMapping*/
@Component
@FeignClient(name = "ylpj")
public interface FeignYxksService {@PostMapping(value = "/imgCaptcha")public ServerResponse getCode(@RequestBody(required = false) Map<String,String> map);
}
2.openfeign局部拦截器(没有Configuration注解,在feign上加configuration进行指定)
package com.zy.fast.admin.config;import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;@Slf4j
public class FeignClientInterceptorConfig implements RequestInterceptor {@Overridepublic void apply(RequestTemplate requestTemplate) {log.info("feign interceptor entered");// 设置请求头信息requestTemplate.header("authKey","7777777");}
}
请求service
package com.zy.fast.admin.feign.yxks;import com.zy.fast.admin.config.FeignClientInterceptorConfig;
import com.zy.fast.admin.moudles.base.entity.ServerResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;import java.util.Map;/*** name:指定调用rest接口所对应的服务名* path:指定调用rest接口所在的controller中的@RequestMapping*/
@Component
@FeignClient(name = "ylpj", configuration = {FeignClientInterceptorConfig.class})
public interface FeignYxksService {@PostMapping(value = "/imgCaptcha")public ServerResponse getCode(@RequestBody(required = false) Map<String,String> map);
}