OpenFeign 設定オプション
グローバル設定
基本設定プロパティ
spring:
cloud:
openfeign:
# Feign クライアントの有効化
enabled: true
# デフォルト設定
default-config:
connect-timeout: 5000
read-timeout: 5000
logger-level: basic
# プロパティへのデフォルト変換の有効化
client:
default-to-properties: true
# リフレッシュサポート
client:
refresh-enabled: false
# 遅延属性解決
lazy-attributes-resolution: false
ロギング設定
spring:
cloud:
openfeign:
# ロギング設定
client:
config:
default:
logger-level: basic
user-service:
logger-level: full
logging:
level:
com.example.clients: DEBUG
タイムアウト設定
spring:
cloud:
openfeign:
client:
config:
default:
connect-timeout: 5000 # 接続タイムアウト(ミリ秒)
read-timeout: 5000 # 読み取りタイムアウト(ミリ秒)
user-service:
connect-timeout: 3000
read-timeout: 10000
クライアント固有の設定
名前付きクライアント設定
spring:
cloud:
openfeign:
client:
config:
# デフォルト設定(すべてのクライアントに適用)
default:
connect-timeout: 5000
read-timeout: 5000
logger-level: basic
# 特定のクライアント設定
user-service:
url: http://localhost:8081
connect-timeout: 3000
read-timeout: 10000
logger-level: full
error-decoder: com.example.CustomErrorDecoder
retryer: com.example.CustomRetryer
encoder: com.example.CustomEncoder
decoder: com.example.CustomDecoder
contract: com.example.CustomContract
設定プロパティの優先順位
@Configuration と設定プロパティを同時に使用する場合、設定プロパティが優先されます。優先順位を変更するには:
spring:
cloud:
openfeign:
client:
default-to-properties: false # @Configuration を優先
HTTP クライアント設定
OkHttpClient 設定
spring:
cloud:
openfeign:
okhttp:
enabled: true
client:
config:
default:
# OkHttpClient 固有の設定
httpclient:
okhttp:
connect-timeout: 5000
read-timeout: 5000
write-timeout: 5000
follow-redirects: true
Apache HttpClient 5 設定
spring:
cloud:
openfeign:
httpclient:
hc5:
enabled: true
client:
config:
default:
# HttpClient 5 固有の設定
httpclient:
hc5:
connect-timeout: 5000
socket-timeout: 5000
connection-request-timeout: 5000
汎用 HTTP クライアント設定
spring:
cloud:
openfeign:
client:
config:
default:
httpclient:
# すべての HTTP クライアントに適用される汎用設定
max-connections: 200
max-connections-per-route: 20
time-to-live: 30
サーキットブレーカー設定
基本サーキットブレーカー設定
spring:
cloud:
openfeign:
circuitbreaker:
enabled: true
group:
enabled: false
alphanumeric-ids:
enabled: true
Resilience4J 設定
resilience4j:
circuitbreaker:
instances:
UserServicegetUser:
minimum-number-of-calls: 69
failure-rate-threshold: 50
wait-duration-in-open-state: 5000
permitted-number-of-calls-in-half-open-state: 10
sliding-window-size: 100
sliding-window-type: COUNT_BASED
timelimiter:
instances:
UserServicegetUser:
timeout-duration: 10s
サーキットブレーカー名パターン
# 古い命名パターン (clientName_methodName) を使用
spring:
cloud:
openfeign:
circuitbreaker:
alphanumeric-ids:
enabled: false
圧縮設定
リクエスト圧縮
spring:
cloud:
openfeign:
compression:
request:
enabled: true
mime-types:
- text/xml
- application/xml
- application/json
min-request-size: 2048
レスポンス圧縮
spring:
cloud:
openfeign:
compression:
response:
enabled: true
OAuth2 設定
基本 OAuth2 設定
spring:
cloud:
openfeign:
oauth2:
enabled: true
client-registration-id: my-client
OAuth2 クライアント登録
spring:
security:
oauth2:
client:
registration:
my-client:
client-id: ${OAUTH2_CLIENT_ID}
client-secret: ${OAUTH2_CLIENT_SECRET}
authorization-grant-type: client_credentials
scope: read,write
provider:
my-provider:
token-uri: https://auth-server.com/oauth/token
キャッシング設定
キャッシングの有効化/無効化
spring:
cloud:
openfeign:
cache:
enabled: true
Spring Cache 設定
spring:
cache:
type: caffeine
caffeine:
spec: maximumSize=500,expireAfterWrite=600s
Micrometer 設定
基本監視設定
spring:
cloud:
openfeign:
micrometer:
enabled: true
クライアント固有の監視
spring:
cloud:
openfeign:
client:
config:
user-service:
micrometer:
enabled: true
グローバル監視の無効化
spring:
cloud:
openfeign:
micrometer:
enabled: false
リトライ設定
カスタムリトライヤー
@Configuration
public class FeignConfig {
@Bean
public Retryer retryer() {
return new Retryer.Default(
100, // 初期間隔(ミリ秒)
1000, // 最大間隔(ミリ秒)
3 // 最大リトライ回数
);
}
}
リトライの無効化
@Configuration
public class FeignConfig {
@Bean
public Retryer retryer() {
return Retryer.NEVER_RETRY; // リトライを無効化
}
}
エンコーダー/デコーダー設定
カスタムエンコーダー
@Configuration
public class FeignConfig {
@Bean
public Encoder feignEncoder() {
return new CustomEncoder();
}
@Bean
public Decoder feignDecoder() {
return new CustomDecoder();
}
}
SpringEncoder 設定
spring:
cloud:
openfeign:
encoder:
charset-from-content-type: false
コントラクト設定
カスタムコントラクト
@Configuration
public class FeignConfig {
@Bean
public Contract feignContract() {
return new SpringMvcContract(); // デフォルト
// または return new feign.Contract.Default(); // ネイティブ Feign アノテーション
}
}
インターセプター設定
リクエストインターセプター
@Configuration
public class FeignConfig {
@Bean
public RequestInterceptor requestInterceptor() {
return new CustomRequestInterceptor();
}
}
設定プロパティによるインターセプター追加
spring:
cloud:
openfeign:
client:
config:
user-service:
request-interceptors:
- com.example.FooRequestInterceptor
- com.example.BarRequestInterceptor
ケイパビリティ設定
カスタムケイパビリティ
@Configuration
public class FeignConfig {
@Bean
public Capability customCapability() {
return new CustomCapability();
}
}
設定プロパティによるケイパビリティ追加
spring:
cloud:
openfeign:
client:
config:
user-service:
capabilities:
- com.example.FooCapability
- com.example.BarCapability
URL 設定
複数の URL 設定方法
| 設定方法 | 例 | 説明 |
|---|---|---|
| アノテーションで指定 | @FeignClient(url="http://localhost:8081") | 優先度最高、ロードバランシングを使用しない |
| 設定プロパティで指定 | spring.cloud.openfeign.client.config.clientName.url | リフレッシュ対応、ロードバランシングを使用しない |
| name のみ指定 | @FeignClient(name="service-name") | サービスディスカバリとロードバランシングを使用 |
動的 URL 設定
spring:
cloud:
openfeign:
client:
config:
user-service:
url: ${USER_SERVICE_URL:http://localhost:8081}
リフレッシュ設定
リフレッシュサポートの有効化
spring:
cloud:
openfeign:
client:
refresh-enabled: true
リフレッシュエンドポイント
# 設定のリフレッシュ
POST /actuator/refresh
# リフレッシュ可能な設定の確認
GET /actuator/configprops
AOT 設定
AOT 互換設定
spring:
cloud:
refresh:
enabled: false
openfeign:
client:
refresh-enabled: false
lazy-attributes-resolution: false
完全設定例
spring:
cloud:
openfeign:
enabled: true
client:
default-to-properties: true
refresh-enabled: false
lazy-attributes-resolution: false
micrometer:
enabled: true
cache:
enabled: true
oauth2:
enabled: false
compression:
request:
enabled: true
mime-types: [application/json, application/xml]
min-request-size: 2048
response:
enabled: true
circuitbreaker:
enabled: true
group:
enabled: false
alphanumeric-ids:
enabled: true
okhttp:
enabled: false
httpclient:
hc5:
enabled: true
client:
config:
default:
connect-timeout: 5000
read-timeout: 5000
logger-level: basic
micrometer:
enabled: true
user-service:
url: http://user-service:8080
connect-timeout: 3000
read-timeout: 10000
logger-level: full
error-decoder: com.example.UserErrorDecoder
retryer: com.example.UserRetryer
request-interceptors:
- com.example.AuthRequestInterceptor
micrometer:
enabled: true
これらの設定オプションを適切に構成することで、OpenFeign の機能を最大限に活用し、さまざまな複雑なマイクロサービス通信要件に対応できます。
