소스 검색

hystrix 추가

hyojungkim 5 년 전
부모
커밋
ca057d063a

+ 1 - 0
eurekaserver/bin/main/application.properties

@@ -3,3 +3,4 @@ eureka.client.serviceUrl.defaultZone=http://${EUREKA}/eureka/
 server.port=8761
 eureka.client.register-with-eureka=true
 eureka.client.fetch-registry=true
+#docker run --rm -p 8761:8761 -e EUREKA=192.168.99.100:8761 --name eureka --network api_bridge eureka-server:gateway

+ 1 - 0
eurekaserver/src/main/resources/application.properties

@@ -3,3 +3,4 @@ eureka.client.serviceUrl.defaultZone=http://${EUREKA}/eureka/
 server.port=8761
 eureka.client.register-with-eureka=true
 eureka.client.fetch-registry=true
+#docker run --rm -p 8761:8761 -e EUREKA=192.168.99.100:8761 --name eureka --network api_bridge eureka-server:gateway

+ 16 - 5
zuulgatewayserver/bin/main/application.yml

@@ -9,6 +9,7 @@ eureka:
       defaultZone: http://${EUREKA}/eureka
 
 zuul:
+  ribbon-isolation-strategy: thread
   retryable: true
   ignored-services: "*"
   prefix: /api
@@ -19,16 +20,24 @@ zuul:
 
 hystrix:
   command:
-    ignored-services:
+    default:
       execution:
         isolation:
           strategy: THREAD
           thread:
-            timeoutInMilliseconds: 61000
+            timeoutInMilliseconds: 32000
+      circuitBreaker:
+        enabled: true
+        requestVolumeThreashold: 3
+        sleepWindowInMilliseconds: 5000
+        errorThresholdPercentage: 50
+      metrics:
+        rollingStates:
+          timeInMilliseconds: 10000
 
 ribbon:
-  ConnectTimeout: 60000
-  ReadTimeout: 60000
+  ConnectTimeout: 30000
+  ReadTimeout: 30000
 
 icd-search:
   ribbon:
@@ -47,5 +56,7 @@ icd-search:
     # 첫 시도 실패시 다음 서버로 재시도 하는 수(첫번째 전송은 제외)
     MaxAutoRetriesNextServer: 1
 
-    #Whether all operations can be retried for this client
+    #이 클라이언트에 대해 모든 작업을 재 시도 할 수 있는지 여부
     OkToRetryOnAllOperations: true
+
+#docker run --rm -p 8080:8080 -e EUREKA=192.168.99.100:8761 --name gateway --network api_bridge zuul-gateway:gateway

BIN
zuulgatewayserver/bin/main/com/idatabank/zuulgatewayapp/zuulgatewayapp/AuthServiceFallbackConfiguration.class


BIN
zuulgatewayserver/bin/main/com/idatabank/zuulgatewayapp/zuulgatewayapp/GatewayClientResponse.class


+ 30 - 0
zuulgatewayserver/src/main/java/com/idatabank/zuulgatewayapp/zuulgatewayapp/AuthServiceFallbackConfiguration.java

@@ -0,0 +1,30 @@
+package com.idatabank.zuulgatewayapp.zuulgatewayapp;
+
+import com.netflix.hystrix.exception.HystrixTimeoutException;
+
+import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.client.ClientHttpResponse;
+
+@Configuration
+public class AuthServiceFallbackConfiguration implements FallbackProvider {
+
+  private static final String NOT_AVAILABLE = "service is not available";
+
+  // fallback을 등록할 서비스
+  @Override
+  public String getRoute() {
+    return "icd-search";
+  }
+
+  // fallback 발생시 호출되는 메서드
+  @Override
+  public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
+    if (cause instanceof HystrixTimeoutException) {
+      return new GatewayClientResponse(HttpStatus.GATEWAY_TIMEOUT, NOT_AVAILABLE);
+    } else {
+      return new GatewayClientResponse(HttpStatus.INTERNAL_SERVER_ERROR, NOT_AVAILABLE);
+    }
+  }
+}

+ 52 - 0
zuulgatewayserver/src/main/java/com/idatabank/zuulgatewayapp/zuulgatewayapp/GatewayClientResponse.java

@@ -0,0 +1,52 @@
+package com.idatabank.zuulgatewayapp.zuulgatewayapp;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.client.ClientHttpResponse;
+
+public class GatewayClientResponse implements ClientHttpResponse {
+  private HttpStatus httpStatus;
+  private String message;
+
+  public GatewayClientResponse(HttpStatus httpStatus, String message) {
+    this.httpStatus = httpStatus;
+    this.message = message;
+  }
+
+  @Override
+  public HttpStatus getStatusCode() throws IOException {
+    return httpStatus;
+  }
+
+  @Override
+  public int getRawStatusCode() throws IOException {
+    return httpStatus.value();
+  }
+
+  @Override
+  public String getStatusText() throws IOException {
+    return httpStatus.getReasonPhrase();
+  }
+
+  @Override
+  public void close() {
+
+  }
+
+  @Override
+  public InputStream getBody() throws IOException {
+    return new ByteArrayInputStream(message.getBytes());
+  }
+
+  @Override
+  public HttpHeaders getHeaders() {
+    HttpHeaders headers = new HttpHeaders();
+    headers.setContentType(MediaType.APPLICATION_JSON);
+    return headers;
+  }
+}

+ 16 - 5
zuulgatewayserver/src/main/resources/application.yml

@@ -9,6 +9,7 @@ eureka:
       defaultZone: http://${EUREKA}/eureka
 
 zuul:
+  ribbon-isolation-strategy: thread
   retryable: true
   ignored-services: "*"
   prefix: /api
@@ -19,16 +20,24 @@ zuul:
 
 hystrix:
   command:
-    ignored-services:
+    default:
       execution:
         isolation:
           strategy: THREAD
           thread:
-            timeoutInMilliseconds: 61000
+            timeoutInMilliseconds: 32000
+      circuitBreaker:
+        enabled: true
+        requestVolumeThreashold: 3
+        sleepWindowInMilliseconds: 5000
+        errorThresholdPercentage: 50
+      metrics:
+        rollingStates:
+          timeInMilliseconds: 10000
 
 ribbon:
-  ConnectTimeout: 60000
-  ReadTimeout: 60000
+  ConnectTimeout: 30000
+  ReadTimeout: 30000
 
 icd-search:
   ribbon:
@@ -47,5 +56,7 @@ icd-search:
     # 첫 시도 실패시 다음 서버로 재시도 하는 수(첫번째 전송은 제외)
     MaxAutoRetriesNextServer: 1
 
-    #Whether all operations can be retried for this client
+    #이 클라이언트에 대해 모든 작업을 재 시도 할 수 있는지 여부
     OkToRetryOnAllOperations: true
+
+#docker run --rm -p 8080:8080 -e EUREKA=192.168.99.100:8761 --name gateway --network api_bridge zuul-gateway:gateway