1)简介
Feign使得编写java http客户端变得更容易,它使用Jersey和CXF等工具为ReST或SOAP服务编写Java客户端。有了它我们就不用谢restTemplate、webClient 调用 HTTP APIs 规则的 ReSTful 接口。
Feign 10.x 需要jdk 8, 9 ,11 支持,如果是jdk 6 可以用 Feign 9.x 。
官方地址:https://github.com/OpenFeign/feign
2)原理
1)pom文件
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2)配置文件
application.properties
#服务端端口
server.port=8761
#服务端禁用注册
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
3)启用 EurekaServer
@SpringBootApplication
//通过注解启用
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
1)pom文件
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2)配置文件
application.properties
#服务端口
server.port=12345
bootstrap.properties
#应用名称
spring.application.name=waiter-service
3)启用 EurekaClient
@EnableDiscoveryClient 注解启用client
@SpringBootApplication
@EnableDiscoveryClient
public class WaiterServiceApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(WaiterServiceApplication.class, args);
}
}
注意:在本地启用EurekaServer,本地EurekaClient不需要配置注册地址,默认会注册上EurekaServer
4)验证启动结果
1)pom文件
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
io.github.openfeign
feign-httpclient
2)配置文件
application.properties
#服务端口0表示任意端口
server.port=0
#feig超时时间设置
feign.client.config.default.connect-timeout=500
feign.client.config.default.read-timeout=500
bootstrap.properties
#服务名称
spring.application.name=customer-service
3)启用Feign
@EnableFeignClients 表示启动Feign
@SpringBootApplication
@Slf4j
@EnableDiscoveryClient
@EnableFeignClients
public class CustomerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerServiceApplication.class, args);
}
/**
* 重新构造httpClient
* @return
*/
@Bean
public CloseableHttpClient httpClient() {
return HttpClients.custom()
.setConnectionTimeToLive(30, TimeUnit.SECONDS)
.evictIdleConnections(30, TimeUnit.SECONDS)
.setMaxConnTotal(200)
.setMaxConnPerRoute(20)
.disableAutomaticRetries()
.setKeepAliveStrategy(new CustomConnectionKeepAliveStrategy())
.build();
}
}
4)定义interface 并加上服务提供方相关配置
@FeignClient(name = "waiter-service", contextId = "coffeeOrder")
public interface CoffeeOrderService {
@GetMapping("/order/{id}")
CoffeeOrder getOrder(@PathVariable("id") Long id);
@PostMapping(path = "/order/", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
CoffeeOrder create(@RequestBody NewOrderRequest newOrder);
}
5)调用远程接口
@Component
@Slf4j
public class CustomerRunner implements ApplicationRunner {
@Autowired
private CoffeeService coffeeService;
@Autowired
private CoffeeOrderService coffeeOrderService;
@Override
public void run(ApplicationArguments args) throws Exception {
readMenu();
}
}