GitHub地址 官方文档地址
1、引入OpenFeign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
2、定义FeignClict 接口
使用@FeignClient(name = 服务名,path = 服务下controller类上路径,configuration = feign配置)
package com.mj.feign.serive.nacos;
import com.mj.feign.config.FeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "api-nacos", path = "/nacos", configuration = FeignConfig.class)
public interface NacosFeignService {
@GetMapping("/get")
String getUsername();
}
3、配置启动类
添加@EnableFeignClients 可以指定包名@EnableFeignClients({“com.mj.feign.*”})
@SpringBootApplication
@EnableFeignClients
public class JayNacosApplication {
public static void main(String[] args) {
SpringApplication.run(JayNacosApplication.class, args);
}
}
4、Feign配置类
package com.mj.feign.config;
import com.mj.feign.intercept.CustomizeIntercept;
import feign.Logger;
import feign.Request;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
@Bean
public Request.Options options(){
return new Request.Options(5000,10000);
}
@Bean
public CustomizeIntercept customizeIntercept(){
return new CustomizeIntercept();
}
}
|