Feign簡介
Feign集成示例
Feign的初始化
Feign與Hystrix集成
Feign使用
- 項目依賴 Web、Actuator、Hystrix、Feign、Eureka
- 在啓動類頭添加註解
@EnableFeignClients
全部使用
或@EnableFeignClients(basePackages = “com.study.*” , defaultConfiguration = “Foo.class”) 部分使用
- 配置修改
server:
port: 8005
spring:
application:
name: feign-demo
eureka:
client:
service-url:
defaultZone : http://127.0.0.1:10000/eureka/
feign:
hystrix:
enabled: true
hystrix:
command:
default:
execution:
isolation:
thread :
timeoutInMilliseconds: 100
- 定義接口,在接口添加註解
@Component
@FeignClient(name="helloserver",fallback = HelloDemoFallback.class)
public interface HelloDemoService {
}
Feign調用流程
首先,掃描到我們自己寫的@FeignClient註解,通過每個註解修飾的一個class去創建一個動態代理,當我們發起調用的時候,他就會幫我們生成一個相應的動態代理對象,通過這個對象去發起http請求到服務提供者