diff --git a/LICENSE b/LICENSE index 9e32cdef1625daed25cf365c865f01050877cff3..528e80a33f6504b2cb2d4b0587057e7e64f6a2cf 100644 --- a/LICENSE +++ b/LICENSE @@ -91,7 +91,7 @@ 3. No Trademark License - No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4. + No trademark license is granted to use the trade names, trademarks, com.bdc.springcloud.service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4. 4. Distribution Restriction diff --git a/cloud-consumer-feign-hystrix-order80/pom.xml b/cloud-consumer-feign-hystrix-order80/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..ac0caef762287e96dafd61859e2512905f9f7a91 --- /dev/null +++ b/cloud-consumer-feign-hystrix-order80/pom.xml @@ -0,0 +1,67 @@ + + + + cloud20220 + com.bdc.springcloud + 1.0-SNAPSHOT + + 4.0.0 + + cloud-consumer-feign-hystrix-order80 + + + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + com.bdc.springcloud + cloud-api-commons + ${project.version} + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + 8 + 8 + + + \ No newline at end of file diff --git a/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/OrderHystrixMain80.java b/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/OrderHystrixMain80.java new file mode 100644 index 0000000000000000000000000000000000000000..c79e7c4da0dd1ef06734ced2f8fabec27c4df4cf --- /dev/null +++ b/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/OrderHystrixMain80.java @@ -0,0 +1,25 @@ +package com.bdc.springcloud; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.hystrix.EnableHystrix; +import org.springframework.cloud.openfeign.EnableFeignClients; + +/** + * gdmz_jqyang@outlook.com 版权所有 © Copyright 2020
+ * + * @Description: 主启动类 + * @Project: cloud20220 + * @CreateDate: Created in 2021/4/12 23:12
+ * @Author: gdmz_jqyang@outlook.com + */ +@EnableFeignClients +@SpringBootApplication +@EnableHystrix +public class OrderHystrixMain80 { + public static void main(String[] args) { + SpringApplication.run(OrderHystrixMain80.class,args); + } +} + + diff --git a/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/controller/OrderHystrixController.java b/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/controller/OrderHystrixController.java new file mode 100644 index 0000000000000000000000000000000000000000..b604708dff920f03c6636872af957934ae938db8 --- /dev/null +++ b/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/controller/OrderHystrixController.java @@ -0,0 +1,58 @@ +package com.bdc.springcloud.controller; + +import com.bdc.springcloud.service.OrderHystrixService; +import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * gdmz_jqyang@outlook.com 版权所有 © Copyright 2020
+ * + * @Description: OrderHystrixController + * @Project: cloud20220 + * @CreateDate: Created in 2021/4/12 23:15
+ * @Author: gdmz_jqyang@outlook.com + */ +@RestController +@Slf4j +@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod") //全局的 +public class OrderHystrixController { + @Resource + private OrderHystrixService orderHystrixService; + + @Value("${server.port}") + private String serverPort; + + @GetMapping("/consumer/payment/hystrix/ok/{id}") + public String paymentInfo_OK(@PathVariable("id") Integer id){ + String result = orderHystrixService.paymentInfo_OK(id); + log.info("*******result:"+result); + return result; + } + @GetMapping("/consumer/payment/hystrix/timeout/{id}") +/* @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = { + @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500") //3秒钟以内就是正常的业务逻辑 + })*/ + @HystrixCommand + public String paymentInfo_TimeOut(@PathVariable("id") Integer id){ + String result = orderHystrixService.paymentInfo_TimeOut(id); + return result; + } + + //兜底方法 + public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){ + return "我是消费者80,对付支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,(┬_┬)"; + } + + //下面是全局fallback方法 + public String payment_Global_FallbackMethod(){ + return "Global异常处理信息,请稍后再试,(┬_┬)"; + } + +} diff --git a/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/service/OrderFallbackService.java b/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/service/OrderFallbackService.java new file mode 100644 index 0000000000000000000000000000000000000000..44be4e91d3edfe662633d638991bfac011ef453b --- /dev/null +++ b/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/service/OrderFallbackService.java @@ -0,0 +1,24 @@ +package com.bdc.springcloud.service; + +import org.springframework.stereotype.Component; + +/** + * gdmz_jqyang@outlook.com 版权所有 © Copyright 2020
+ * + * @Description: OrderHystrixService + * @Project: cloud20220 + * @CreateDate: Created in 2021/4/13 00:24
+ * @Author: gdmz_jqyang@outlook.com + */ +@Component +public class OrderFallbackService implements OrderHystrixService{ + @Override + public String paymentInfo_OK(Integer id) { + return "-----PaymentFallbackService fall back-paymentInfo_OK , (┬_┬)"; + } + + @Override + public String paymentInfo_TimeOut(Integer id) { + return "-----PaymentFallbackService fall back-paymentInfo_TimeOut , (┬_┬)"; + } +} diff --git a/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/service/OrderHystrixService.java b/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/service/OrderHystrixService.java new file mode 100644 index 0000000000000000000000000000000000000000..23b573e657bde859dcffdee70918e13d42bd473a --- /dev/null +++ b/cloud-consumer-feign-hystrix-order80/src/main/java/com/bdc/springcloud/service/OrderHystrixService.java @@ -0,0 +1,24 @@ +package com.bdc.springcloud.service; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +/** + * gdmz_jqyang@outlook.com 版权所有 © Copyright 2020
+ * + * @Description: OrderHystrixService + * @Project: cloud20220 + * @CreateDate: Created in 2021/4/12 23:14
+ * @Author: gdmz_jqyang@outlook.com + */ +@Component +@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = OrderFallbackService.class) +public interface OrderHystrixService { + @GetMapping("/payment/hystrix/ok/{id}") + public String paymentInfo_OK(@PathVariable("id") Integer id); + + @GetMapping("/payment/hystrix/timeout/{id}") + public String paymentInfo_TimeOut(@PathVariable("id") Integer id); +} diff --git a/cloud-consumer-feign-hystrix-order80/src/main/resources/application.yml b/cloud-consumer-feign-hystrix-order80/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..ff004e25983493ba59776de83837f45fbfcadd7d --- /dev/null +++ b/cloud-consumer-feign-hystrix-order80/src/main/resources/application.yml @@ -0,0 +1,17 @@ +server: + port: 80 + +eureka: + client: + register-with-eureka: true #表识不向注册中心注册自己 + fetch-registry: true #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务 + service-url: + defaultZone: http://127.0.0.1:7001/eureka/ + +spring: + application: + name: cloud-provider-hystrix-order + +feign: + hystrix: + enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样。 diff --git a/cloud-consumer-feign-order80/src/main/resources/application.yml b/cloud-consumer-feign-order80/src/main/resources/application.yml index 7655deb29b18d51e33addedde7ec5874704efb92..5e8ac78a7a0be63a206fd662b44ce2912e09cc0d 100644 --- a/cloud-consumer-feign-order80/src/main/resources/application.yml +++ b/cloud-consumer-feign-order80/src/main/resources/application.yml @@ -4,7 +4,7 @@ eureka: client: register-with-eureka: false service-url: - defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka + defaultZone: http://127.0.0.1:7001/eureka, http://eureka7002.com:7002/eureka ribbon: ReadTimeout: 5000 ConnectTimeout: 5000 diff --git a/cloud-consumer-hystrix-dashboard9001/pom.xml b/cloud-consumer-hystrix-dashboard9001/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..2b1411902d5133d4e782f3ddf8dd0f5d7201eebc --- /dev/null +++ b/cloud-consumer-hystrix-dashboard9001/pom.xml @@ -0,0 +1,50 @@ + + + + cloud20220 + com.bdc.springcloud + 1.0-SNAPSHOT + + 4.0.0 + + cloud-consumer-hystrix-dashboard9001 + + + + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix-dashboard + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + 8 + 8 + + + \ No newline at end of file diff --git a/cloud-consumer-hystrix-dashboard9001/src/main/java/com/bdc/springcloud/HystrixDashboardMain9001.java b/cloud-consumer-hystrix-dashboard9001/src/main/java/com/bdc/springcloud/HystrixDashboardMain9001.java new file mode 100644 index 0000000000000000000000000000000000000000..4a9c721d53087d0c0954042b5697db037e29154d --- /dev/null +++ b/cloud-consumer-hystrix-dashboard9001/src/main/java/com/bdc/springcloud/HystrixDashboardMain9001.java @@ -0,0 +1,21 @@ +package com.bdc.springcloud; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; + +/** + * gdmz_jqyang@outlook.com 版权所有 © Copyright 2020
+ * + * @Description: HystrixDashboardMain + * @Project: cloud20220 + * @CreateDate: Created in 2021/4/14 23:02
+ * @Author: gdmz_jqyang@outlook.com + */ +@SpringBootApplication +@EnableHystrixDashboard +public class HystrixDashboardMain9001 { + public static void main(String[] args) { + SpringApplication.run(HystrixDashboardMain9001.class,args); + } +} diff --git a/cloud-consumer-hystrix-dashboard9001/src/main/resources/application.yml b/cloud-consumer-hystrix-dashboard9001/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..7fd77988479492fcd13b7e2f39850c3458cc10b0 --- /dev/null +++ b/cloud-consumer-hystrix-dashboard9001/src/main/resources/application.yml @@ -0,0 +1,2 @@ +server: + port: 9001 diff --git a/cloud-consumer-order80/src/main/java/com/bdc/springcloud/OrderMain80.java b/cloud-consumer-order80/src/main/java/com/bdc/springcloud/OrderMain80.java index 2140a8f0f03ba4c8c8fe352b10ed5cf71bc78195..2d545c611551063321d329f84b3a816dbe4a2458 100644 --- a/cloud-consumer-order80/src/main/java/com/bdc/springcloud/OrderMain80.java +++ b/cloud-consumer-order80/src/main/java/com/bdc/springcloud/OrderMain80.java @@ -1,14 +1,13 @@ package com.bdc.springcloud; -import com.bdc.myrule.MyselfRule; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; -import org.springframework.cloud.netflix.ribbon.RibbonClient; -import org.springframework.cloud.netflix.ribbon.RibbonClients; +import org.springframework.cloud.netflix.hystrix.EnableHystrix; @SpringBootApplication @EnableEurekaClient +@EnableHystrix //@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MyselfRule.class) public class OrderMain80 { public static void main(String[] args) { diff --git a/cloud-consumer-order80/src/main/resources/application.yml b/cloud-consumer-order80/src/main/resources/application.yml index 206d825a99d89ebbc7a49ed9607fa3d415e6c4c1..3359090e11f89635e0dcb98a2696ae98f02bff9a 100644 --- a/cloud-consumer-order80/src/main/resources/application.yml +++ b/cloud-consumer-order80/src/main/resources/application.yml @@ -3,12 +3,15 @@ server: spring: application: - name: cloud-order-service + name: cloud-order-com.bdc.springcloud.service eureka: client: register-with-eureka: true fetchRegistry: true service-url: - #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版 - defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #单机 + #defaultZone: http://127.0.0.1:7001/eureka,http://eureka7002.com:7002/eureka #集群版 + defaultZone: http://127.0.0.1:7001/eureka,http://eureka7002.com:7002/eureka #单机 +feign: + hystrix: + enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样。 diff --git a/cloud-eureka-server7001/src/main/resources/application.yml b/cloud-eureka-server7001/src/main/resources/application.yml index 904ae40cb02bcdbfcff5c73cdd045895d6b94f07..eaa383a600f7aa29a182abd09b6d400123cc4ab0 100644 --- a/cloud-eureka-server7001/src/main/resources/application.yml +++ b/cloud-eureka-server7001/src/main/resources/application.yml @@ -3,13 +3,13 @@ server: eureka: instance: - hostname: eureka7001.com #eureka服务端的实例名字 + hostname: 127.0.0.1 #eureka服务端的实例名字 client: register-with-eureka: false #表识不向注册中心注册自己 fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务 service-url: #defaultZone: http://eureka7002.com:7002/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址 集群 - defaultZone: http://eureka7001.com:7001/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址 单机 + defaultZone: http://127.0.0.1:7001/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址 单机 #server: #enable-self-preservation: false #eviction-interval-timer-in-ms: 2000 diff --git a/cloud-eureka-server7002/src/main/resources/application.yml b/cloud-eureka-server7002/src/main/resources/application.yml index 04fe7731ed1ec14574f13e1c1537a29cd82c9f0e..b71d2eec361b29b8c138e7eaecd06ac63ec8b3e1 100644 --- a/cloud-eureka-server7002/src/main/resources/application.yml +++ b/cloud-eureka-server7002/src/main/resources/application.yml @@ -8,5 +8,5 @@ eureka: register-with-eureka: false #表识不向注册中心注册自己 fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务 service-url: - defaultZone: http://eureka7001.com:7001/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址 + defaultZone: http://127.0.0.1:7001/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址 diff --git a/cloud-provider-hystrix-payment8001/pom.xml b/cloud-provider-hystrix-payment8001/pom.xml index f28f574b9a949cc96bc0c80170b7931de59fc031..90e4728764c4ed8fb358d6449d99fc8fb687592b 100644 --- a/cloud-provider-hystrix-payment8001/pom.xml +++ b/cloud-provider-hystrix-payment8001/pom.xml @@ -28,7 +28,6 @@ spring-cloud-starter-netflix-eureka-client - com.bdc.springcloud cloud-api-commons diff --git a/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/PaymentHystrixMain8001.java b/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/PaymentHystrixMain8001.java index b345937800b26e306b550ba289c21c2fc64359a0..d18ed6c270f7173af6ba534c33284fd9c1f2e1af 100644 --- a/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/PaymentHystrixMain8001.java +++ b/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/PaymentHystrixMain8001.java @@ -1,13 +1,31 @@ package com.bdc.springcloud; +import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.netflix.hystrix.EnableHystrix; +import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableEurekaClient +@EnableHystrix +@EnableCircuitBreaker public class PaymentHystrixMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentHystrixMain8001.class,args); } + + + @Bean + public ServletRegistrationBean getServlet(){ + HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); + ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); + registrationBean.setLoadOnStartup(1); + registrationBean.addUrlMappings("/hystrix.stream"); + registrationBean.setName("HystrixMetricsStreamServlet"); + return registrationBean; + } } diff --git a/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/controller/PaymentController.java b/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/controller/PaymentController.java index 143642f4367ac72292cff25830e963ba4ff1965f..7a99a4f76ea5a582fd132bb7ef34f1732fb03701 100644 --- a/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/controller/PaymentController.java +++ b/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/controller/PaymentController.java @@ -39,4 +39,12 @@ public class PaymentController { log.info("*******result:"+result); return result; } + //===服务熔断 + @GetMapping("/payment/circuit/{id}") + public String paymentCircuitBreaker(@PathVariable("id") Integer id){ + String result = paymentService.paymentCircuitBreaker(id); + log.info("*******result:"+result); + return result; + } + } diff --git a/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/service/PaymentService.java b/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/service/PaymentService.java index a130b9d01fc1ecf8e189e89621343fc668661338..8a3a4f05af385419a3f3cff04a87175dd23d5427 100644 --- a/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/service/PaymentService.java +++ b/cloud-provider-hystrix-payment8001/src/main/java/com/bdc/springcloud/service/PaymentService.java @@ -1,6 +1,10 @@ package com.bdc.springcloud.service; +import cn.hutool.core.util.IdUtil; +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; +import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.PathVariable; import java.util.concurrent.TimeUnit; @@ -12,9 +16,36 @@ public class PaymentService { } //失败 + @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHadler",commandProperties = { + @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000") + }) public String paymentInfo_TimeOut(Integer id){ - int timeNumber = 3; + int timeNumber = 5; try { TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) {e.printStackTrace();} return "线程池:"+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t"+"呜呜呜"+" 耗时(秒)"+timeNumber; } + + public String paymentInfo_TimeOutHadler(Integer id){ + return "线程池:"+Thread.currentThread().getName()+" paymentInfo_TimeOutHadler,id: "+id+"\t"+"呜呜呜"; + } + + //服务熔断 + @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = { + @HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //是否开启断路器 + @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //请求次数 + @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //时间范围 + @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"), //失败率达到多少后跳闸 + }) + public String paymentCircuitBreaker(@PathVariable("id") Integer id){ + if (id < 0){ + throw new RuntimeException("*****id 不能负数"); + } + String serialNumber = IdUtil.simpleUUID(); + + return Thread.currentThread().getName()+"\t"+"调用成功,流水号:"+serialNumber; + } + public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){ + return "id 不能负数,请稍候再试,(┬_┬)/~~ id: " +id; + } + } diff --git a/cloud-provider-hystrix-payment8001/src/main/resources/application.yml b/cloud-provider-hystrix-payment8001/src/main/resources/application.yml index 9439d2a8f699bac1aca62615a16e75e238988e52..46d8075a9ae7d9ca7ad1ac213ce502b775ad930b 100644 --- a/cloud-provider-hystrix-payment8001/src/main/resources/application.yml +++ b/cloud-provider-hystrix-payment8001/src/main/resources/application.yml @@ -6,7 +6,7 @@ eureka: fetch-registry: true #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务 service-url: # defaultZone: http://eureka7002.com:7002/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址 - defaultZone: http://eureka7001.com:7001/eureka/ + defaultZone: http://127.0.0.1:7001/eureka/ instance: instance-id: cloud-provider-hystrix-payment8001 prefer-ip-address: true @@ -16,3 +16,8 @@ spring: application: name: cloud-provider-hystrix-payment # eviction-interval-timer-in-ms: 2000 + +feign: + hystrix: + enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样。 + diff --git a/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/controller/PaymentController.java b/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/controller/PaymentController.java index 097d420f360f36b132d32a8bf2a25c1fc3ed5634..545016e32b6abac08a149a102f539251b59173ed 100644 --- a/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/controller/PaymentController.java +++ b/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/controller/PaymentController.java @@ -69,6 +69,12 @@ public class PaymentController { return serverPort; } + @GetMapping(value = "/payment/testRequest") + public String testRequest(){ + paymentService.tetsRequest(); + return serverPort; + } + @GetMapping(value = "/payment/feign/timeout") public String paymentFeignTimeout(){ diff --git a/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/service/PaymentService.java b/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/service/PaymentService.java index 9feeaa948b65cc28301d7b06ecb1dee8e0102bc1..a7cc78406dd305c5542a290a5a60b813d7547115 100644 --- a/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/service/PaymentService.java +++ b/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/service/PaymentService.java @@ -7,4 +7,6 @@ public interface PaymentService { public int create(Payment payment); public Payment getPaymentById(@Param("id")Long id); + + public void tetsRequest(); } diff --git a/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/service/impl/PaymentServiceImpl.java b/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/service/impl/PaymentServiceImpl.java index 6a03bf65fe5d12582f894bf9a1114a88b77009ef..a06a74392fc648a128f53729a07a44a22f72ff86 100644 --- a/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/service/impl/PaymentServiceImpl.java +++ b/cloud-provider-payment-8001/src/main/java/com/bdc/springcloud/service/impl/PaymentServiceImpl.java @@ -4,6 +4,8 @@ import com.bdc.springcloud.dao.PaymentDao; import com.bdc.springcloud.entities.Payment; import com.bdc.springcloud.service.PaymentService; import org.springframework.stereotype.Service; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource; @@ -22,4 +24,10 @@ public class PaymentServiceImpl implements PaymentService { public Payment getPaymentById(Long id) { return paymentDao.getPaymentById(id); } + + @Override + public void tetsRequest() { + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + System.out.println(requestAttributes.getRequest()); + } } diff --git a/cloud-provider-payment-8001/src/main/resources/application.yml b/cloud-provider-payment-8001/src/main/resources/application.yml index 8987c1fd1f1fedf823437e93b0803c880e2a5edf..5f5c94d78d8723250309de6f435a72efdb070c2e 100644 --- a/cloud-provider-payment-8001/src/main/resources/application.yml +++ b/cloud-provider-payment-8001/src/main/resources/application.yml @@ -1,23 +1,23 @@ server: - port: 8001 + port: 8002 spring: application: - name: cloud-payment-service + name: cloud-payment-com.bdc.springcloud.service datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db2020?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=UTC username: root - password: 123456789 + password: 123456 eureka: client: register-with-eureka: true fetchRegistry: true service-url: - #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版 - defaultZone: http://eureka7001.com:7001/eureka #单机 + #defaultZone: http://127.0.0.1:7001/eureka,http://eureka7002.com:7002/eureka #集群版 + defaultZone: http://127.0.0.1:7001/eureka #单机 instance: instance-id: payment8001 prefer-ip-address: true diff --git a/cloud-provider-payment-8002/src/main/resources/application.yml b/cloud-provider-payment-8002/src/main/resources/application.yml index b428933d265b0ab23074f01d500c015c2e449acf..abff3c9c4c42a5d7c5d9019a6c140f389f346477 100644 --- a/cloud-provider-payment-8002/src/main/resources/application.yml +++ b/cloud-provider-payment-8002/src/main/resources/application.yml @@ -16,7 +16,7 @@ eureka: register-with-eureka: true fetchRegistry: true service-url: - defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版 + defaultZone: http://127.0.0.1:7001/eureka,http://eureka7002.com:7002/eureka #集群版 instance: instance-id: payment8002 prefer-ip-address: true diff --git a/pom.xml b/pom.xml index 389619be15c00d27ed5e92028adb080a60081cbb..034da93aa2e2584c4781876a83f519d762e5e1ab 100644 --- a/pom.xml +++ b/pom.xml @@ -118,6 +118,7 @@ org.springframework.boot spring-boot-maven-plugin + 2.2.6.RELEASE true true