From 311598aead5e507b8e0741cbd4bafd5c55fddffa Mon Sep 17 00:00:00 2001 From: steel Date: Fri, 26 Apr 2019 14:41:52 +0800 Subject: [PATCH 1/6] update to openfeign --- client/pom.xml | 9 ++-- .../demo/client}/HelloClientApplication.java | 6 +-- client/src/main/resources/application.yml | 43 +++++++++++++++---- pom.xml | 20 +++++++-- server/pom.xml | 9 ++-- .../demo/server}/HelloServerApplication.java | 13 +++--- server/src/main/resources/application.yml | 34 +++++++++++++-- 7 files changed, 100 insertions(+), 34 deletions(-) rename client/src/main/java/{demo => org/fegin/demo/client}/HelloClientApplication.java (86%) rename server/src/main/java/{demo => org/fegin/demo/server}/HelloServerApplication.java (68%) diff --git a/client/pom.xml b/client/pom.xml index bf82281..080a8e3 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -3,8 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.test - feign-eureka-hello-client + client 0.0.1-SNAPSHOT jar @@ -12,7 +11,7 @@ Demo project for Spring Cloud - org.test + org.fegin.demo feign-eureka 0.0.1-SNAPSHOT .. @@ -34,11 +33,11 @@ org.springframework.cloud - spring-cloud-starter-feign + spring-cloud-starter-openfeign org.springframework.cloud - spring-cloud-starter-eureka + spring-cloud-starter-netflix-eureka-client org.springframework.boot diff --git a/client/src/main/java/demo/HelloClientApplication.java b/client/src/main/java/org/fegin/demo/client/HelloClientApplication.java similarity index 86% rename from client/src/main/java/demo/HelloClientApplication.java rename to client/src/main/java/org/fegin/demo/client/HelloClientApplication.java index 1131de6..130cd9f 100644 --- a/client/src/main/java/demo/HelloClientApplication.java +++ b/client/src/main/java/org/fegin/demo/client/HelloClientApplication.java @@ -1,11 +1,11 @@ -package demo; +package org.fegin.demo.client; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.cloud.netflix.feign.EnableFeignClients; -import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; diff --git a/client/src/main/resources/application.yml b/client/src/main/resources/application.yml index c6736ec..85054dd 100644 --- a/client/src/main/resources/application.yml +++ b/client/src/main/resources/application.yml @@ -1,25 +1,50 @@ +info: + version: 1.0.0 + spring: application: name: HelloClient + cloud: + inetutils: + default-hostname: hello.client server: port: 7211 eureka: - password: password client: serviceUrl: - defaultZone: http://user:${eureka.password}@localhost:8761/eureka/ + defaultZone: http://192.168.5.181:8761/eureka,http://10.20.0.61:8761/eureka instance: leaseRenewalIntervalInSeconds: 10 metadataMap: instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}} + prefer-ip-address: true +# ip-address: 192.168.6.132 + hostname: hello.client + instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port} + +management: + endpoint: + restart: + enabled: true + shutdown: + enabled: true + health: + enabled: true +feign: + httpclient: + max-connections: 4 + max-connections-per-route: 1 + connection-timeout: 2000 + compression: + request: + enabled: true + response: + enabled: true -endpoints: - restart: - enabled: true - shutdown: - enabled: true - health: - sensitive: false \ No newline at end of file +logging: + level: + com: + netflix: debug \ No newline at end of file diff --git a/pom.xml b/pom.xml index 55e0f38..683c88e 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.test + org.fegin.demo feign-eureka 0.0.1-SNAPSHOT pom @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 2.1.4.RELEASE @@ -33,10 +33,24 @@ org.springframework.cloud spring-cloud-dependencies - Camden.BUILD-SNAPSHOT + Greenwich.RELEASE pom import + + org.springframework.cloud + spring-cloud-openfeign-dependencies + 2.1.0.RELEASE + pom + import + + + org.springframework.cloud + spring-cloud-netflix-dependencies + 2.1.0.RELEASE + pom + import + diff --git a/server/pom.xml b/server/pom.xml index 0502fa7..786d45f 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -3,8 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.test - feign-eureka-hello-server + server 0.0.1-SNAPSHOT jar @@ -12,7 +11,7 @@ Demo project for Spring Cloud - org.test + org.fegin.demo feign-eureka 0.0.1-SNAPSHOT .. @@ -34,11 +33,11 @@ org.springframework.cloud - spring-cloud-starter-feign + spring-cloud-starter-openfeign org.springframework.cloud - spring-cloud-starter-eureka + spring-cloud-starter-netflix-eureka-client org.springframework.boot diff --git a/server/src/main/java/demo/HelloServerApplication.java b/server/src/main/java/org/fegin/demo/server/HelloServerApplication.java similarity index 68% rename from server/src/main/java/demo/HelloServerApplication.java rename to server/src/main/java/org/fegin/demo/server/HelloServerApplication.java index 08d09fa..0f9778a 100644 --- a/server/src/main/java/demo/HelloServerApplication.java +++ b/server/src/main/java/org/fegin/demo/server/HelloServerApplication.java @@ -1,14 +1,17 @@ -package demo; +package org.fegin.demo.server; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + /** * @author Spencer Gibb */ @@ -20,9 +23,9 @@ public class HelloServerApplication { DiscoveryClient client; @RequestMapping("/") - public String hello() { - ServiceInstance localInstance = client.getLocalServiceInstance(); - return "Hello World: "+ localInstance.getServiceId()+":"+localInstance.getHost()+":"+localInstance.getPort(); + public String hello() throws JsonProcessingException { + List localInstance = client.getServices(); + return "Hello World: "+ new ObjectMapper().writeValueAsString(localInstance); } public static void main(String[] args) { diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index 66af54a..557518e 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -1,16 +1,42 @@ +info: + version: 1.0.0 + spring: application: name: HelloServer + cloud: + inetutils: + default-hostname: hello.server server: port: 7111 eureka: - password: password client: serviceUrl: - defaultZone: http://user:${eureka.password}@localhost:8761/eureka/ + defaultZone: http://192.168.5.181:8761/eureka,http://10.20.0.61:8761/eureka instance: leaseRenewalIntervalInSeconds: 10 - metadataMap: - instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}} \ No newline at end of file + prefer-ip-address: true +# ip-address: 192.168.6.132 + hostname: hello.server + # 本地有虚拟机网卡会导致windows一直获取的是localhost + instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port} + +feign: + hystrix: + enabled: true + httpclient: + max-connections: 4 + max-connections-per-route: 1 + connection-timeout: 2000 + compression: + request: + enabled: true + response: + enabled: true + +logging: + level: + com: + netflix: debug \ No newline at end of file From 9425e95319258b6fa3d08770a29a2fd9ff788737 Mon Sep 17 00:00:00 2001 From: steel Date: Fri, 26 Apr 2019 16:20:14 +0800 Subject: [PATCH 2/6] Constructing --- .../org/fegin/demo/client/HelloClient.java | 16 ++++++++++ .../demo/client/HelloClientApplication.java | 20 ------------- .../fegin/demo/client/HelloController.java | 25 ++++++++++++++++ .../demo/client/HystrixClientFallback.java | 15 ++++++++++ client/src/main/resources/application.yml | 10 ++++++- .../fegin/demo/server/HelloController.java | 30 +++++++++++++++++++ .../demo/server/HelloServerApplication.java | 18 ----------- server/src/main/resources/application.yml | 8 ++++- 8 files changed, 102 insertions(+), 40 deletions(-) create mode 100644 client/src/main/java/org/fegin/demo/client/HelloClient.java create mode 100644 client/src/main/java/org/fegin/demo/client/HelloController.java create mode 100644 client/src/main/java/org/fegin/demo/client/HystrixClientFallback.java create mode 100644 server/src/main/java/org/fegin/demo/server/HelloController.java diff --git a/client/src/main/java/org/fegin/demo/client/HelloClient.java b/client/src/main/java/org/fegin/demo/client/HelloClient.java new file mode 100644 index 0000000..9332afe --- /dev/null +++ b/client/src/main/java/org/fegin/demo/client/HelloClient.java @@ -0,0 +1,16 @@ +package org.fegin.demo.client; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.RequestMapping; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +/** + * @author steel + * @datetime 2019/4/26 15:55 + */ +@FeignClient(name = "HelloServer", fallback = HystrixClientFallback.class) +public interface HelloClient { + @RequestMapping(value = "/", method = GET) + String hello(); +} diff --git a/client/src/main/java/org/fegin/demo/client/HelloClientApplication.java b/client/src/main/java/org/fegin/demo/client/HelloClientApplication.java index 130cd9f..815e2f7 100644 --- a/client/src/main/java/org/fegin/demo/client/HelloClientApplication.java +++ b/client/src/main/java/org/fegin/demo/client/HelloClientApplication.java @@ -1,39 +1,19 @@ package org.fegin.demo.client; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; -import org.springframework.cloud.openfeign.FeignClient; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import static org.springframework.web.bind.annotation.RequestMethod.GET; /** * @author Spencer Gibb */ @SpringBootApplication @EnableDiscoveryClient -@RestController @EnableFeignClients public class HelloClientApplication { - @Autowired - HelloClient client; - - @RequestMapping("/") - public String hello() { - return client.hello(); - } - public static void main(String[] args) { SpringApplication.run(HelloClientApplication.class, args); } - @FeignClient("HelloServer") - interface HelloClient { - @RequestMapping(value = "/", method = GET) - String hello(); - } } diff --git a/client/src/main/java/org/fegin/demo/client/HelloController.java b/client/src/main/java/org/fegin/demo/client/HelloController.java new file mode 100644 index 0000000..7088452 --- /dev/null +++ b/client/src/main/java/org/fegin/demo/client/HelloController.java @@ -0,0 +1,25 @@ +package org.fegin.demo.client; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author steel + * @datetime 2019/4/26 15:56 + */ +@RestController +public class HelloController { + private final HelloClient helloClient; + + @Autowired + public HelloController(HelloClient helloClient) { + this.helloClient = helloClient; + } + + @RequestMapping(method = RequestMethod.GET, value = "/") + public String hello() { + return helloClient.hello(); + } +} diff --git a/client/src/main/java/org/fegin/demo/client/HystrixClientFallback.java b/client/src/main/java/org/fegin/demo/client/HystrixClientFallback.java new file mode 100644 index 0000000..519b0ef --- /dev/null +++ b/client/src/main/java/org/fegin/demo/client/HystrixClientFallback.java @@ -0,0 +1,15 @@ +package org.fegin.demo.client; + +import org.springframework.stereotype.Service; + +/** + * @author steel + * @datetime 2019/4/26 15:55 + */ +@Service +public class HystrixClientFallback implements HelloClient { + @Override + public String hello() { + return "fallback"; + } +} diff --git a/client/src/main/resources/application.yml b/client/src/main/resources/application.yml index 85054dd..ac39b0c 100644 --- a/client/src/main/resources/application.yml +++ b/client/src/main/resources/application.yml @@ -35,7 +35,7 @@ management: feign: httpclient: - max-connections: 4 + max-connections: 1 max-connections-per-route: 1 connection-timeout: 2000 compression: @@ -43,6 +43,14 @@ feign: enabled: true response: enabled: true + client: + config: + default: + connectTimeout: 5000 + readTimeout: 5000 + loggerLevel: basic + hystrix: + enabled: true logging: level: diff --git a/server/src/main/java/org/fegin/demo/server/HelloController.java b/server/src/main/java/org/fegin/demo/server/HelloController.java new file mode 100644 index 0000000..315d6c9 --- /dev/null +++ b/server/src/main/java/org/fegin/demo/server/HelloController.java @@ -0,0 +1,30 @@ +package org.fegin.demo.server; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * @author steel + * @datetime 2019/4/26 16:13 + */ +@RestController +public class HelloController { + private final DiscoveryClient discoveryClient; + + @Autowired + public HelloController(DiscoveryClient client) { + this.discoveryClient = client; + } + + @RequestMapping("/") + public String hello() throws JsonProcessingException { + List localInstance = discoveryClient.getServices(); + return "Hello World: "+ new ObjectMapper().writeValueAsString(localInstance); + } +} diff --git a/server/src/main/java/org/fegin/demo/server/HelloServerApplication.java b/server/src/main/java/org/fegin/demo/server/HelloServerApplication.java index 0f9778a..291cf30 100644 --- a/server/src/main/java/org/fegin/demo/server/HelloServerApplication.java +++ b/server/src/main/java/org/fegin/demo/server/HelloServerApplication.java @@ -1,33 +1,15 @@ package org.fegin.demo.server; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.List; /** * @author Spencer Gibb */ @SpringBootApplication @EnableDiscoveryClient -@RestController public class HelloServerApplication { - @Autowired - DiscoveryClient client; - - @RequestMapping("/") - public String hello() throws JsonProcessingException { - List localInstance = client.getServices(); - return "Hello World: "+ new ObjectMapper().writeValueAsString(localInstance); - } - public static void main(String[] args) { SpringApplication.run(HelloServerApplication.class, args); } diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index 557518e..4a90b08 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -27,7 +27,7 @@ feign: hystrix: enabled: true httpclient: - max-connections: 4 + max-connections: 1 max-connections-per-route: 1 connection-timeout: 2000 compression: @@ -35,6 +35,12 @@ feign: enabled: true response: enabled: true + client: + config: + default: + connectTimeout: 5000 + readTimeout: 5000 + loggerLevel: basic logging: level: From 6b2877407712c104c723dfe8e620b48d78af4e8c Mon Sep 17 00:00:00 2001 From: steel Date: Fri, 26 Apr 2019 18:57:32 +0800 Subject: [PATCH 3/6] Constructing Hystrix briker --- .../org/fegin/demo/client/HelloClient.java | 2 +- ...Fallback.java => HelloClientFallback.java} | 4 +- .../client/HelloClientFallbackFactory.java | 27 ++++++++++++ client/src/main/resources/application.yml | 27 ++++++++++-- .../fegin/demo/client/CommonMainTests.java | 42 +++++++++++++++++++ .../org/fegin/demo/client/CommonTests.java | 9 ++++ pom.xml | 1 + .../fegin/demo/server/HelloController.java | 4 +- server/src/main/resources/application.yml | 4 +- 9 files changed, 111 insertions(+), 9 deletions(-) rename client/src/main/java/org/fegin/demo/client/{HystrixClientFallback.java => HelloClientFallback.java} (70%) create mode 100644 client/src/main/java/org/fegin/demo/client/HelloClientFallbackFactory.java create mode 100644 client/src/test/java/org/fegin/demo/client/CommonMainTests.java create mode 100644 client/src/test/java/org/fegin/demo/client/CommonTests.java diff --git a/client/src/main/java/org/fegin/demo/client/HelloClient.java b/client/src/main/java/org/fegin/demo/client/HelloClient.java index 9332afe..72f5f0c 100644 --- a/client/src/main/java/org/fegin/demo/client/HelloClient.java +++ b/client/src/main/java/org/fegin/demo/client/HelloClient.java @@ -9,7 +9,7 @@ * @author steel * @datetime 2019/4/26 15:55 */ -@FeignClient(name = "HelloServer", fallback = HystrixClientFallback.class) +@FeignClient(name = "HelloServer", fallbackFactory = HelloClientFallbackFactory.class) public interface HelloClient { @RequestMapping(value = "/", method = GET) String hello(); diff --git a/client/src/main/java/org/fegin/demo/client/HystrixClientFallback.java b/client/src/main/java/org/fegin/demo/client/HelloClientFallback.java similarity index 70% rename from client/src/main/java/org/fegin/demo/client/HystrixClientFallback.java rename to client/src/main/java/org/fegin/demo/client/HelloClientFallback.java index 519b0ef..e02a903 100644 --- a/client/src/main/java/org/fegin/demo/client/HystrixClientFallback.java +++ b/client/src/main/java/org/fegin/demo/client/HelloClientFallback.java @@ -6,8 +6,8 @@ * @author steel * @datetime 2019/4/26 15:55 */ -@Service -public class HystrixClientFallback implements HelloClient { +@Service("helloClientFallback") +public class HelloClientFallback implements HelloClient { @Override public String hello() { return "fallback"; diff --git a/client/src/main/java/org/fegin/demo/client/HelloClientFallbackFactory.java b/client/src/main/java/org/fegin/demo/client/HelloClientFallbackFactory.java new file mode 100644 index 0000000..89604c7 --- /dev/null +++ b/client/src/main/java/org/fegin/demo/client/HelloClientFallbackFactory.java @@ -0,0 +1,27 @@ +package org.fegin.demo.client; + +import feign.hystrix.FallbackFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +/** + * @author steel + * @datetime 2019/4/26 17:39 + */ +@Service +public class HelloClientFallbackFactory implements FallbackFactory { + private static final Logger LOGGER = LoggerFactory.getLogger(HelloClientFallbackFactory.class); + private HelloClient helloClient; + + public HelloClientFallbackFactory(@Qualifier("helloClientFallback") HelloClient helloClient) { + this.helloClient = helloClient; + } + + @Override + public HelloClient create(Throwable throwable) { + LOGGER.error("fallback error ", throwable); + return helloClient; + } +} diff --git a/client/src/main/resources/application.yml b/client/src/main/resources/application.yml index ac39b0c..ee59928 100644 --- a/client/src/main/resources/application.yml +++ b/client/src/main/resources/application.yml @@ -32,11 +32,16 @@ management: enabled: true health: enabled: true - +# hystrix: +# config: +# execution: +# isolation: +# thread: +# timeoutInMilliseconds: 2000 feign: httpclient: - max-connections: 1 - max-connections-per-route: 1 + max-connections: 200 + max-connections-per-route: 50 connection-timeout: 2000 compression: request: @@ -52,6 +57,22 @@ feign: hystrix: enabled: true +# 以配置hystrix超时时间为例 +hystrix: + command: + #默认的超时时间设置 + default: + execution: + isolation: + thread: + timeoutInMilliseconds: 1000 + # commandKey,默认为方法名 + hello: + execution: + isolation: + thread: + timeoutInMilliseconds: 3000 #暂未生效,需要跟踪 + logging: level: com: diff --git a/client/src/test/java/org/fegin/demo/client/CommonMainTests.java b/client/src/test/java/org/fegin/demo/client/CommonMainTests.java new file mode 100644 index 0000000..3543c08 --- /dev/null +++ b/client/src/test/java/org/fegin/demo/client/CommonMainTests.java @@ -0,0 +1,42 @@ +package org.fegin.demo.client; + +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * @author steel + * @datetime 2019/4/26 16:33 + */ +public class CommonMainTests { + private static final Logger LOGGER = LoggerFactory.getLogger(CommonMainTests.class); + private static HttpClient httpClient = HttpClientBuilder.create().build(); + + public static void main(String[] args) { + ExecutorService executorService = Executors.newFixedThreadPool(100); + CountDownLatch countDownLatch = new CountDownLatch(1); + for (int i = 0; i < 1000; i++) { + executorService.submit(() -> { + try { + countDownLatch.await(); + HttpResponse httpResponse = httpClient.execute(new HttpGet("http://192.168.115.1:7211/")); + LOGGER.info(IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8")); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + + }); + } + countDownLatch.countDown(); + + } +} diff --git a/client/src/test/java/org/fegin/demo/client/CommonTests.java b/client/src/test/java/org/fegin/demo/client/CommonTests.java new file mode 100644 index 0000000..6022eb4 --- /dev/null +++ b/client/src/test/java/org/fegin/demo/client/CommonTests.java @@ -0,0 +1,9 @@ +package org.fegin.demo.client; + +/** + * @author steel + * @datetime 2019/4/26 16:31 + */ +public class CommonTests { + +} diff --git a/pom.xml b/pom.xml index 683c88e..10e4d64 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,7 @@ UTF-8 1.8 + 1.4.6.RELEASE diff --git a/server/src/main/java/org/fegin/demo/server/HelloController.java b/server/src/main/java/org/fegin/demo/server/HelloController.java index 315d6c9..a3546ca 100644 --- a/server/src/main/java/org/fegin/demo/server/HelloController.java +++ b/server/src/main/java/org/fegin/demo/server/HelloController.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang.math.RandomUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; @@ -23,7 +24,8 @@ public HelloController(DiscoveryClient client) { } @RequestMapping("/") - public String hello() throws JsonProcessingException { + public String hello() throws JsonProcessingException, InterruptedException { + Thread.sleep(RandomUtils.nextInt(2000)); List localInstance = discoveryClient.getServices(); return "Hello World: "+ new ObjectMapper().writeValueAsString(localInstance); } diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index 4a90b08..3320aa6 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -27,8 +27,8 @@ feign: hystrix: enabled: true httpclient: - max-connections: 1 - max-connections-per-route: 1 + max-connections: 200 + max-connections-per-route: 50 connection-timeout: 2000 compression: request: From d3160b019af794fd30125e984147aa3462d365f3 Mon Sep 17 00:00:00 2001 From: steel Date: Sun, 28 Apr 2019 10:48:46 +0800 Subject: [PATCH 4/6] add commandKey by customer ordering --- client/src/main/resources/application.yml | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/client/src/main/resources/application.yml b/client/src/main/resources/application.yml index ee59928..1b48c97 100644 --- a/client/src/main/resources/application.yml +++ b/client/src/main/resources/application.yml @@ -32,12 +32,14 @@ management: enabled: true health: enabled: true -# hystrix: -# config: -# execution: -# isolation: -# thread: -# timeoutInMilliseconds: 2000 + hystrix: + config: + default: + execution: + isolation: + thread: + timeoutInMilliseconds: 2000 + feign: httpclient: max-connections: 200 @@ -67,12 +69,13 @@ hystrix: thread: timeoutInMilliseconds: 1000 # commandKey,默认为方法名 - hello: + HelloClient#hello(): execution: isolation: thread: - timeoutInMilliseconds: 3000 #暂未生效,需要跟踪 + timeoutInMilliseconds: 1500 +# logging logging: level: com: From d04de520fef71f9180c3daf1fa10ea28f8cdc37f Mon Sep 17 00:00:00 2001 From: steel Date: Sun, 28 Apr 2019 11:29:06 +0800 Subject: [PATCH 5/6] remove un used dependencies --- pom.xml | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/pom.xml b/pom.xml index 10e4d64..dd3e7db 100644 --- a/pom.xml +++ b/pom.xml @@ -38,20 +38,6 @@ pom import - - org.springframework.cloud - spring-cloud-openfeign-dependencies - 2.1.0.RELEASE - pom - import - - - org.springframework.cloud - spring-cloud-netflix-dependencies - 2.1.0.RELEASE - pom - import - From afd863201d9db612afc8e6ef2a156f29d2dd3f00 Mon Sep 17 00:00:00 2001 From: steel Date: Sun, 28 Apr 2019 17:15:45 +0800 Subject: [PATCH 6/6] add Hystrix support --- .../demo/client/HelloClientApplication.java | 2 ++ .../HystrixServletRegistrationBean.java | 16 +++++++++++++ .../configuration/HystrixConfiguration.java | 24 +++++++++++++++++++ pom.xml | 8 +++++++ .../demo/server/HelloServerApplication.java | 2 ++ .../HystrixServletRegistrationBean.java | 16 +++++++++++++ .../configuration/HystrixConfiguration.java | 24 +++++++++++++++++++ server/src/main/resources/application.yml | 15 ++++++++++++ 8 files changed, 107 insertions(+) create mode 100644 client/src/main/java/org/fegin/demo/client/HystrixServletRegistrationBean.java create mode 100644 client/src/main/java/org/fegin/demo/client/configuration/HystrixConfiguration.java create mode 100644 server/src/main/java/org/fegin/demo/server/HystrixServletRegistrationBean.java create mode 100644 server/src/main/java/org/fegin/demo/server/configuration/HystrixConfiguration.java diff --git a/client/src/main/java/org/fegin/demo/client/HelloClientApplication.java b/client/src/main/java/org/fegin/demo/client/HelloClientApplication.java index 815e2f7..cbf41da 100644 --- a/client/src/main/java/org/fegin/demo/client/HelloClientApplication.java +++ b/client/src/main/java/org/fegin/demo/client/HelloClientApplication.java @@ -3,6 +3,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; /** @@ -10,6 +11,7 @@ */ @SpringBootApplication @EnableDiscoveryClient +@EnableHystrixDashboard @EnableFeignClients public class HelloClientApplication { public static void main(String[] args) { diff --git a/client/src/main/java/org/fegin/demo/client/HystrixServletRegistrationBean.java b/client/src/main/java/org/fegin/demo/client/HystrixServletRegistrationBean.java new file mode 100644 index 0000000..d46d06d --- /dev/null +++ b/client/src/main/java/org/fegin/demo/client/HystrixServletRegistrationBean.java @@ -0,0 +1,16 @@ +package org.fegin.demo.client; + +import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; +import org.springframework.boot.web.servlet.ServletRegistrationBean; + +/** + * @author steel + * @datetime 2019/4/28 11:46 + */ +public class HystrixServletRegistrationBean extends ServletRegistrationBean { + + public HystrixServletRegistrationBean(HystrixMetricsStreamServlet servlet, String... urlMappings) { + super(servlet, urlMappings); + } + +} diff --git a/client/src/main/java/org/fegin/demo/client/configuration/HystrixConfiguration.java b/client/src/main/java/org/fegin/demo/client/configuration/HystrixConfiguration.java new file mode 100644 index 0000000..a3ae3d5 --- /dev/null +++ b/client/src/main/java/org/fegin/demo/client/configuration/HystrixConfiguration.java @@ -0,0 +1,24 @@ +package org.fegin.demo.client.configuration; + +import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; +import org.fegin.demo.client.HystrixServletRegistrationBean; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author steel + * @datetime 2019/4/28 11:40 + */ +@Configuration +public class HystrixConfiguration { + @Bean(name = "hystrixRegistrationBean") + public ServletRegistrationBean servletRegistrationBean() { + ServletRegistrationBean registration = new HystrixServletRegistrationBean( + new HystrixMetricsStreamServlet(), "/hystrix.stream"); + registration.setName("hystrixServlet"); + registration.setLoadOnStartup(1); + return registration; + } + +} diff --git a/pom.xml b/pom.xml index dd3e7db..9adf16d 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,14 @@ 1.4.6.RELEASE + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix-dashboard + 2.1.0.RELEASE + + + diff --git a/server/src/main/java/org/fegin/demo/server/HelloServerApplication.java b/server/src/main/java/org/fegin/demo/server/HelloServerApplication.java index 291cf30..0f761d8 100644 --- a/server/src/main/java/org/fegin/demo/server/HelloServerApplication.java +++ b/server/src/main/java/org/fegin/demo/server/HelloServerApplication.java @@ -3,12 +3,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; /** * @author Spencer Gibb */ @SpringBootApplication @EnableDiscoveryClient +@EnableHystrixDashboard public class HelloServerApplication { public static void main(String[] args) { SpringApplication.run(HelloServerApplication.class, args); diff --git a/server/src/main/java/org/fegin/demo/server/HystrixServletRegistrationBean.java b/server/src/main/java/org/fegin/demo/server/HystrixServletRegistrationBean.java new file mode 100644 index 0000000..b242181 --- /dev/null +++ b/server/src/main/java/org/fegin/demo/server/HystrixServletRegistrationBean.java @@ -0,0 +1,16 @@ +package org.fegin.demo.server; + +import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; +import org.springframework.boot.web.servlet.ServletRegistrationBean; + +/** + * @author steel + * @datetime 2019/4/28 11:46 + */ +public class HystrixServletRegistrationBean extends ServletRegistrationBean { + + public HystrixServletRegistrationBean(HystrixMetricsStreamServlet servlet, String... urlMappings) { + super(servlet, urlMappings); + } + +} diff --git a/server/src/main/java/org/fegin/demo/server/configuration/HystrixConfiguration.java b/server/src/main/java/org/fegin/demo/server/configuration/HystrixConfiguration.java new file mode 100644 index 0000000..34b6fc6 --- /dev/null +++ b/server/src/main/java/org/fegin/demo/server/configuration/HystrixConfiguration.java @@ -0,0 +1,24 @@ +package org.fegin.demo.server.configuration; + +import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; +import org.fegin.demo.server.HystrixServletRegistrationBean; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author steel + * @datetime 2019/4/28 11:40 + */ +@Configuration +public class HystrixConfiguration { + @Bean(name = "hystrixRegistrationBean") + public ServletRegistrationBean servletRegistrationBean() { + ServletRegistrationBean registration = new HystrixServletRegistrationBean( + new HystrixMetricsStreamServlet(), "/hystrix.stream"); + registration.setName("hystrixServlet"); + registration.setLoadOnStartup(1); + return registration; + } + +} diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index 3320aa6..9be85b2 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -42,6 +42,21 @@ feign: readTimeout: 5000 loggerLevel: basic +management: + endpoint: + restart: + enabled: true + shutdown: + enabled: true + health: + enabled: true + hystrix: + config: + default: + execution: + isolation: + thread: + timeoutInMilliseconds: 2000 logging: level: com: