├── README.md ├── cloud-api-commons ├── pom.xml └── src │ └── main │ └── java │ └── cn │ └── hzp │ └── springcloud │ └── entities │ ├── CommonResult.java │ └── Payment.java ├── cloud-config-center-3344 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── ConfigCenterMain3344.java │ └── resources │ └── application.yml ├── cloud-config-client-3355 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── ConfigClientMain3355.java │ │ └── controller │ │ └── ConfigClientController.java │ └── resources │ └── bootstrap.yml ├── cloud-config-client-3366 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── ConfigClientMain3366.java │ │ └── controller │ │ └── ConfigClientController.java │ └── resources │ └── bootstrap.yml ├── cloud-consumer-feign-hystrix-order80 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── OrderHystrixMain80.java │ │ ├── controller │ │ └── OrderHystrixController.java │ │ └── service │ │ ├── PaymentFallbackService.java │ │ └── PaymentHystrixService.java │ └── resources │ └── application.yml ├── cloud-consumer-feign-order80 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── OrderFeignMain80.java │ │ ├── config │ │ └── FeignConfig.java │ │ ├── controller │ │ └── OrderFeignController.java │ │ └── service │ │ └── PaymentFeignService.java │ └── resources │ └── application.yml ├── cloud-consumer-hystrix-dashboard9001 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── HystrixDashboardMain9001.java │ └── resources │ └── application.yml ├── cloud-consumer-order80 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ ├── myrule │ │ └── MySelfRule.java │ │ └── springcloud │ │ ├── OrderMain80.java │ │ ├── config │ │ └── ApplicationContextConfig.java │ │ ├── controller │ │ └── OrderController.java │ │ └── lb │ │ ├── LoadBalancer.java │ │ └── MyLB.java │ └── resources │ └── application.yml ├── cloud-consumerconsul-order80 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── OrderMainConsul80.java │ │ ├── config │ │ └── ApplicationContextConfig.java │ │ └── controller │ │ └── OrderConsulController.java │ └── resources │ └── application.yml ├── cloud-consumerzk-order80 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── OrderZKMain80.java │ │ ├── config │ │ └── ApplicationContextConfig.java │ │ └── controller │ │ └── OrderZKController.java │ └── resources │ └── application.yml ├── cloud-eureka-server7001 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── EurekaMain7001.java │ └── resources │ └── application.yml ├── cloud-eureka-server7002 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── EurekaMain7002.java │ └── resources │ └── application.yml ├── cloud-gateway-gateway9527 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── cn │ │ │ └── hzp │ │ │ └── springcloud │ │ │ ├── GateWayMain9527.java │ │ │ ├── config │ │ │ └── GateWayConfig.java │ │ │ └── filter │ │ │ └── MyLogGateWayFilter.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── T2.java ├── cloud-provider-hystrix-payment8001 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── PaymentHystrixMain8001.java │ │ ├── controller │ │ └── PaymentController.java │ │ └── service │ │ └── PaymentService.java │ └── resources │ └── application.yml ├── cloud-provider-payment8001 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── PaymentMain8001.java │ │ ├── controller │ │ └── PaymentController.java │ │ ├── dao │ │ └── PaymentDao.java │ │ └── service │ │ ├── PaymentService.java │ │ └── impl │ │ └── PaymentServiceImpl.java │ └── resources │ ├── application.yml │ └── mapper │ └── PaymentMapper.xml ├── cloud-provider-payment8002 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── PaymentMain8002.java │ │ ├── controller │ │ └── PaymentController.java │ │ ├── dao │ │ └── PaymentDao.java │ │ └── service │ │ ├── PaymentService.java │ │ └── impl │ │ └── PaymentServiceImpl.java │ └── resources │ ├── application.yml │ └── mapper │ └── PaymentMapper.xml ├── cloud-provider-payment8004 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── PaymentMain8004.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloud-providerconsul-payment8006 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── PaymentMain8006.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloud-stream-rabbitmq-consumer8802 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── StreamMQMain8802.java │ │ └── controller │ │ └── ReceiveMessageListenerController.java │ └── resources │ └── application.yml ├── cloud-stream-rabbitmq-consumer8803 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── StreamMQMain8803.java │ │ └── controller │ │ └── ReceiveMessageListenerController.java │ └── resources │ └── application.yml ├── cloud-stream-rabbitmq-provider8801 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ ├── StreamMQMain8801.java │ │ ├── controller │ │ └── SendMessageController.java │ │ └── service │ │ ├── IMessageProvider.java │ │ └── impl │ │ └── MessageProviderImpl.java │ └── resources │ └── application.yml ├── cloudalibaba-config-nacos-client3377 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── alibaba │ │ ├── NacosConfigClientMain3377.java │ │ └── controller │ │ └── ConfigClientController.java │ └── resources │ ├── application.yml │ └── bootstrap.yml ├── cloudalibaba-consumer-nacos-order83 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── alibaba │ │ ├── OrderNacosMain83.java │ │ ├── config │ │ └── ApplicationContextConfig.java │ │ └── controller │ │ └── OrderNacosController.java │ └── resources │ └── application.yml ├── cloudalibaba-consumer-nacos-order84 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── alibaba │ │ ├── OrderNacosMain84.java │ │ ├── config │ │ └── ApplicationContextConfig.java │ │ ├── controller │ │ └── CircleBreakerController.java │ │ └── service │ │ ├── PaymentFallbackService.java │ │ └── PaymentService.java │ └── resources │ └── application.yml ├── cloudalibaba-provider-payment9001 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── alibaba │ │ ├── PaymentMain9001.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloudalibaba-provider-payment9002 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── alibaba │ │ ├── PaymentMain9002.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloudalibaba-provider-payment9003 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── alibaba │ │ ├── PaymentMain9003.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloudalibaba-provider-payment9004 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── alibaba │ │ ├── PaymentMain9004.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloudalibaba-sentinel-service8401 ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── hzp │ │ └── springcloud │ │ └── alibaba │ │ ├── MainApp8401.java │ │ ├── controller │ │ ├── FlowLimitController.java │ │ └── RateLimitController.java │ │ └── myHandler │ │ └── CustomerBlockHandler.java │ └── resources │ └── application.yml ├── pom.xml └── sql └── cloud2020.sql /README.md: -------------------------------------------------------------------------------- 1 | # xiaohu-springcloud2020 2 | 一个简单的springcloud全家桶工程,学习ing,不断更新中 3 | 4 | 脑图请移步这里https://github.com/xiaohu66/Naotu 5 | -------------------------------------------------------------------------------- /cloud-api-commons/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-api-commons 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-devtools 17 | runtime 18 | true 19 | 20 | 21 | org.projectlombok 22 | lombok 23 | true 24 | 1.16.18 25 | 26 | 27 | cn.hutool 28 | hutool-all 29 | 5.1.0 30 | 31 | 32 | -------------------------------------------------------------------------------- /cloud-api-commons/src/main/java/cn/hzp/springcloud/entities/CommonResult.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.entities; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | * @Classname CommonResult 9 | * @Description TODO 10 | * @Date 2020/3/12 11:33 11 | * @Created by HuZP 12 | */ 13 | @Data 14 | @AllArgsConstructor 15 | @NoArgsConstructor 16 | public class CommonResult { 17 | private Integer code; 18 | private String message; 19 | private T data; 20 | 21 | public CommonResult(Integer code, String message) { 22 | this.code = code; 23 | this.message = message; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /cloud-api-commons/src/main/java/cn/hzp/springcloud/entities/Payment.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.entities; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | * @Classname Payment 11 | * @Description TODO 12 | * @Date 2020/3/12 11:31 13 | * @Created by HuZP 14 | */ 15 | @Data 16 | @NoArgsConstructor 17 | @AllArgsConstructor 18 | public class Payment implements Serializable { 19 | private long id; 20 | private String serial; 21 | } 22 | -------------------------------------------------------------------------------- /cloud-config-center-3344/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-config-center-3344 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-bus-amqp 19 | 20 | 21 | 22 | org.springframework.cloud 23 | spring-cloud-config-server 24 | 25 | 26 | 27 | org.springframework.cloud 28 | spring-cloud-starter-netflix-eureka-client 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-web 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-actuator 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-devtools 44 | runtime 45 | true 46 | 47 | 48 | 49 | org.projectlombok 50 | lombok 51 | 1.16.18 52 | true 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-starter-test 58 | test 59 | 60 | 61 | -------------------------------------------------------------------------------- /cloud-config-center-3344/src/main/java/cn/hzp/springcloud/ConfigCenterMain3344.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.config.server.EnableConfigServer; 6 | 7 | /** 8 | * @Classname ConfigCenterMain3344 9 | * @Description TODO 10 | * @Date 2020/3/16 11:21 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableConfigServer 15 | public class ConfigCenterMain3344 { 16 | public static void main(String[] args) { 17 | SpringApplication.run(ConfigCenterMain3344.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-config-center-3344/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3344 3 | spring: 4 | application: 5 | name: cloud-config-center 6 | cloud: 7 | config: 8 | server: 9 | git: 10 | uri: https://github.com/HZzzzzzP/springcloud-config.git 11 | search-paths: 12 | - springcloud-config 13 | label: master 14 | rabbitmq: #mq相关配置 15 | host: localhost 16 | port: 5672 17 | username: guest 18 | password: guest 19 | eureka: 20 | client: 21 | register-with-eureka: true 22 | fetch-registry: true 23 | service-url: 24 | # defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka 25 | defaultZone: http://eureka7001.cn:7001/eureka 26 | # rabbitmq相关配置,暴露bus刷新点 27 | management: 28 | endpoints: #暴露bus刷新配置的端点 29 | web: 30 | exposure: 31 | include: 'bus-refresh' -------------------------------------------------------------------------------- /cloud-config-client-3355/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-config-client-3355 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-bus-amqp 19 | 20 | 21 | 22 | org.springframework.cloud 23 | spring-cloud-starter-config 24 | 25 | 26 | 27 | org.springframework.cloud 28 | spring-cloud-starter-netflix-eureka-client 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-web 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-actuator 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-devtools 44 | runtime 45 | true 46 | 47 | 48 | 49 | org.projectlombok 50 | lombok 51 | 1.16.18 52 | true 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-starter-test 58 | test 59 | 60 | 61 | -------------------------------------------------------------------------------- /cloud-config-client-3355/src/main/java/cn/hzp/springcloud/ConfigClientMain3355.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | /** 4 | * @Classname ConfigClientMain3355 5 | * @Description TODO 6 | * @Date 2020/3/16 11:34 7 | * @Created by HuZP 8 | */ 9 | public class ConfigClientMain3355 { 10 | } 11 | -------------------------------------------------------------------------------- /cloud-config-client-3355/src/main/java/cn/hzp/springcloud/controller/ConfigClientController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.cloud.context.config.annotation.RefreshScope; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @Classname ConfigClientController 10 | * @Description TODO 11 | * @Date 2020/3/16 11:35 12 | * @Created by HuZP 13 | */ 14 | @RestController 15 | //实现刷新功能 16 | @RefreshScope 17 | public class ConfigClientController { 18 | 19 | @Value("${config.info}") 20 | private String configInfo; 21 | 22 | @GetMapping("/configInfo") 23 | public String getConfigInfo(){ 24 | return configInfo; 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /cloud-config-client-3355/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3355 3 | spring: 4 | application: 5 | name: config-client 6 | cloud: 7 | config: 8 | label: master #分支名称 9 | name: config #配置文件名称 10 | profile: dev #读取后缀文件名,即 master分支config-dev.yml 11 | uri: http://localhost:3344 #配置中心地址 12 | rabbitmq: #mq相关配置 13 | host: localhost 14 | port: 5672 15 | username: guest 16 | password: guest 17 | eureka: 18 | client: 19 | register-with-eureka: true 20 | fetch-registry: true 21 | service-url: 22 | # defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka 23 | defaultZone: http://eureka7001.cn:7001/eureka 24 | #暴露监控端点 25 | management: 26 | endpoints: 27 | web: 28 | exposure: 29 | include: "*" 30 | -------------------------------------------------------------------------------- /cloud-config-client-3366/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-config-client-3366 13 | 14 | 15 | 16 | 17 | 18 | org.springframework.cloud 19 | spring-cloud-starter-bus-amqp 20 | 21 | 22 | 23 | org.springframework.cloud 24 | spring-cloud-starter-config 25 | 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-netflix-eureka-client 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-actuator 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-devtools 45 | runtime 46 | true 47 | 48 | 49 | 50 | org.projectlombok 51 | lombok 52 | 1.16.18 53 | true 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-starter-test 59 | test 60 | 61 | 62 | -------------------------------------------------------------------------------- /cloud-config-client-3366/src/main/java/cn/hzp/springcloud/ConfigClientMain3366.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * @Classname ConfigClientMain3366 8 | * @Description TODO 9 | * @Date 2020/3/16 14:01 10 | * @Created by HuZP 11 | */ 12 | @SpringBootApplication 13 | public class ConfigClientMain3366 { 14 | public static void main(String[] args) { 15 | SpringApplication.run(ConfigClientMain3366.class); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /cloud-config-client-3366/src/main/java/cn/hzp/springcloud/controller/ConfigClientController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.cloud.context.config.annotation.RefreshScope; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @Classname ConfigClientController 10 | * @Description TODO 11 | * @Date 2020/3/16 14:02 12 | * @Created by HuZP 13 | */ 14 | @RestController 15 | //实现刷新功能 16 | @RefreshScope 17 | public class ConfigClientController { 18 | 19 | @Value("${config.info}") 20 | private String configInfo; 21 | 22 | @Value("${server.port}") 23 | private String serverPort; 24 | 25 | @GetMapping("/configInfo") 26 | public String getConfigInfo(){ 27 | return "serverPort: "+serverPort+"\t\n\nconfigInfo:"+configInfo; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /cloud-config-client-3366/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3366 3 | spring: 4 | application: 5 | name: config-client 6 | cloud: 7 | config: 8 | label: master #分支名称 9 | name: config #配置文件名称 10 | profile: dev #读取后缀文件名,即 master分支config-dev.yml 11 | uri: http://localhost:3344 #配置中心地址 12 | rabbitmq: #mq相关配置 13 | host: localhost 14 | port: 5672 15 | username: guest 16 | password: guest 17 | eureka: 18 | client: 19 | register-with-eureka: true 20 | fetch-registry: true 21 | service-url: 22 | # defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka 23 | defaultZone: http://eureka7001.cn:7001/eureka 24 | #暴露监控端点 25 | management: 26 | endpoints: 27 | web: 28 | exposure: 29 | include: "*" 30 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-consumer-feign-hystrix-order80 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-openfeign 19 | 20 | 21 | 22 | org.springframework.cloud 23 | spring-cloud-starter-netflix-hystrix 24 | 25 | 26 | 27 | org.springframework.cloud 28 | spring-cloud-starter-netflix-eureka-client 29 | 30 | 31 | cn.hzp.springcloud 32 | cloud-api-commons 33 | ${project.version} 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-actuator 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-devtools 48 | runtime 49 | true 50 | 51 | 52 | org.projectlombok 53 | lombok 54 | 1.16.18 55 | true 56 | 57 | 58 | org.springframework.boot 59 | spring-boot-starter-test 60 | test 61 | 62 | 63 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/src/main/java/cn/hzp/springcloud/OrderHystrixMain80.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 7 | import org.springframework.cloud.openfeign.EnableFeignClients; 8 | 9 | /** 10 | * @Classname OrderHystrixMain80 11 | * @Description TODO 12 | * @Date 2020/3/13 16:05 13 | * @Created by HuZP 14 | */ 15 | @SpringBootApplication 16 | @EnableEurekaClient 17 | @EnableFeignClients 18 | @EnableHystrix 19 | public class OrderHystrixMain80 { 20 | public static void main(String[] args) { 21 | SpringApplication.run(OrderHystrixMain80.class, args); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/src/main/java/cn/hzp/springcloud/controller/OrderHystrixController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hzp.springcloud.service.PaymentHystrixService; 4 | import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; 5 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import javax.annotation.Resource; 12 | 13 | /** 14 | * @Classname OrderHystrixController 15 | * @Description TODO 16 | * @Date 2020/3/13 16:05 17 | * @Created by HuZP 18 | */ 19 | @RestController 20 | @Slf4j 21 | @DefaultProperties(defaultFallback = "payment_Global_FallbackMethod") 22 | public class OrderHystrixController { 23 | @Resource 24 | private PaymentHystrixService paymentHystrixService; 25 | 26 | @GetMapping("/consumer/payment/hystrix/ok/{id}") 27 | public String paymentInfo_OK(@PathVariable("id") Integer id){ 28 | return paymentHystrixService.paymentInfo_OK(id); 29 | } 30 | /** 31 | * 超时访问 32 | * @param id 33 | * @return 34 | */ 35 | @GetMapping("/consumer/payment/hystrix/timeout/{id}") 36 | /* @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = { 37 | @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500") 38 | })*/ 39 | @HystrixCommand 40 | public String paymentInfo_TimeOut(@PathVariable("id") Integer id) { 41 | int age = 10/0; //方法前挂了,跟后面挂了两种 42 | return paymentHystrixService.paymentInfo_TimeOut(id); 43 | } 44 | 45 | public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) { 46 | return "我是消费者80,对方支付系统繁忙请10秒种后再试或者自己运行出错请检查自己,o(╥﹏╥)o"; 47 | } 48 | 49 | //下面是全局fallback方法 50 | public String payment_Global_FallbackMethod(){ 51 | return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~"; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/src/main/java/cn/hzp/springcloud/service/PaymentFallbackService.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.service; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | /** 6 | * @Classname PaymentFallbackService 7 | * @Description TODO 8 | * @Date 2020/3/13 16:06 9 | * @Created by HuZP 10 | */ 11 | @Component 12 | public class PaymentFallbackService implements PaymentHystrixService { 13 | @Override 14 | public String paymentInfo_OK(Integer id) { 15 | return "--- PaymentFallbackService fall paymentInfo_OK vack ,/(ㄒoㄒ)/~~"; 16 | } 17 | 18 | @Override 19 | public String paymentInfo_TimeOut(Integer id) { 20 | return "--- PaymentFallbackService fall paymentInfo_TimeOut, /(ㄒoㄒ)/~~"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/src/main/java/cn/hzp/springcloud/service/PaymentHystrixService.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.service; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | 8 | /** 9 | * @Classname PaymentHystrixService 10 | * @Description TODO 11 | * @Date 2020/3/13 16:06 12 | * @Created by HuZP 13 | */ 14 | @Component 15 | @FeignClient(value = "cloud-provider-hystrix-payment",fallback = PaymentFallbackService.class) 16 | public interface PaymentHystrixService { 17 | /** 18 | * 正常访问 19 | * 20 | * @param id 21 | * @return 22 | */ 23 | @GetMapping("/payment/hystrix/ok/{id}") 24 | public String paymentInfo_OK(@PathVariable("id") Integer id); 25 | /** 26 | * 超时访问 27 | * 28 | * @param id 29 | * @return 30 | */ 31 | @GetMapping("/payment/hystrix/timeout/{id}") 32 | public String paymentInfo_TimeOut(@PathVariable("id") Integer id); 33 | } 34 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 80 3 | eureka: 4 | client: 5 | register-with-eureka: false 6 | fetch-registry: true 7 | service-url: 8 | # defaultZone: http://eureka7001.cn:7001/eureka,http://eureka7002.cn:7002/eureka 9 | defaultZone: http://eureka7001.cn:7001/eureka 10 | # 设置feign客户端超时时间(OpenFeign默认支持ribbon) 11 | feign: 12 | hystrix: 13 | enabled: true -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-consumer-feign-order80 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-openfeign 19 | 20 | 21 | 22 | org.springframework.cloud 23 | spring-cloud-starter-netflix-eureka-client 24 | 25 | 26 | cn.hzp.springcloud 27 | cloud-api-commons 28 | ${project.version} 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-actuator 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-devtools 43 | runtime 44 | true 45 | 46 | 47 | org.projectlombok 48 | lombok 49 | 1.16.18 50 | true 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-starter-test 55 | test 56 | 57 | 58 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/src/main/java/cn/hzp/springcloud/OrderFeignMain80.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.openfeign.EnableFeignClients; 6 | 7 | /** 8 | * @Classname OrderFeignMain80 9 | * @Description TODO 10 | * @Date 2020/3/13 15:09 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableFeignClients 15 | public class OrderFeignMain80 { 16 | public static void main(String[] args) { 17 | SpringApplication.run(OrderFeignMain80.class, args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/src/main/java/cn/hzp/springcloud/config/FeignConfig.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.config; 2 | 3 | import feign.Logger; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * @Classname FeignConfig 9 | * @Description TODO 10 | * @Date 2020/3/13 15:07 11 | * @Created by HuZP 12 | */ 13 | @Configuration 14 | public class FeignConfig { 15 | /** 16 | * feignClient配置日志级别 17 | * 18 | * @return 19 | */ 20 | @Bean 21 | public Logger.Level feignLoggerLevel() { 22 | // 请求和响应的头信息,请求和响应的正文及元数据 23 | return Logger.Level.FULL; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/src/main/java/cn/hzp/springcloud/controller/OrderFeignController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hzp.springcloud.entities.CommonResult; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import cn.hzp.springcloud.service.PaymentFeignService; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import javax.annotation.Resource; 12 | 13 | /** 14 | * @Classname OrderFeignController 15 | * @Description TODO 16 | * @Date 2020/3/13 15:08 17 | * @Created by HuZP 18 | */ 19 | @RestController 20 | @Slf4j 21 | public class OrderFeignController { 22 | @Resource 23 | private PaymentFeignService paymentFeignService; 24 | 25 | @GetMapping(value = "/consumer/payment/get/{id}") 26 | public CommonResult getPaymentById(@PathVariable("id") Long id){ 27 | CommonResult paymentById = paymentFeignService.getPaymentById(id); 28 | return paymentById; 29 | } 30 | 31 | @GetMapping("/consumer/payment/feign/timeout") 32 | public String paymentFeignTimeout(){ 33 | //open-feign-ribbon,客户端默认等待一秒钟 34 | return paymentFeignService.paymentFeignTimeout(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/src/main/java/cn/hzp/springcloud/service/PaymentFeignService.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.service; 2 | 3 | import cn.hzp.springcloud.entities.CommonResult; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import org.springframework.cloud.openfeign.FeignClient; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | 10 | /** 11 | * @Classname PaymentFeignService 12 | * @Description TODO 13 | * @Date 2020/3/13 15:08 14 | * @Created by HuZP 15 | */ 16 | @Component 17 | @FeignClient(value = "cloud-payment-service") 18 | public interface PaymentFeignService { 19 | 20 | @GetMapping(value = "/payment/get/{id}") 21 | public CommonResult getPaymentById(@PathVariable("id") Long id); 22 | 23 | @GetMapping("/payment/feign/timeout") 24 | public String paymentFeignTimeout(); 25 | } 26 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 80 3 | eureka: 4 | client: 5 | register-with-eureka: false 6 | fetch-registry: true 7 | service-url: 8 | defaultZone: http://eureka7001.cn:7001/eureka,http://eureka7002.cn:7002/eureka 9 | # 设置feign客户端超时时间(OpenFeign默认支持ribbon) 10 | ribbon: 11 | # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间 12 | ReadTimeout: 5000 13 | # 指的是建立连接后从服务器读取到可用资源所用的时间 14 | ConnectTimeout: 5000 15 | logging: 16 | level: 17 | # feign日志以什么级别监控哪个接口 18 | cn.hzp.springcloud.service.PaymentFeignService: debug 19 | 20 | -------------------------------------------------------------------------------- /cloud-consumer-hystrix-dashboard9001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-consumer-hystrix-dashboard9001 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-netflix-hystrix-dashboard 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-actuator 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-devtools 29 | runtime 30 | true 31 | 32 | 33 | org.projectlombok 34 | lombok 35 | 1.16.18 36 | true 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | -------------------------------------------------------------------------------- /cloud-consumer-hystrix-dashboard9001/src/main/java/cn/hzp/springcloud/HystrixDashboardMain9001.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 6 | 7 | /** 8 | * @Classname HystrixDashboardMain9001 9 | * @Description TODO 10 | * @Date 2020/3/13 16:54 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableHystrixDashboard 15 | public class HystrixDashboardMain9001 { 16 | public static void main(String[] args) { 17 | SpringApplication.run(HystrixDashboardMain9001.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-consumer-hystrix-dashboard9001/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9001 -------------------------------------------------------------------------------- /cloud-consumer-order80/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-consumer-order80 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-actuator 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-devtools 29 | runtime 30 | true 31 | 32 | 33 | 34 | org.projectlombok 35 | lombok 36 | 1.16.18 37 | true 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | 47 | cn.hzp.springcloud 48 | cloud-api-commons 49 | ${project.version} 50 | 51 | 52 | 53 | org.springframework.cloud 54 | spring-cloud-starter-netflix-eureka-client 55 | 56 | 57 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/cn/hzp/myrule/MySelfRule.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.myrule; 2 | 3 | import com.netflix.loadbalancer.IRule; 4 | import com.netflix.loadbalancer.RandomRule; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | /** 9 | * @Classname MySelfRule 10 | * @Description TODO 11 | * @Date 2020/3/13 13:51 12 | * @Created by HuZP 13 | */ 14 | @Configuration 15 | public class MySelfRule { 16 | 17 | @Bean 18 | public IRule myRule(){ 19 | return new RandomRule(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/cn/hzp/springcloud/OrderMain80.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import cn.hzp.myrule.MySelfRule; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 7 | import org.springframework.cloud.netflix.ribbon.RibbonClient; 8 | 9 | /** 10 | * @Classname OrderMain80 11 | * @Description TODO 12 | * @Date 2020/3/12 14:24 13 | * @Created by HuZP 14 | */ 15 | @SpringBootApplication 16 | @EnableEurekaClient 17 | @RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class) 18 | public class OrderMain80 { 19 | public static void main(String[] args) { 20 | SpringApplication.run(OrderMain80.class, args); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/cn/hzp/springcloud/config/ApplicationContextConfig.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.config; 2 | 3 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | /** 9 | * @Classname ApplicationContextConfig 10 | * @Description TODO 11 | * @Date 2020/3/12 14:26 12 | * @Created by HuZP 13 | */ 14 | @Configuration 15 | public class ApplicationContextConfig { 16 | @Bean 17 | // @LoadBalanced 18 | public RestTemplate restTemplate(){ 19 | return new RestTemplate(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/cn/hzp/springcloud/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hzp.springcloud.entities.CommonResult; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import cn.hzp.springcloud.lb.LoadBalancer; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.cloud.client.ServiceInstance; 8 | import org.springframework.cloud.client.discovery.DiscoveryClient; 9 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.web.bind.annotation.GetMapping; 12 | import org.springframework.web.bind.annotation.PathVariable; 13 | import org.springframework.web.bind.annotation.RequestBody; 14 | import org.springframework.web.bind.annotation.RestController; 15 | import org.springframework.web.client.RestTemplate; 16 | 17 | import javax.annotation.Resource; 18 | import java.net.URI; 19 | import java.util.List; 20 | 21 | /** 22 | * @Classname OrderController 23 | * @Description TODO 24 | * @Date 2020/3/12 14:20 25 | * @Created by HuZP 26 | */ 27 | @RestController 28 | @Slf4j 29 | public class OrderController { 30 | // public static final String PARYMENT_URL="http://localhost:8001"; 31 | public static final String PARYMENT_URL="http://CLOUD-PAYMENT-SERVICE"; 32 | @Resource 33 | private LoadBalancer loadBalancer; 34 | @Resource 35 | private DiscoveryClient discoveryClient; 36 | @Resource 37 | private RestTemplate restTemplate; 38 | 39 | @GetMapping("/consumer/payment/create") 40 | public CommonResult create(Payment payment){ 41 | return restTemplate.postForObject(PARYMENT_URL+"/payment/create",payment,CommonResult.class); 42 | } 43 | 44 | @GetMapping("/consumer/payment/get/{id}") 45 | public CommonResult getPayment(@PathVariable("id") Long id){ 46 | return restTemplate.getForObject(PARYMENT_URL+"/payment/get/"+id,CommonResult.class); 47 | } 48 | 49 | @GetMapping("/consumer/payment/getForEntity/{id}") 50 | public CommonResult getPayment2(@PathVariable("id") Long id){ 51 | ResponseEntity entity = restTemplate.getForEntity(PARYMENT_URL + "/payment/get/" + id, CommonResult.class); 52 | 53 | if(entity.getStatusCode().is2xxSuccessful()){ 54 | return entity.getBody(); 55 | }else{ 56 | return new CommonResult<>(444,"操作失败"); 57 | } 58 | } 59 | 60 | @GetMapping("/consumer/payment/lb") 61 | public String getPaymentLB(){ 62 | List instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); 63 | if(instances == null || instances.size()<=0){ 64 | return null; 65 | } 66 | ServiceInstance serviceInstance = loadBalancer.instances(instances); 67 | URI uri = serviceInstance.getUri(); 68 | return restTemplate.getForObject(uri+"/payment/lb",String.class); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/cn/hzp/springcloud/lb/LoadBalancer.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.lb; 2 | 3 | import org.springframework.cloud.client.ServiceInstance; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @Classname LoadBalancer 9 | * @Description TODO 10 | * @Date 2020/3/13 14:26 11 | * @Created by HuZP 12 | */ 13 | public interface LoadBalancer { 14 | ServiceInstance instances(List serviceInstances); 15 | } 16 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/cn/hzp/springcloud/lb/MyLB.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.lb; 2 | 3 | import org.springframework.cloud.client.ServiceInstance; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | import java.util.concurrent.atomic.AtomicInteger; 8 | 9 | /** 10 | * @Classname MyLB 11 | * @Description TODO 12 | * @Date 2020/3/13 14:30 13 | * @Created by HuZP 14 | */ 15 | @Component 16 | public class MyLB implements LoadBalancer { 17 | private AtomicInteger atomicInteger = new AtomicInteger(0); 18 | 19 | public final int getAndIncrement() { 20 | int current; 21 | int next; 22 | do { 23 | current = this.atomicInteger.get(); 24 | //这个数字防止索引越界,但是一般不会超过这个数字,为了严谨性 25 | next = current >= 2147483647 ? 0 : current + 1; 26 | }while (!this.atomicInteger.compareAndSet(current,next)); 27 | System.out.println("****第几次访问,次数next"+next); 28 | return next; 29 | } 30 | 31 | @Override 32 | public ServiceInstance instances(List serviceInstances) { 33 | int index = getAndIncrement() % serviceInstances.size(); 34 | return serviceInstances.get(index); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 80 3 | spring: 4 | application: 5 | name: cloud-order-service 6 | eureka: 7 | client: 8 | #表示向注册中心注册自己 默认为true 9 | register-with-eureka: true 10 | #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 11 | fetch-registry: true 12 | service-url: 13 | # 入驻地址 14 | # defaultZone: http://localhost:7001/eureka/ 15 | #集群版 16 | defaultZone: http://eureka7001.cn:7001/eureka/,http://eureka7002.cn:7002/eureka/ 17 | #服务名称 18 | instance: 19 | instance-id: order80 20 | #访问路径显示IP地址 21 | prefer-ip-address: true -------------------------------------------------------------------------------- /cloud-consumerconsul-order80/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-consumerconsul-order80 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-consul-discovery 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-actuator 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-devtools 34 | runtime 35 | true 36 | 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | 1.16.18 42 | true 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | 52 | cn.hzp.springcloud 53 | cloud-api-commons 54 | ${project.version} 55 | 56 | 57 | -------------------------------------------------------------------------------- /cloud-consumerconsul-order80/src/main/java/cn/hzp/springcloud/OrderMainConsul80.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname OrderMainConsul80 9 | * @Description TODO 10 | * @Date 2020/3/13 10:53 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableDiscoveryClient 15 | public class OrderMainConsul80 { 16 | public static void main(String[] args) { 17 | SpringApplication.run(OrderMainConsul80.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-consumerconsul-order80/src/main/java/cn/hzp/springcloud/config/ApplicationContextConfig.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.config; 2 | 3 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | /** 9 | * @Classname ApplicationContextConfig 10 | * @Description TODO 11 | * @Date 2020/3/13 10:53 12 | * @Created by HuZP 13 | */ 14 | @Configuration 15 | public class ApplicationContextConfig { 16 | @Bean 17 | @LoadBalanced //赋予RestTemplate负载均衡的能力 18 | public RestTemplate getRestTemplate() { 19 | return new RestTemplate(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /cloud-consumerconsul-order80/src/main/java/cn/hzp/springcloud/controller/OrderConsulController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hzp.springcloud.entities.Payment; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import org.springframework.web.client.RestTemplate; 8 | 9 | import javax.annotation.Resource; 10 | 11 | /** 12 | * @Classname OrderConsulController 13 | * @Description TODO 14 | * @Date 2020/3/13 10:53 15 | * @Created by HuZP 16 | */ 17 | @RestController 18 | @Slf4j 19 | public class OrderConsulController { 20 | public static final String PAYMENT_URL = "http://consul-provider-payment"; 21 | 22 | @Resource 23 | private RestTemplate restTemplate; 24 | 25 | @GetMapping("/consumer/payment/consul") 26 | public String create(Payment payment) { 27 | return restTemplate.getForObject(PAYMENT_URL + "/payment/consul", String.class); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /cloud-consumerconsul-order80/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | #consul服务端口号 2 | server: 3 | port: 80 4 | spring: 5 | application: 6 | name: cloud-provider-order 7 | #consul注册中心地址 8 | cloud: 9 | consul: 10 | host: localhost 11 | port: 8500 12 | discovery: 13 | service-name: ${spring.application.name} 14 | #hostname: 127.0.0.1 15 | -------------------------------------------------------------------------------- /cloud-consumerzk-order80/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-consumerzk-order80 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-actuator 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-devtools 30 | runtime 31 | true 32 | 33 | 34 | 35 | org.projectlombok 36 | lombok 37 | 1.16.18 38 | true 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | 48 | cn.hzp.springcloud 49 | cloud-api-commons 50 | ${project.version} 51 | 52 | 53 | 54 | org.springframework.cloud 55 | spring-cloud-starter-zookeeper-discovery 56 | 57 | 58 | 59 | org.apache.zookeeper 60 | zookeeper 61 | 62 | 63 | 64 | 65 | org.apache.zookeeper 66 | zookeeper 67 | 3.4.14 68 | 69 | 70 | -------------------------------------------------------------------------------- /cloud-consumerzk-order80/src/main/java/cn/hzp/springcloud/OrderZKMain80.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname OrderZKMain80 9 | * @Description TODO 10 | * @Date 2020/3/13 10:24 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableDiscoveryClient 15 | public class OrderZKMain80 { 16 | public static void main(String[] args) { 17 | SpringApplication.run(OrderZKMain80.class, args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-consumerzk-order80/src/main/java/cn/hzp/springcloud/config/ApplicationContextConfig.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.config; 2 | 3 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | /** 9 | * @Classname ApplicationContextConfig 10 | * @Description TODO 11 | * @Date 2020/3/13 10:27 12 | * @Created by HuZP 13 | */ 14 | @Configuration 15 | public class ApplicationContextConfig { 16 | @Bean 17 | @LoadBalanced 18 | public RestTemplate getRestTemplate(){ 19 | return new RestTemplate(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /cloud-consumerzk-order80/src/main/java/cn/hzp/springcloud/controller/OrderZKController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hzp.springcloud.entities.Payment; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import org.springframework.web.client.RestTemplate; 8 | 9 | import javax.annotation.Resource; 10 | 11 | /** 12 | * @Classname OrderZKController 13 | * @Description TODO 14 | * @Date 2020/3/13 10:27 15 | * @Created by HuZP 16 | */ 17 | @RestController 18 | @Slf4j 19 | public class OrderZKController { 20 | public static final String PAYMENT_URL = "http://cloud-provider-payment"; 21 | 22 | @Resource 23 | private RestTemplate restTemplate; 24 | 25 | @GetMapping("/consumer/payment/zk") 26 | public String create(Payment payment) { 27 | return restTemplate.getForObject(PAYMENT_URL + "/payment/zk", String.class); 28 | } 29 | } -------------------------------------------------------------------------------- /cloud-consumerzk-order80/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 80 3 | 4 | spring: 5 | application: 6 | name: cloud-consumer-order 7 | cloud: 8 | zookeeper: 9 | connect-string: 127.0.0.1:2181 10 | -------------------------------------------------------------------------------- /cloud-eureka-server7001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-eureka-server7001 13 | 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-starter-netflix-eureka-server 18 | 19 | 20 | 21 | cn.hzp.springcloud 22 | cloud-api-commons 23 | ${project.version} 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-devtools 37 | runtime 38 | true 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /cloud-eureka-server7001/src/main/java/cn/hzp/springcloud/EurekaMain7001.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | /** 8 | * @Classname EurekaMain7001 9 | * @Description TODO 10 | * @Date 2020/3/12 15:30 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableEurekaServer 15 | public class EurekaMain7001 { 16 | public static void main(String[] args) { 17 | SpringApplication.run(EurekaMain7001.class,args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-eureka-server7001/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7001 3 | 4 | eureka: 5 | instance: 6 | hostname: eureka7001.cn #eureka服务端实例名称 7 | client: 8 | register-with-eureka: false #表示不向注册中心注册自己 9 | fetch-registry: false #false表示自己就是注册中心,我的职责就是维护服务实例,并不区检索服务 10 | service-url: 11 | # defaultZone: http://eureka7002.cn:7002/eureka/ 12 | defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 13 | server: 14 | # 关闭Eureka的自我保护机制 15 | enable-self-preservation: false 16 | # 清理间隔时间(单位毫秒,默认是60*1000) 17 | eviction-interval-timer-in-ms: 2000 -------------------------------------------------------------------------------- /cloud-eureka-server7002/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-eureka-server7002 13 | 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-starter-netflix-eureka-server 18 | 19 | 20 | 21 | cn.hzp.springcloud 22 | cloud-api-commons 23 | ${project.version} 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-devtools 37 | runtime 38 | true 39 | 40 | 41 | -------------------------------------------------------------------------------- /cloud-eureka-server7002/src/main/java/cn/hzp/springcloud/EurekaMain7002.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | /** 8 | * @Classname EurekaMain7002 9 | * @Description TODO 10 | * @Date 2020/3/12 15:52 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableEurekaServer 15 | public class EurekaMain7002 { 16 | public static void main(String[] args) { 17 | SpringApplication.run(EurekaMain7002.class, args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-eureka-server7002/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7002 3 | 4 | eureka: 5 | instance: 6 | hostname: eureka7002.cn #eureka服务端实例名称 7 | client: 8 | register-with-eureka: false #表示不向注册中心注册自己 9 | fetch-registry: false #false表示自己就是注册中心,我的职责就是维护服务实例,并不区检索服务 10 | service-url: 11 | defaultZone: http://eureka7001.cn:7001/eureka/ 12 | # 单机版 13 | # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-gateway-gateway9527 13 | 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-starter-gateway 18 | 19 | 20 | 21 | 22 | org.springframework.cloud 23 | spring-cloud-starter-netflix-eureka-client 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-actuator 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-devtools 34 | runtime 35 | true 36 | 37 | 38 | org.projectlombok 39 | lombok 40 | 1.16.18 41 | true 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/main/java/cn/hzp/springcloud/GateWayMain9527.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname GateWayMain9527 9 | * @Description TODO 10 | * @Date 2020/3/16 9:31 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableDiscoveryClient 15 | public class GateWayMain9527 { 16 | public static void main(String[] args) { 17 | SpringApplication.run(GateWayMain9527.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/main/java/cn/hzp/springcloud/config/GateWayConfig.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.config; 2 | 3 | import org.springframework.cloud.gateway.route.RouteLocator; 4 | import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | /** 9 | * @Classname GateWayConfig 10 | * @Description TODO 11 | * @Date 2020/3/16 9:32 12 | * @Created by HuZP 13 | */ 14 | @Configuration 15 | public class GateWayConfig { 16 | 17 | @Bean 18 | public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){ 19 | RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes(); 20 | routes.route("path route atguigu", 21 | r ->r.path("/guonei").uri("https://www.baidu.com")).build(); 22 | return routes.build(); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/main/java/cn/hzp/springcloud/filter/MyLogGateWayFilter.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.filter; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; 5 | import org.springframework.cloud.gateway.filter.GlobalFilter; 6 | import org.springframework.core.Ordered; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.server.reactive.ServerHttpRequest; 9 | import org.springframework.stereotype.Component; 10 | import org.springframework.web.server.ServerWebExchange; 11 | import reactor.core.publisher.Mono; 12 | 13 | import java.util.Date; 14 | 15 | /** 16 | * @Classname MyLogGateWayFilter 17 | * @Description TODO 18 | * @Date 2020/3/16 9:32 19 | * @Created by HuZP 20 | */ 21 | @Component 22 | @Slf4j 23 | public class MyLogGateWayFilter implements GlobalFilter, Ordered { 24 | 25 | @Override 26 | public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { 27 | log.info("come in global filter: {}", new Date()); 28 | 29 | ServerHttpRequest request = exchange.getRequest(); 30 | String uname = request.getQueryParams().getFirst("uname"); 31 | if (uname == null) { 32 | log.info("用户名为null,非法用户"); 33 | exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); 34 | return exchange.getResponse().setComplete(); 35 | } 36 | // 放行 37 | return chain.filter(exchange); 38 | } 39 | 40 | /** 41 | * 过滤器加载的顺序 越小,优先级别越高 42 | * 43 | * @return 44 | */ 45 | @Override 46 | public int getOrder() { 47 | return 0; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9527 3 | spring: 4 | application: 5 | name: cloud-gateway 6 | cloud: 7 | gateway: 8 | discovery: 9 | locator: 10 | enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名称进行路由 11 | routes: 12 | - id: payment_route # 路由的id,没有规定规则但要求唯一,建议配合服务名 13 | #匹配后提供服务的路由地址 14 | #uri: http://localhost:8001 15 | uri: lb://CLOUD-PAYMENT-SERVICE 16 | predicates: 17 | - Path=/payment/get/** # 断言,路径相匹配的进行路由 18 | - id: payment_route2 19 | #uri: http://localhost:8001 20 | uri: lb://CLOUD-PAYMENT-SERVICE 21 | predicates: 22 | - Path=/payment/lb/** #断言,路径相匹配的进行路由 23 | - After=2020-03-16T10:06:42.873+08:00[Asia/Shanghai] 24 | eureka: 25 | instance: 26 | hostname: cloud-gateway-service 27 | client: 28 | #表示向注册中心注册自己 默认为true 29 | register-with-eureka: true 30 | #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 31 | fetch-registry: true 32 | service-url: 33 | # 入驻地址 34 | defaultZone: http://localhost:7001/eureka/ 35 | #集群版 36 | # defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ 37 | -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/test/java/T2.java: -------------------------------------------------------------------------------- 1 | import java.time.ZonedDateTime; 2 | 3 | /** 4 | * @Classname T2 5 | * @Description TODO 6 | * @Date 2020/3/16 10:06 7 | * @Created by HuZP 8 | */ 9 | public class T2 { 10 | public static void main(String[] args) { 11 | // 默认时区 12 | ZonedDateTime now = ZonedDateTime.now(); 13 | System.out.println(now); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /cloud-provider-hystrix-payment8001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-provider-hystrix-payment8001 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-netflix-hystrix 19 | 20 | 21 | 22 | org.springframework.cloud 23 | spring-cloud-starter-netflix-eureka-client 24 | 25 | 26 | cn.hzp.springcloud 27 | cloud-api-commons 28 | ${project.version} 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-actuator 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-devtools 43 | runtime 44 | true 45 | 46 | 47 | org.projectlombok 48 | lombok 49 | 1.16.18 50 | true 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-starter-test 55 | test 56 | 57 | 58 | -------------------------------------------------------------------------------- /cloud-provider-hystrix-payment8001/src/main/java/cn/hzp/springcloud/PaymentHystrixMain8001.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.web.servlet.ServletRegistrationBean; 7 | import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; 8 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | /** 12 | * @Classname PaymentHystrixMain8001 13 | * @Description TODO 14 | * @Date 2020/3/13 15:54 15 | * @Created by HuZP 16 | */ 17 | @SpringBootApplication 18 | @EnableDiscoveryClient 19 | @EnableCircuitBreaker 20 | public class PaymentHystrixMain8001 { 21 | public static void main(String[] args) { 22 | SpringApplication.run(PaymentHystrixMain8001.class, args); 23 | } 24 | /** 25 | * 此配置是为了服务监控而配置,与服务容错本身无观,springCloud 升级之后的坑 26 | * ServletRegistrationBean因为springboot的默认路径不是/hystrix.stream 27 | * 只要在自己的项目中配置上下面的servlet即可 28 | * @return 29 | */ 30 | @Bean 31 | public ServletRegistrationBean getServlet(){ 32 | HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); 33 | ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(streamServlet); 34 | registrationBean.setLoadOnStartup(1); 35 | registrationBean.addUrlMappings("/hystrix.stream"); 36 | registrationBean.setName("HystrixMetricsStreamServlet"); 37 | return registrationBean; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /cloud-provider-hystrix-payment8001/src/main/java/cn/hzp/springcloud/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hzp.springcloud.service.PaymentService; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import javax.annotation.Resource; 11 | 12 | /** 13 | * @Classname PaymentController 14 | * @Description TODO 15 | * @Date 2020/3/13 15:38 16 | * @Created by HuZP 17 | */ 18 | @RestController 19 | @Slf4j 20 | public class PaymentController { 21 | @Resource 22 | private PaymentService paymentService; 23 | 24 | @Value("${server.port}") 25 | private String servicePort; 26 | 27 | /** 28 | * 正常访问 29 | * 30 | * @param id 31 | * @return 32 | */ 33 | @GetMapping("/payment/hystrix/ok/{id}") 34 | public String paymentInfo_OK(@PathVariable("id") Integer id) { 35 | String result = paymentService.paymentInfo_OK(id); 36 | log.info("*****result:" + result); 37 | return result; 38 | } 39 | 40 | /** 41 | * 超时访问 42 | * 43 | * @param id 44 | * @return 45 | */ 46 | @GetMapping("/payment/hystrix/timeout/{id}") 47 | public String paymentInfo_TimeOut(@PathVariable("id") Integer id) { 48 | String result = paymentService.paymentInfo_TimeOut(id); 49 | log.info("*****result:" + result); 50 | return result; 51 | 52 | } 53 | /** 54 | * 服务熔断 55 | * http://localhost:8001/payment/circuit/32 56 | * @param id 57 | * @return 58 | */ 59 | @GetMapping("/payment/circuit/{id}") 60 | public String paymentCircuitBreaker(@PathVariable("id") Integer id) { 61 | String result = paymentService.paymentCircuitBreaker(id); 62 | log.info("***result:" + result); 63 | return result; 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /cloud-provider-hystrix-payment8001/src/main/java/cn/hzp/springcloud/service/PaymentService.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.service; 2 | 3 | import cn.hutool.core.util.IdUtil; 4 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 5 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | 9 | import java.util.concurrent.TimeUnit; 10 | 11 | /** 12 | * @Classname PaymentService 13 | * @Description TODO 14 | * @Date 2020/3/13 15:38 15 | * @Created by HuZP 16 | */ 17 | @Service 18 | public class PaymentService { 19 | /** 20 | * 正常访问 21 | * 22 | * @param id 23 | * @return 24 | */ 25 | public String paymentInfo_OK(Integer id) { 26 | return "线程池:" + Thread.currentThread().getName() + " paymentInfo_OK,id:" + id + "\t" + "O(∩_∩)O哈哈~"; 27 | } 28 | 29 | /** 30 | * 超时访问 31 | * 32 | * @param id 33 | * @return 34 | */ 35 | @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = { 36 | @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") 37 | }) 38 | public String paymentInfo_TimeOut(Integer id) { 39 | int timeNumber = 3; 40 | try { TimeUnit.SECONDS.sleep(timeNumber); } catch (InterruptedException e) { e.printStackTrace();} 41 | return "线程池:" + Thread.currentThread().getName() + " paymentInfo_TimeOut,id:" + id + "\t" + 42 | "O(∩_∩)O哈哈~ 耗时(秒)"; 43 | } 44 | 45 | public String paymentInfo_TimeOutHandler(Integer id){ 46 | return "线程池:" + Thread.currentThread().getName() + " 8001系统繁忙请稍后再试!!,id:" + id + "\t"+"我哭了!!"; 47 | } 48 | 49 | //====服务熔断,上方是降级 50 | /** 51 | * 在10秒窗口期中10次请求有6次是请求失败的,断路器将起作用 52 | * @param id 53 | * @return 54 | */ 55 | @HystrixCommand( 56 | fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = { 57 | @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),// 是否开启断路器 58 | @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),// 请求次数 59 | @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),// 时间窗口期/时间范文 60 | @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")// 失败率达到多少后跳闸 61 | } 62 | ) 63 | public String paymentCircuitBreaker(@PathVariable("id") Integer id) { 64 | if (id < 0) { 65 | throw new RuntimeException("*****id不能是负数"); 66 | } 67 | String serialNumber = IdUtil.simpleUUID(); 68 | return Thread.currentThread().getName() + "\t" + "调用成功,流水号:" + serialNumber; 69 | } 70 | 71 | public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id) { 72 | return "id 不能负数,请稍后重试,o(╥﹏╥)o id:" + id; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /cloud-provider-hystrix-payment8001/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8001 3 | spring: 4 | application: 5 | name: cloud-provider-hystrix-payment 6 | eureka: 7 | client: 8 | register-with-eureka: true 9 | fetch-registry: true 10 | service-url: 11 | # defaultZone: http://eureka7001.cn:7001/eureka,http://eureka7002.cn:7002/eureka 12 | defaultZone: http://eureka7001.cn:7001/eureka 13 | 14 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-provider-payment8001 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-actuator 24 | 25 | 26 | 27 | org.mybatis.spring.boot 28 | mybatis-spring-boot-starter 29 | 1.3.0 30 | 31 | 32 | 33 | com.alibaba 34 | druid-spring-boot-starter 35 | 1.1.16 36 | 37 | 38 | 39 | mysql 40 | mysql-connector-java 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-jdbc 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-devtools 51 | runtime 52 | true 53 | 54 | 55 | 56 | org.projectlombok 57 | lombok 58 | 1.16.18 59 | true 60 | 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-test 65 | test 66 | 67 | 68 | 69 | cn.hzp.springcloud 70 | cloud-api-commons 71 | ${project.version} 72 | 73 | 74 | 75 | org.springframework.cloud 76 | spring-cloud-starter-netflix-eureka-client 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/java/cn/hzp/springcloud/PaymentMain8001.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 8 | 9 | /** 10 | * @Classname PaymentMain8001 11 | * @Description TODO 12 | * @Date 2020/3/12 11:12 13 | * @Created by HuZP 14 | */ 15 | @SpringBootApplication 16 | @EnableEurekaClient 17 | @EnableDiscoveryClient 18 | public class PaymentMain8001 { 19 | public static void main(String[] args) { 20 | SpringApplication.run(PaymentMain8001.class,args); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/java/cn/hzp/springcloud/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hzp.springcloud.entities.CommonResult; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import cn.hzp.springcloud.service.PaymentService; 6 | 7 | import com.sun.media.jfxmedia.logging.Logger; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.springframework.beans.factory.annotation.Value; 10 | import org.springframework.cloud.client.ServiceInstance; 11 | import org.springframework.cloud.client.discovery.DiscoveryClient; 12 | import org.springframework.web.bind.annotation.*; 13 | 14 | import javax.annotation.Resource; 15 | import javax.security.auth.login.LoginContext; 16 | import java.util.List; 17 | import java.util.concurrent.TimeUnit; 18 | 19 | /** 20 | * @Classname PaymentController 21 | * @Description TODO 22 | * @Date 2020/3/12 11:53 23 | * @Created by HuZP 24 | */ 25 | @RestController 26 | @Slf4j 27 | public class PaymentController { 28 | @Resource 29 | private PaymentService paymentService; 30 | @Value("${server.port}") 31 | private String serverPort; 32 | @Resource 33 | private DiscoveryClient discoveryClient; 34 | 35 | @PostMapping(value = "/payment/create") 36 | public CommonResult create(@RequestBody Payment payment) { 37 | int result = paymentService.create(payment); 38 | log.info("******插入结果" + result); 39 | 40 | if (result > 0) { 41 | return new CommonResult(200, "插入数据库成功" + serverPort, result); 42 | } else { 43 | return new CommonResult(444, "插入数据库错误", null); 44 | } 45 | } 46 | 47 | @GetMapping(value = "/payment/get/{id}") 48 | public CommonResult getPaymentById(@PathVariable("id") Long id) { 49 | Payment payment = paymentService.getPaymentById(id); 50 | log.info("******插入结果" + payment); 51 | 52 | if (payment != null) { 53 | return new CommonResult(200, "查询成功" + serverPort, payment); 54 | } else { 55 | return new CommonResult(444, "查询失败", null); 56 | } 57 | } 58 | 59 | @GetMapping(value = "/payment/discovery") 60 | public Object discovery() { 61 | List services = discoveryClient.getServices(); 62 | for (String element : services) { 63 | log.info("****element" + element); 64 | } 65 | 66 | List instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); 67 | for (ServiceInstance instance : instances) { 68 | log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getUri()+"\t"+instance.getPort()); 69 | } 70 | return this.discoveryClient; 71 | } 72 | 73 | @GetMapping("/payment/lb") 74 | public String getPaymentLB(){ 75 | return serverPort; 76 | } 77 | 78 | @GetMapping("/payment/feign/timeout") 79 | public String paymentFeignTimeout(){ 80 | try { 81 | TimeUnit.SECONDS.sleep(3); 82 | }catch (InterruptedException e){ 83 | e.printStackTrace(); 84 | } 85 | return serverPort; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/java/cn/hzp/springcloud/dao/PaymentDao.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.dao; 2 | 3 | import cn.hzp.springcloud.entities.Payment; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * @Classname PaymentDao 9 | * @Description TODO 10 | * @Date 2020/3/12 11:38 11 | * @Created by HuZP 12 | */ 13 | @Mapper 14 | public interface PaymentDao { 15 | /** 16 | * 通过ID查询单条数据 17 | * 18 | * @param id 主键 19 | * @return 实例对象 20 | */ 21 | Payment getPaymentById(@Param("id") Long id); 22 | 23 | 24 | /** 25 | * 新增数据 26 | * 27 | * @param payment 实例对象 28 | * @return 影响行数 29 | */ 30 | int create(Payment payment); 31 | } 32 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/java/cn/hzp/springcloud/service/PaymentService.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.service; 2 | 3 | import cn.hzp.springcloud.entities.Payment; 4 | import org.apache.ibatis.annotations.Param; 5 | 6 | /** 7 | * @Classname PaymentService 8 | * @Description TODO 9 | * @Date 2020/3/12 11:50 10 | * @Created by HuZP 11 | */ 12 | public interface PaymentService { 13 | /** 14 | * 通过ID查询单条数据 15 | * 16 | * @param id 主键 17 | * @return 实例对象 18 | */ 19 | Payment getPaymentById(@Param("id") Long id); 20 | 21 | 22 | /** 23 | * 新增数据 24 | * 25 | * @param payment 实例对象 26 | * @return 影响行数 27 | */ 28 | int create(Payment payment); 29 | } 30 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/java/cn/hzp/springcloud/service/impl/PaymentServiceImpl.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.service.impl; 2 | 3 | import cn.hzp.springcloud.dao.PaymentDao; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import cn.hzp.springcloud.service.PaymentService; 6 | import org.springframework.stereotype.Service; 7 | 8 | import javax.annotation.Resource; 9 | 10 | /** 11 | * @Classname PaymentServiceImpl 12 | * @Description TODO 13 | * @Date 2020/3/12 11:51 14 | * @Created by HuZP 15 | */ 16 | @Service 17 | public class PaymentServiceImpl implements PaymentService { 18 | @Resource 19 | private PaymentDao paymentDao; 20 | 21 | @Override 22 | public Payment getPaymentById(Long id) { 23 | return paymentDao.getPaymentById(id); 24 | } 25 | 26 | @Override 27 | public int create(Payment payment) { 28 | return paymentDao.create(payment); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8001 3 | 4 | spring: 5 | application: 6 | name: cloud-payment-service #服务名称 7 | datasource: 8 | type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型 9 | driver-class-name: com.mysql.cj.jdbc.Driver 10 | url: jdbc:mysql://localhost:3306/cloud2020?characterEncoding=utf8&useSSL=false&useUnicode=true&serverTimezone=UTC 11 | username: root 12 | password: root 13 | eureka: 14 | client: 15 | #表示向注册中心注册自己 默认为true 16 | register-with-eureka: true 17 | #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 18 | fetch-registry: true 19 | service-url: 20 | # 入驻地址 21 | # defaultZone: http://localhost:7001/eureka/ 22 | #集群版 23 | defaultZone: http://eureka7001.cn:7001/eureka/,http://eureka7002.cn:7002/eureka/ 24 | #服务名称 25 | instance: 26 | instance-id: payment8001 27 | #访问路径显示IP地址 28 | prefer-ip-address: true 29 | mybatis: 30 | mapper-locations: classpath:mapper/*.xml 31 | type-aliases-package: cn.hzp.springcloud.entities #所有entity别名所在包 -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/resources/mapper/PaymentMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | insert into payment(serial) values(#{serial}); 12 | 13 | 14 | 17 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-provider-payment8001 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-actuator 24 | 25 | 26 | 27 | org.mybatis.spring.boot 28 | mybatis-spring-boot-starter 29 | 1.3.0 30 | 31 | 32 | 33 | com.alibaba 34 | druid-spring-boot-starter 35 | 1.1.16 36 | 37 | 38 | 39 | mysql 40 | mysql-connector-java 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-jdbc 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-devtools 51 | runtime 52 | true 53 | 54 | 55 | 56 | org.projectlombok 57 | lombok 58 | 1.16.18 59 | true 60 | 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-test 65 | test 66 | 67 | 68 | 69 | cn.hzp.springcloud 70 | cloud-api-commons 71 | ${project.version} 72 | 73 | 74 | 75 | org.springframework.cloud 76 | spring-cloud-starter-netflix-eureka-client 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/java/cn/hzp/springcloud/PaymentMain8002.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 7 | 8 | /** 9 | * @Classname PaymentMain8001 10 | * @Description TODO 11 | * @Date 2020/3/12 11:12 12 | * @Created by HuZP 13 | */ 14 | @SpringBootApplication 15 | @EnableEurekaClient 16 | public class PaymentMain8002 { 17 | public static void main(String[] args) { 18 | SpringApplication.run(PaymentMain8002.class,args); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/java/cn/hzp/springcloud/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hzp.springcloud.entities.CommonResult; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import cn.hzp.springcloud.service.PaymentService; 6 | import com.sun.media.jfxmedia.logging.Logger; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import javax.annotation.Resource; 12 | import javax.security.auth.login.LoginContext; 13 | 14 | /** 15 | * @Classname PaymentController 16 | * @Description TODO 17 | * @Date 2020/3/12 11:53 18 | * @Created by HuZP 19 | */ 20 | @RestController 21 | @Slf4j 22 | public class PaymentController { 23 | @Resource 24 | private PaymentService paymentService; 25 | @Value("${server.port}") 26 | private String serverPort; 27 | 28 | @PostMapping(value = "/payment/create") 29 | public CommonResult create(@RequestBody Payment payment){ 30 | int result = paymentService.create(payment); 31 | log.info("******插入结果"+result); 32 | 33 | if(result>0){ 34 | return new CommonResult(200,"插入数据库成功"+serverPort,result); 35 | }else{ 36 | return new CommonResult(444,"插入数据库错误",null); 37 | } 38 | } 39 | 40 | @GetMapping(value = "/payment/get/{id}") 41 | public CommonResult getPaymentById(@PathVariable("id") Long id){ 42 | Payment payment = paymentService.getPaymentById(id); 43 | log.info("******插入结果"+payment); 44 | 45 | if(payment!=null){ 46 | return new CommonResult(200,"查询成功"+serverPort,payment); 47 | }else{ 48 | return new CommonResult(444,"查询失败",null); 49 | } 50 | } 51 | 52 | @GetMapping("/payment/lb") 53 | public String getPaymentLB(){ 54 | return serverPort; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/java/cn/hzp/springcloud/dao/PaymentDao.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.dao; 2 | 3 | import cn.hzp.springcloud.entities.Payment; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * @Classname PaymentDao 9 | * @Description TODO 10 | * @Date 2020/3/12 11:38 11 | * @Created by HuZP 12 | */ 13 | @Mapper 14 | public interface PaymentDao { 15 | /** 16 | * 通过ID查询单条数据 17 | * 18 | * @param id 主键 19 | * @return 实例对象 20 | */ 21 | Payment getPaymentById(@Param("id") Long id); 22 | 23 | 24 | /** 25 | * 新增数据 26 | * 27 | * @param payment 实例对象 28 | * @return 影响行数 29 | */ 30 | int create(Payment payment); 31 | } 32 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/java/cn/hzp/springcloud/service/PaymentService.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.service; 2 | 3 | import cn.hzp.springcloud.entities.Payment; 4 | import org.apache.ibatis.annotations.Param; 5 | 6 | /** 7 | * @Classname PaymentService 8 | * @Description TODO 9 | * @Date 2020/3/12 11:50 10 | * @Created by HuZP 11 | */ 12 | public interface PaymentService { 13 | /** 14 | * 通过ID查询单条数据 15 | * 16 | * @param id 主键 17 | * @return 实例对象 18 | */ 19 | Payment getPaymentById(@Param("id") Long id); 20 | 21 | 22 | /** 23 | * 新增数据 24 | * 25 | * @param payment 实例对象 26 | * @return 影响行数 27 | */ 28 | int create(Payment payment); 29 | } 30 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/java/cn/hzp/springcloud/service/impl/PaymentServiceImpl.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.service.impl; 2 | 3 | import cn.hzp.springcloud.dao.PaymentDao; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import cn.hzp.springcloud.service.PaymentService; 6 | import org.springframework.stereotype.Service; 7 | 8 | import javax.annotation.Resource; 9 | 10 | /** 11 | * @Classname PaymentServiceImpl 12 | * @Description TODO 13 | * @Date 2020/3/12 11:51 14 | * @Created by HuZP 15 | */ 16 | @Service 17 | public class PaymentServiceImpl implements PaymentService { 18 | @Resource 19 | private PaymentDao paymentDao; 20 | 21 | @Override 22 | public Payment getPaymentById(Long id) { 23 | return paymentDao.getPaymentById(id); 24 | } 25 | 26 | @Override 27 | public int create(Payment payment) { 28 | return paymentDao.create(payment); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8002 3 | 4 | spring: 5 | application: 6 | name: cloud-payment-service #服务名称 7 | datasource: 8 | type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型 9 | driver-class-name: com.mysql.cj.jdbc.Driver 10 | url: jdbc:mysql://localhost:3306/cloud2020?characterEncoding=utf8&useSSL=false&useUnicode=true&serverTimezone=UTC 11 | username: root 12 | password: root 13 | eureka: 14 | client: 15 | #表示向注册中心注册自己 默认为true 16 | register-with-eureka: true 17 | #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 18 | fetch-registry: true 19 | service-url: 20 | # 入驻地址 21 | # defaultZone: http://localhost:7001/eureka/ 22 | #集群版 23 | defaultZone: http://eureka7001.cn:7001/eureka/,http://eureka7002.cn:7002/eureka/ 24 | #服务名称 25 | instance: 26 | instance-id: payment8002 27 | #访问路径显示IP地址 28 | prefer-ip-address: true 29 | # Eureka客户端向服务端发送心跳的时间间隔,单位秒,默认30 30 | lease-renewal-interval-in-seconds: 1 31 | # Eureka服务端在收到最后一次心跳后的等待时间上限,单位秒,默认90 32 | lease-expiration-duration-in-seconds: 2 33 | mybatis: 34 | mapper-locations: classpath:mapper/*.xml 35 | type-aliases-package: cn.hzp.springcloud.entities #所有entity别名所在包 -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/resources/mapper/PaymentMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | insert into payment(serial) values(#{serial}); 12 | 13 | 14 | 17 | -------------------------------------------------------------------------------- /cloud-provider-payment8004/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-provider-payment8004 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-actuator 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-devtools 30 | runtime 31 | true 32 | 33 | 34 | 35 | org.projectlombok 36 | lombok 37 | 1.16.18 38 | true 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | 48 | cn.hzp.springcloud 49 | cloud-api-commons 50 | ${project.version} 51 | 52 | 53 | 54 | org.springframework.cloud 55 | spring-cloud-starter-zookeeper-discovery 56 | 57 | 58 | 59 | org.apache.zookeeper 60 | zookeeper 61 | 62 | 63 | 64 | 65 | org.apache.zookeeper 66 | zookeeper 67 | 3.6.0 68 | 69 | 70 | -------------------------------------------------------------------------------- /cloud-provider-payment8004/src/main/java/cn/hzp/springcloud/PaymentMain8004.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname PaymentMain8004 9 | * @Description TODO 10 | * @Date 2020/3/13 9:48 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableDiscoveryClient 15 | public class PaymentMain8004 { 16 | public static void main(String[] args) { 17 | SpringApplication.run(PaymentMain8004.class, args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-provider-payment8004/src/main/java/cn/hzp/springcloud/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hutool.core.lang.UUID; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @Classname PaymentController 11 | * @Description TODO 12 | * @Date 2020/3/13 9:49 13 | * @Created by HuZP 14 | */ 15 | @RestController 16 | @Slf4j 17 | public class PaymentController { 18 | @Value("${server.port}") 19 | private String serverPort; 20 | 21 | @RequestMapping(value = "/payment/zk") 22 | public String payment(){ 23 | return "SpringCloud with zookeeper: "+serverPort+"\t"+ UUID.randomUUID().toString(); 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /cloud-provider-payment8004/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8004 3 | 4 | spring: 5 | application: 6 | name: cloud-provider-payment 7 | cloud: 8 | zookeeper: 9 | connect-string: 127.0.0.1:2181 10 | -------------------------------------------------------------------------------- /cloud-providerconsul-payment8006/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-providerconsul-payment8006 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-consul-discovery 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-actuator 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-devtools 34 | runtime 35 | true 36 | 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | 1.16.18 42 | true 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | 52 | cn.hzp.springcloud 53 | cloud-api-commons 54 | ${project.version} 55 | 56 | 57 | -------------------------------------------------------------------------------- /cloud-providerconsul-payment8006/src/main/java/cn/hzp/springcloud/PaymentMain8006.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname PaymentMain8006 9 | * @Description TODO 10 | * @Date 2020/3/13 10:49 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableDiscoveryClient 15 | public class PaymentMain8006 { 16 | public static void main(String[] args) { 17 | SpringApplication.run(PaymentMain8006.class, args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-providerconsul-payment8006/src/main/java/cn/hzp/springcloud/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hutool.core.lang.UUID; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @Classname PaymentController 11 | * @Description TODO 12 | * @Date 2020/3/13 10:50 13 | * @Created by HuZP 14 | */ 15 | @RestController 16 | @Slf4j 17 | public class PaymentController { 18 | @Value("${server.port}") 19 | private String serverPort; 20 | 21 | @RequestMapping(value = "/payment/consul") 22 | public String payment(){ 23 | return "SpringCloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString(); 24 | } 25 | } -------------------------------------------------------------------------------- /cloud-providerconsul-payment8006/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | #consul服务端口号 2 | server: 3 | port: 8006 4 | spring: 5 | application: 6 | name: consul-provider-payment 7 | #consul注册中心地址 8 | cloud: 9 | consul: 10 | host: localhost 11 | port: 8500 12 | discovery: 13 | service-name: ${spring.application.name} 14 | #hostname: 127.0.0.1 15 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8802/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-stream-rabbitmq-consumer8802 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | 19 | 20 | org.springframework.cloud 21 | spring-cloud-starter-netflix-eureka-client 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-stream-rabbit 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-actuator 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-devtools 35 | runtime 36 | true 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | 1.16.18 42 | true 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-test 47 | test 48 | 49 | 50 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8802/src/main/java/cn/hzp/springcloud/StreamMQMain8802.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * @Classname StreamMQMain8802 8 | * @Description TODO 9 | * @Date 2020/3/16 15:35 10 | * @Created by HuZP 11 | */ 12 | @SpringBootApplication 13 | public class StreamMQMain8802 14 | { 15 | public static void main(String[] args) 16 | { 17 | SpringApplication.run(StreamMQMain8802.class,args); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8802/src/main/java/cn/hzp/springcloud/controller/ReceiveMessageListenerController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.cloud.stream.annotation.EnableBinding; 5 | import org.springframework.cloud.stream.annotation.StreamListener; 6 | import org.springframework.cloud.stream.messaging.Sink; 7 | import org.springframework.messaging.Message; 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * @Classname ReceiveMessageListenerController 12 | * @Description TODO 13 | * @Date 2020/3/16 15:36 14 | * @Created by HuZP 15 | */ 16 | @Component 17 | @EnableBinding(Sink.class) 18 | public class ReceiveMessageListenerController 19 | { 20 | @Value("${server.port}") 21 | private String serverPort; 22 | 23 | 24 | @StreamListener(Sink.INPUT) 25 | public void input(Message message) 26 | { 27 | System.out.println("消费者1号,----->接受到的消息: "+message.getPayload()+"\t port: "+serverPort); 28 | } 29 | } -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8802/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8802 3 | 4 | spring: 5 | application: 6 | name: cloud-stream-consumer 7 | cloud: 8 | stream: 9 | binders: # 在此处配置要绑定的rabbitmq的服务信息; 10 | defaultRabbit: # 表示定义的名称,用于于binding整合 11 | type: rabbit # 消息组件类型 12 | environment: # 设置rabbitmq的相关的环境配置 13 | spring: 14 | rabbitmq: 15 | host: localhost 16 | port: 5672 17 | username: guest 18 | password: guest 19 | bindings: # 服务的整合处理 20 | input: # 这个名字是一个通道的名称 21 | destination: studyExchange # 表示要使用的Exchange名称定义 22 | content-type: application/json # 设置消息类型,本次为对象json,如果是文本则设置“text/plain” 23 | binder: defaultRabbit # 设置要绑定的消息服务的具体设置 24 | group: hzpA 25 | 26 | 27 | 28 | eureka: 29 | client: # 客户端进行Eureka注册的配置 30 | service-url: 31 | defaultZone: http://localhost:7001/eureka 32 | instance: 33 | lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒) 34 | lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒) 35 | instance-id: receive-8802.com # 在信息列表时显示主机名称 36 | prefer-ip-address: true # 访问的路径变为IP地址 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8803/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-stream-rabbitmq-consumer8803 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | 19 | 20 | org.springframework.cloud 21 | spring-cloud-starter-netflix-eureka-client 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-stream-rabbit 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-actuator 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-devtools 35 | runtime 36 | true 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | 1.16.18 42 | true 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-test 47 | test 48 | 49 | 50 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8803/src/main/java/cn/hzp/springcloud/StreamMQMain8803.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * @Classname StreamMQMain8803 8 | * @Description TODO 9 | * @Date 2020/3/16 15:45 10 | * @Created by HuZP 11 | */ 12 | @SpringBootApplication 13 | public class StreamMQMain8803 14 | { 15 | public static void main(String[] args) 16 | { 17 | SpringApplication.run(StreamMQMain8803.class,args); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8803/src/main/java/cn/hzp/springcloud/controller/ReceiveMessageListenerController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.cloud.stream.annotation.EnableBinding; 5 | import org.springframework.cloud.stream.annotation.StreamListener; 6 | import org.springframework.cloud.stream.messaging.Sink; 7 | import org.springframework.messaging.Message; 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * @Classname ReceiveMessageListenerController 12 | * @Description TODO 13 | * @Date 2020/3/16 15:45 14 | * @Created by HuZP 15 | */ 16 | @Component 17 | @EnableBinding(Sink.class) 18 | public class ReceiveMessageListenerController 19 | { 20 | @Value("${server.port}") 21 | private String serverPort; 22 | 23 | 24 | @StreamListener(Sink.INPUT) 25 | public void input(Message message) 26 | { 27 | System.out.println("消费者2号,----->接受到的消息: "+message.getPayload()+"\t port: "+serverPort); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8803/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8803 3 | 4 | spring: 5 | application: 6 | name: cloud-stream-consumer 7 | cloud: 8 | stream: 9 | binders: # 在此处配置要绑定的rabbitmq的服务信息; 10 | defaultRabbit: # 表示定义的名称,用于于binding整合 11 | type: rabbit # 消息组件类型 12 | environment: # 设置rabbitmq的相关的环境配置 13 | spring: 14 | rabbitmq: 15 | host: localhost 16 | port: 5672 17 | username: guest 18 | password: guest 19 | bindings: # 服务的整合处理 20 | input: # 这个名字是一个通道的名称 21 | destination: studyExchange # 表示要使用的Exchange名称定义 22 | content-type: application/json # 设置消息类型,本次为对象json,如果是文本则设置“text/plain” 23 | binder: defaultRabbit # 设置要绑定的消息服务的具体设置 24 | group: hzpA 25 | 26 | eureka: 27 | client: # 客户端进行Eureka注册的配置 28 | service-url: 29 | defaultZone: http://localhost:7001/eureka 30 | instance: 31 | lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒) 32 | lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒) 33 | instance-id: receive-8803.com # 在信息列表时显示主机名称 34 | prefer-ip-address: true # 访问的路径变为IP地址 35 | 36 | 37 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-stream-rabbitmq-provider8801 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-actuator 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-client 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-starter-stream-rabbit 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-devtools 36 | runtime 37 | true 38 | 39 | 40 | org.projectlombok 41 | lombok 42 | 1.16.18 43 | true 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/src/main/java/cn/hzp/springcloud/StreamMQMain8801.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * @Classname StreamMQMain8801 8 | * @Description TODO 9 | * @Date 2020/3/16 15:31 10 | * @Created by HuZP 11 | */ 12 | @SpringBootApplication 13 | public class StreamMQMain8801 14 | { 15 | public static void main(String[] args) 16 | { 17 | SpringApplication.run(StreamMQMain8801.class,args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/src/main/java/cn/hzp/springcloud/controller/SendMessageController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.controller; 2 | 3 | import cn.hzp.springcloud.service.IMessageProvider; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import javax.annotation.Resource; 8 | 9 | /** 10 | * @Classname SendMessageController 11 | * @Description TODO 12 | * @Date 2020/3/16 15:31 13 | * @Created by HuZP 14 | */ 15 | @RestController 16 | public class SendMessageController 17 | { 18 | @Resource 19 | private IMessageProvider messageProvider; 20 | 21 | @GetMapping(value = "/sendMessage") 22 | public String sendMessage() 23 | { 24 | return messageProvider.send(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/src/main/java/cn/hzp/springcloud/service/IMessageProvider.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.service; 2 | 3 | /** 4 | * @Classname IMessageProvider 5 | * @Description TODO 6 | * @Date 2020/3/16 15:32 7 | * @Created by HuZP 8 | */ 9 | public interface IMessageProvider 10 | { 11 | String send(); 12 | } 13 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/src/main/java/cn/hzp/springcloud/service/impl/MessageProviderImpl.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.service.impl; 2 | 3 | import cn.hzp.springcloud.service.IMessageProvider; 4 | import org.springframework.cloud.stream.annotation.EnableBinding; 5 | import org.springframework.messaging.MessageChannel; 6 | import org.springframework.integration.support.MessageBuilder; 7 | import javax.annotation.Resource; 8 | import org.springframework.cloud.stream.messaging.Source; 9 | 10 | import java.util.UUID; 11 | 12 | /** 13 | * @Classname MessageProviderImpl 14 | * @Description TODO 15 | * @Date 2020/3/16 15:33 16 | * @Created by HuZP 17 | */ 18 | @EnableBinding(Source.class) //定义消息的推送管道 19 | public class MessageProviderImpl implements IMessageProvider 20 | { 21 | @Resource 22 | private MessageChannel output; // 消息发送管道 23 | 24 | @Override 25 | public String send() 26 | { 27 | String serial = UUID.randomUUID().toString(); 28 | output.send(MessageBuilder.withPayload(serial).build()); 29 | System.out.println("*****serial: "+serial); 30 | return null; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8801 3 | 4 | spring: 5 | application: 6 | name: cloud-stream-provider 7 | cloud: 8 | stream: 9 | binders: # 在此处配置要绑定的rabbitmq的服务信息; 10 | defaultRabbit: # 表示定义的名称,用于于binding整合 11 | type: rabbit # 消息组件类型 12 | environment: # 设置rabbitmq的相关的环境配置 13 | spring: 14 | rabbitmq: 15 | host: localhost 16 | port: 5672 17 | username: guest 18 | password: guest 19 | bindings: # 服务的整合处理 20 | output: # 这个名字是一个通道的名称 21 | destination: studyExchange # 表示要使用的Exchange名称定义 22 | content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain” 23 | binder: defaultRabbit # 设置要绑定的消息服务的具体设置 24 | 25 | eureka: 26 | client: # 客户端进行Eureka注册的配置 27 | service-url: 28 | defaultZone: http://localhost:7001/eureka 29 | instance: 30 | lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒) 31 | lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒) 32 | instance-id: send-8801.com # 在信息列表时显示主机名称 33 | prefer-ip-address: true # 访问的路径变为IP地址 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /cloudalibaba-config-nacos-client3377/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloudalibaba-config-nacos-client3377 13 | 14 | 15 | 16 | 17 | com.alibaba.cloud 18 | spring-cloud-starter-alibaba-nacos-config 19 | 20 | 21 | 22 | com.alibaba.cloud 23 | spring-cloud-starter-alibaba-nacos-discovery 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-devtools 38 | runtime 39 | true 40 | 41 | 42 | org.projectlombok 43 | lombok 44 | 1.16.18 45 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | -------------------------------------------------------------------------------- /cloudalibaba-config-nacos-client3377/src/main/java/cn/hzp/springcloud/alibaba/NacosConfigClientMain3377.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname NacosConfigClientMain3377 9 | * @Description TODO 10 | * @Date 2020/3/17 15:01 11 | * @Created by HuZP 12 | */ 13 | @EnableDiscoveryClient 14 | @SpringBootApplication 15 | public class NacosConfigClientMain3377 16 | { 17 | public static void main(String[] args) { 18 | SpringApplication.run(NacosConfigClientMain3377.class, args); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloudalibaba-config-nacos-client3377/src/main/java/cn/hzp/springcloud/alibaba/controller/ConfigClientController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.cloud.context.config.annotation.RefreshScope; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @Classname ConfigClientController 10 | * @Description TODO 11 | * @Date 2020/3/17 15:02 12 | * @Created by HuZP 13 | */ 14 | @RestController 15 | @RefreshScope //支持Nacos的动态刷新功能。 16 | public class ConfigClientController 17 | { 18 | @Value("${config.info}") 19 | private String configInfo; 20 | 21 | @GetMapping("/config/info") 22 | public String getConfigInfo() { 23 | return configInfo; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /cloudalibaba-config-nacos-client3377/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev # 表示开发环境 4 | #active: test # 表示测试环境 5 | # active: info -------------------------------------------------------------------------------- /cloudalibaba-config-nacos-client3377/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | # nacos配置 2 | server: 3 | port: 3377 4 | 5 | spring: 6 | application: 7 | name: nacos-config-client 8 | cloud: 9 | nacos: 10 | discovery: 11 | server-addr: localhost:8848 #Nacos服务注册中心地址 12 | config: 13 | server-addr: localhost:8848 #Nacos作为配置中心地址 14 | file-extension: yaml #指定yaml格式的配置 15 | group: DEV_GROUP 16 | namespace: c6ba8d59-65ec-4fb7-88ed-22dddd6069a5 17 | 18 | # ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension} 19 | # nacos-config-client-dev.yaml 20 | 21 | # nacos-config-client-test.yaml ----> config.info -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order83/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloudalibaba-consumer-nacos-order83 13 | 14 | 15 | 16 | 17 | com.alibaba.cloud 18 | spring-cloud-starter-alibaba-nacos-discovery 19 | 20 | 21 | 22 | com.atguigu.springcloud 23 | cloud-api-commons 24 | ${project.version} 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-actuator 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-devtools 39 | runtime 40 | true 41 | 42 | 43 | org.projectlombok 44 | lombok 45 | 1.16.18 46 | true 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-test 51 | test 52 | 53 | 54 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order83/src/main/java/cn/hzp/springcloud/alibaba/OrderNacosMain83.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname OrderNacosMain83 9 | * @Description TODO 10 | * @Date 2020/3/17 14:33 11 | * @Created by HuZP 12 | */ 13 | @EnableDiscoveryClient 14 | @SpringBootApplication 15 | public class OrderNacosMain83 16 | { 17 | public static void main(String[] args) 18 | { 19 | SpringApplication.run(OrderNacosMain83.class,args); 20 | } 21 | } -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order83/src/main/java/cn/hzp/springcloud/alibaba/config/ApplicationContextConfig.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.config; 2 | 3 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | /** 9 | * @Classname ApplicationContextConfig 10 | * @Description TODO 11 | * @Date 2020/3/17 14:33 12 | * @Created by HuZP 13 | */ 14 | @Configuration 15 | public class ApplicationContextConfig 16 | { 17 | @Bean 18 | @LoadBalanced 19 | public RestTemplate getRestTemplate() 20 | { 21 | return new RestTemplate(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order83/src/main/java/cn/hzp/springcloud/alibaba/controller/OrderNacosController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RestController; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | import javax.annotation.Resource; 11 | 12 | /** 13 | * @Classname OrderNacosController 14 | * @Description TODO 15 | * @Date 2020/3/17 14:34 16 | * @Created by HuZP 17 | */ 18 | @RestController 19 | @Slf4j 20 | public class OrderNacosController 21 | { 22 | @Resource 23 | private RestTemplate restTemplate; 24 | 25 | @Value("${service-url.nacos-user-service}") 26 | private String serverURL; 27 | 28 | @GetMapping(value = "/consumer/payment/nacos/{id}") 29 | public String paymentInfo(@PathVariable("id") Long id) 30 | { 31 | return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order83/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 83 3 | 4 | 5 | spring: 6 | application: 7 | name: nacos-order-consumer 8 | cloud: 9 | nacos: 10 | discovery: 11 | server-addr: localhost:8848 12 | 13 | 14 | #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者) 15 | service-url: 16 | nacos-user-service: http://nacos-payment-provider 17 | 18 | 19 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloudalibaba-consumer-nacos-order84 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-openfeign 19 | 20 | 21 | 22 | com.alibaba.cloud 23 | spring-cloud-starter-alibaba-nacos-discovery 24 | 25 | 26 | 27 | com.alibaba.cloud 28 | spring-cloud-starter-alibaba-sentinel 29 | 30 | 31 | 32 | cn.hzp.springcloud 33 | cloud-api-commons 34 | ${project.version} 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-web 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-actuator 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-devtools 49 | runtime 50 | true 51 | 52 | 53 | org.projectlombok 54 | lombok 55 | 1.16.18 56 | true 57 | 58 | 59 | org.springframework.boot 60 | spring-boot-starter-test 61 | test 62 | 63 | 64 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/java/cn/hzp/springcloud/alibaba/OrderNacosMain84.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.openfeign.EnableFeignClients; 7 | 8 | /** 9 | * @Classname OrderNacosMain84 10 | * @Description TODO 11 | * @Date 2020/3/19 16:54 12 | * @Created by HuZP 13 | */ 14 | @EnableDiscoveryClient 15 | @SpringBootApplication 16 | @EnableFeignClients 17 | public class OrderNacosMain84 18 | { 19 | public static void main(String[] args) { 20 | SpringApplication.run(OrderNacosMain84.class, args); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/java/cn/hzp/springcloud/alibaba/config/ApplicationContextConfig.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.config; 2 | 3 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | /** 9 | * @Classname ApplicationContextConfig 10 | * @Description TODO 11 | * @Date 2020/3/19 16:54 12 | * @Created by HuZP 13 | */ 14 | @Configuration 15 | public class ApplicationContextConfig 16 | { 17 | @Bean 18 | @LoadBalanced 19 | public RestTemplate getRestTemplate() 20 | { 21 | return new RestTemplate(); 22 | } 23 | } -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/java/cn/hzp/springcloud/alibaba/controller/CircleBreakerController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.controller; 2 | 3 | import cn.hzp.springcloud.alibaba.service.PaymentService; 4 | import cn.hzp.springcloud.entities.CommonResult; 5 | import cn.hzp.springcloud.entities.Payment; 6 | import com.alibaba.csp.sentinel.annotation.SentinelResource; 7 | import com.alibaba.csp.sentinel.slots.block.BlockException; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.PathVariable; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.RestController; 13 | import org.springframework.web.client.RestTemplate; 14 | 15 | import javax.annotation.Resource; 16 | 17 | /** 18 | * @Classname CircleBreakerController 19 | * @Description TODO 20 | * @Date 2020/3/19 16:55 21 | * @Created by HuZP 22 | */ 23 | @RestController 24 | @Slf4j 25 | public class CircleBreakerController 26 | { 27 | public static final String SERVICE_URL = "http://nacos-payment-provider"; 28 | 29 | @Resource 30 | private RestTemplate restTemplate; 31 | 32 | @RequestMapping("/consumer/fallback/{id}") 33 | //@SentinelResource(value = "fallback") //没有配置 34 | //@SentinelResource(value = "fallback",fallback = "handlerFallback") //fallback只负责业务异常 35 | //@SentinelResource(value = "fallback",blockHandler = "blockHandler") //blockHandler只负责sentinel控制台配置违规 36 | @SentinelResource(value = "fallback",fallback = "handlerFallback",blockHandler = "blockHandler", 37 | exceptionsToIgnore = {IllegalArgumentException.class}) 38 | public CommonResult fallback(@PathVariable Long id) 39 | { 40 | CommonResult result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id); 41 | 42 | if (id == 4) { 43 | throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常...."); 44 | }else if (result.getData() == null) { 45 | throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常"); 46 | } 47 | 48 | return result; 49 | } 50 | //本例是fallback 51 | public CommonResult handlerFallback(@PathVariable Long id, Throwable e) { 52 | Payment payment = new Payment(id,"null"); 53 | return new CommonResult<>(444,"兜底异常handlerFallback,exception内容 "+e.getMessage(),payment); 54 | } 55 | //本例是blockHandler 56 | public CommonResult blockHandler(@PathVariable Long id, BlockException blockException) { 57 | Payment payment = new Payment(id,"null"); 58 | return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException "+blockException.getMessage(),payment); 59 | } 60 | 61 | //==================OpenFeign 62 | @Resource 63 | private PaymentService paymentService; 64 | 65 | @GetMapping(value = "/consumer/paymentSQL/{id}") 66 | public CommonResult paymentSQL(@PathVariable("id") Long id) 67 | { 68 | return paymentService.paymentSQL(id); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/java/cn/hzp/springcloud/alibaba/service/PaymentFallbackService.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.service; 2 | 3 | import cn.hzp.springcloud.entities.CommonResult; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @Classname PaymentFallbackService 9 | * @Description TODO 10 | * @Date 2020/3/19 16:56 11 | * @Created by HuZP 12 | */ 13 | @Component 14 | public class PaymentFallbackService implements PaymentService 15 | { 16 | @Override 17 | public CommonResult paymentSQL(Long id) 18 | { 19 | return new CommonResult<>(444,"服务降级返回,---PaymentFallbackService",new Payment(id,"errorSerial")); 20 | } 21 | } -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/java/cn/hzp/springcloud/alibaba/service/PaymentService.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.service; 2 | 3 | import cn.hzp.springcloud.entities.CommonResult; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import org.springframework.cloud.openfeign.FeignClient; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | 9 | /** 10 | * @Classname PaymentService 11 | * @Description TODO 12 | * @Date 2020/3/19 16:56 13 | * @Created by HuZP 14 | */ 15 | @FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class) 16 | public interface PaymentService 17 | { 18 | @GetMapping(value = "/paymentSQL/{id}") 19 | public CommonResult paymentSQL(@PathVariable("id") Long id); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 84 3 | 4 | 5 | spring: 6 | application: 7 | name: nacos-order-consumer 8 | cloud: 9 | nacos: 10 | discovery: 11 | server-addr: localhost:8848 12 | sentinel: 13 | transport: 14 | #配置Sentinel dashboard地址 15 | dashboard: localhost:8080 16 | #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口 17 | port: 8719 18 | 19 | #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者) 20 | service-url: 21 | nacos-user-service: http://nacos-payment-provider 22 | 23 | # 激活Sentinel对Feign的支持 24 | feign: 25 | sentinel: 26 | enabled: true -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloudalibaba-provider-payment9001 13 | 14 | 15 | 16 | 17 | com.alibaba.cloud 18 | spring-cloud-starter-alibaba-nacos-discovery 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-actuator 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-devtools 33 | runtime 34 | true 35 | 36 | 37 | org.projectlombok 38 | lombok 39 | 1.16.18 40 | true 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9001/src/main/java/cn/hzp/springcloud/alibaba/PaymentMain9001.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname PaymentMain9001 9 | * @Description TODO 10 | * @Date 2020/3/17 14:00 11 | * @Created by HuZP 12 | */ 13 | @EnableDiscoveryClient 14 | @SpringBootApplication 15 | public class PaymentMain9001 16 | { 17 | public static void main(String[] args) { 18 | SpringApplication.run(PaymentMain9001.class, args); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9001/src/main/java/cn/hzp/springcloud/alibaba/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @Classname PaymentController 10 | * @Description TODO 11 | * @Date 2020/3/17 14:01 12 | * @Created by HuZP 13 | */ 14 | @RestController 15 | public class PaymentController 16 | { 17 | @Value("${server.port}") 18 | private String serverPort; 19 | 20 | @GetMapping(value = "/payment/nacos/{id}") 21 | public String getPayment(@PathVariable("id") Integer id) 22 | { 23 | return "nacos registry, serverPort: "+ serverPort+"\t id"+id; 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9001/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9001 3 | 4 | spring: 5 | application: 6 | name: nacos-payment-provider 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 #配置Nacos地址 11 | 12 | management: 13 | endpoints: 14 | web: 15 | exposure: 16 | include: '*' -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9002/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloudalibaba-provider-payment9002 13 | 14 | 15 | 16 | 17 | com.alibaba.cloud 18 | spring-cloud-starter-alibaba-nacos-discovery 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-actuator 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-devtools 33 | runtime 34 | true 35 | 36 | 37 | org.projectlombok 38 | lombok 39 | 1.16.18 40 | true 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9002/src/main/java/cn/hzp/springcloud/alibaba/PaymentMain9002.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname PaymentMain9002 9 | * @Description TODO 10 | * @Date 2020/3/17 14:26 11 | * @Created by HuZP 12 | */ 13 | @EnableDiscoveryClient 14 | @SpringBootApplication 15 | public class PaymentMain9002 16 | { 17 | public static void main(String[] args) { 18 | SpringApplication.run(PaymentMain9002.class, args); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9002/src/main/java/cn/hzp/springcloud/alibaba/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @Classname PaymentController 10 | * @Description TODO 11 | * @Date 2020/3/17 14:27 12 | * @Created by HuZP 13 | */ 14 | @RestController 15 | public class PaymentController 16 | { 17 | @Value("${server.port}") 18 | private String serverPort; 19 | 20 | @GetMapping(value = "/payment/nacos/{id}") 21 | public String getPayment(@PathVariable("id") Integer id) 22 | { 23 | return "nacos registry, serverPort: "+ serverPort+"\t id"+id; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9002/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9002 3 | 4 | spring: 5 | application: 6 | name: nacos-payment-provider 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 #配置Nacos地址 11 | # 换成nginx的1111端口,做集群 12 | # server-addr: 192.168.111.144:1111 13 | 14 | management: 15 | endpoints: 16 | web: 17 | exposure: 18 | include: '*' -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9003/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloudalibaba-provider-payment9003 13 | 14 | 15 | 16 | 17 | com.alibaba.cloud 18 | spring-cloud-starter-alibaba-nacos-discovery 19 | 20 | 21 | cn.hzp.springcloud 22 | cloud-api-commons 23 | ${project.version} 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-devtools 38 | runtime 39 | true 40 | 41 | 42 | org.projectlombok 43 | lombok 44 | 1.16.18 45 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9003/src/main/java/cn/hzp/springcloud/alibaba/PaymentMain9003.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname PaymentMain9003 9 | * @Description TODO 10 | * @Date 2020/3/19 16:43 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableDiscoveryClient 15 | public class PaymentMain9003 16 | { 17 | public static void main(String[] args) { 18 | SpringApplication.run(PaymentMain9003.class, args); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9003/src/main/java/cn/hzp/springcloud/alibaba/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.controller; 2 | 3 | import cn.hzp.springcloud.entities.CommonResult; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import java.util.HashMap; 11 | 12 | /** 13 | * @Classname PaymentController 14 | * @Description TODO 15 | * @Date 2020/3/19 16:43 16 | * @Created by HuZP 17 | */ 18 | @RestController 19 | public class PaymentController 20 | { 21 | @Value("${server.port}") 22 | private String serverPort; 23 | 24 | public static HashMap hashMap = new HashMap<>(); 25 | static 26 | { 27 | hashMap.put(1L,new Payment(1L,"9003111 ")); 28 | hashMap.put(2L,new Payment(2L,"9003222")); 29 | hashMap.put(3L,new Payment(3L,"9003333")); 30 | } 31 | 32 | @GetMapping(value = "/paymentSQL/{id}") 33 | public CommonResult paymentSQL(@PathVariable("id") Long id) 34 | { 35 | Payment payment = hashMap.get(id); 36 | CommonResult result = new CommonResult(200,"from mysql,serverPort: "+serverPort,payment); 37 | return result; 38 | } 39 | } -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9003/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9003 3 | 4 | spring: 5 | application: 6 | name: nacos-payment-provider 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 #配置Nacos地址 11 | 12 | management: 13 | endpoints: 14 | web: 15 | exposure: 16 | include: '*' 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9004/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloudalibaba-provider-payment9004 13 | 14 | 15 | 16 | 17 | com.alibaba.cloud 18 | spring-cloud-starter-alibaba-nacos-discovery 19 | 20 | 21 | cn.hzp.springcloud 22 | cloud-api-commons 23 | ${project.version} 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-devtools 38 | runtime 39 | true 40 | 41 | 42 | org.projectlombok 43 | lombok 44 | 1.16.18 45 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9004/src/main/java/cn/hzp/springcloud/alibaba/PaymentMain9004.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname PaymentMain9004 9 | * @Description TODO 10 | * @Date 2020/3/19 16:46 11 | * @Created by HuZP 12 | */ 13 | @SpringBootApplication 14 | @EnableDiscoveryClient 15 | public class PaymentMain9004 16 | { 17 | public static void main(String[] args) { 18 | SpringApplication.run(PaymentMain9004.class, args); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9004/src/main/java/cn/hzp/springcloud/alibaba/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.controller; 2 | 3 | import cn.hzp.springcloud.entities.CommonResult; 4 | import cn.hzp.springcloud.entities.Payment; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import java.util.HashMap; 11 | 12 | /** 13 | * @Classname PaymentController 14 | * @Description TODO 15 | * @Date 2020/3/19 16:46 16 | * @Created by HuZP 17 | */ 18 | @RestController 19 | public class PaymentController 20 | { 21 | @Value("${server.port}") 22 | private String serverPort; 23 | 24 | public static HashMap hashMap = new HashMap<>(); 25 | static 26 | { 27 | hashMap.put(1L,new Payment(1L,"9004111")); 28 | hashMap.put(2L,new Payment(2L,"9004222")); 29 | hashMap.put(3L,new Payment(3L,"9004333")); 30 | } 31 | 32 | @GetMapping(value = "/paymentSQL/{id}") 33 | public CommonResult paymentSQL(@PathVariable("id") Long id) 34 | { 35 | Payment payment = hashMap.get(id); 36 | CommonResult result = new CommonResult(200,"from mysql,serverPort: "+serverPort,payment); 37 | return result; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9004/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9004 3 | 4 | spring: 5 | application: 6 | name: nacos-payment-provider 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 #配置Nacos地址 11 | 12 | management: 13 | endpoints: 14 | web: 15 | exposure: 16 | include: '*' 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | cn.hzp.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloudalibaba-sentinel-service8401 13 | 14 | 15 | 16 | 17 | cn.hzp.springcloud 18 | cloud-api-commons 19 | ${project.version} 20 | 21 | 22 | 23 | com.alibaba.cloud 24 | spring-cloud-starter-alibaba-nacos-discovery 25 | 26 | 27 | 28 | com.alibaba.csp 29 | sentinel-datasource-nacos 30 | 31 | 32 | 33 | com.alibaba.cloud 34 | spring-cloud-starter-alibaba-sentinel 35 | 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-starter-openfeign 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-web 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-actuator 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-devtools 54 | runtime 55 | true 56 | 57 | 58 | cn.hutool 59 | hutool-all 60 | 4.6.3 61 | 62 | 63 | org.projectlombok 64 | lombok 65 | true 66 | 1.16.18 67 | 68 | 69 | org.springframework.boot 70 | spring-boot-starter-test 71 | test 72 | 73 | 74 | -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/src/main/java/cn/hzp/springcloud/alibaba/MainApp8401.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @Classname MainApp8401 9 | * @Description TODO 10 | * @Date 2020/3/18 14:49 11 | * @Created by HuZP 12 | */ 13 | @EnableDiscoveryClient 14 | @SpringBootApplication 15 | public class MainApp8401 16 | { 17 | public static void main(String[] args) { 18 | SpringApplication.run(MainApp8401.class, args); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/src/main/java/cn/hzp/springcloud/alibaba/controller/FlowLimitController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.controller; 2 | 3 | import com.alibaba.csp.sentinel.annotation.SentinelResource; 4 | import com.alibaba.csp.sentinel.slots.block.BlockException; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RequestParam; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | /** 11 | * @Classname FlowLimitController 12 | * @Description TODO 13 | * @Date 2020/3/18 14:58 14 | * @Created by HuZP 15 | */ 16 | @RestController 17 | @Slf4j 18 | public class FlowLimitController 19 | { 20 | @GetMapping("/testA") 21 | public String testA() 22 | { 23 | return "------testA"; 24 | } 25 | 26 | @GetMapping("/testB") 27 | public String testB() 28 | { 29 | log.info(Thread.currentThread().getName()+"\t"+"...testB"); 30 | return "------testB"; 31 | } 32 | 33 | 34 | @GetMapping("/testD") 35 | public String testD() 36 | { 37 | // try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } 38 | // log.info("testD 测试RT"); 39 | 40 | log.info("testD 异常比例"); 41 | int age = 10/0; 42 | return "------testD"; 43 | } 44 | 45 | @GetMapping("/testE") 46 | public String testE() 47 | { 48 | log.info("testE 测试异常数"); 49 | int age = 10/0; 50 | return "------testE 测试异常数"; 51 | } 52 | 53 | @GetMapping("/testHotKey") 54 | @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey") 55 | public String testHotKey(@RequestParam(value = "p1",required = false) String p1, 56 | @RequestParam(value = "p2",required = false) String p2) 57 | { 58 | //int age = 10/0; 59 | return "------testHotKey"; 60 | } 61 | public String deal_testHotKey (String p1, String p2, BlockException exception) 62 | { 63 | return "------deal_testHotKey,o(╥﹏╥)o"; //sentinel系统默认的提示:Blocked by Sentinel (flow limiting) 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/src/main/java/cn/hzp/springcloud/alibaba/controller/RateLimitController.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.controller; 2 | 3 | import cn.hzp.springcloud.alibaba.myHandler.CustomerBlockHandler; 4 | import cn.hzp.springcloud.entities.CommonResult; 5 | import cn.hzp.springcloud.entities.Payment; 6 | import com.alibaba.csp.sentinel.annotation.SentinelResource; 7 | import com.alibaba.csp.sentinel.slots.block.BlockException; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | /** 12 | * @Classname RateLimitController 13 | * @Description TODO 14 | * @Date 2020/3/18 14:59 15 | * @Created by HuZP 16 | */ 17 | @RestController 18 | public class RateLimitController 19 | { 20 | @GetMapping("/byResource") 21 | @SentinelResource(value = "byResource",blockHandler = "handleException") 22 | public CommonResult byResource() 23 | { 24 | return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001")); 25 | } 26 | public CommonResult handleException(BlockException exception) 27 | { 28 | return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用"); 29 | } 30 | 31 | @GetMapping("/rateLimit/byUrl") 32 | @SentinelResource(value = "byUrl") 33 | public CommonResult byUrl() 34 | { 35 | return new CommonResult(200,"按url限流测试OK",new Payment(2020L,"serial002")); 36 | } 37 | 38 | 39 | @GetMapping("/rateLimit/customerBlockHandler") 40 | @SentinelResource(value = "customerBlockHandler", 41 | blockHandlerClass = CustomerBlockHandler.class, 42 | blockHandler = "handlerException2") 43 | public CommonResult customerBlockHandler() 44 | { 45 | return new CommonResult(200,"按客戶自定义",new Payment(2020L,"serial003")); 46 | } 47 | } -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/src/main/java/cn/hzp/springcloud/alibaba/myHandler/CustomerBlockHandler.java: -------------------------------------------------------------------------------- 1 | package cn.hzp.springcloud.alibaba.myHandler; 2 | 3 | import cn.hzp.springcloud.entities.CommonResult; 4 | import com.alibaba.csp.sentinel.slots.block.BlockException; 5 | 6 | /** 7 | * @Classname CustomerBlockHandler 8 | * @Description TODO 9 | * @Date 2020/3/18 14:57 10 | * @Created by HuZP 11 | */ 12 | public class CustomerBlockHandler 13 | { 14 | public static CommonResult handlerException(BlockException exception) 15 | { 16 | return new CommonResult(4444,"按客戶自定义,global handlerException----1"); 17 | } 18 | public static CommonResult handlerException2(BlockException exception) 19 | { 20 | return new CommonResult(4444,"按客戶自定义,global handlerException----2"); 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8401 3 | 4 | spring: 5 | application: 6 | name: cloudalibaba-sentinel-service 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: localhost:8848 #Nacos服务注册中心地址 11 | sentinel: 12 | transport: 13 | dashboard: localhost:8080 #配置Sentinel dashboard地址 14 | port: 8719 15 | datasource: 16 | ds1: 17 | nacos: 18 | server-addr: localhost:8848 19 | dataId: cloudalibaba-sentinel-service 20 | groupId: DEFAULT_GROUP 21 | data-type: json 22 | rule-type: flow 23 | 24 | management: 25 | endpoints: 26 | web: 27 | exposure: 28 | include: '*' 29 | 30 | feign: 31 | sentinel: 32 | enabled: true # 激活Sentinel对Feign的支持 33 | 34 | 35 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | cn.hzp.springcloud 8 | cloud2020 9 | 1.0-SNAPSHOT 10 | pom 11 | 12 | cloud-provider-payment8001 13 | cloud-consumer-order80 14 | cloud-api-commons 15 | cloud-eureka-server7001 16 | cloud-eureka-server7002 17 | cloud-provider-payment8004 18 | cloud-consumerzk-order80 19 | cloud-providerconsul-payment8006 20 | cloud-consumerconsul-order80 21 | cloud-consumer-feign-order80 22 | cloud-provider-hystrix-payment8001 23 | cloud-consumer-feign-hystrix-order80 24 | cloud-consumer-hystrix-dashboard9001 25 | cloud-gateway-gateway9527 26 | cloud-config-center-3344 27 | cloud-config-client-3355 28 | cloud-config-client-3366 29 | cloud-stream-rabbitmq-provider8801 30 | cloud-stream-rabbitmq-consumer8802 31 | cloud-stream-rabbitmq-consumer8803 32 | cloudalibaba-provider-payment9001 33 | cloudalibaba-provider-payment9002 34 | cloudalibaba-consumer-nacos-order83 35 | cloudalibaba-config-nacos-client3377 36 | cloudalibaba-sentinel-service8401 37 | cloudalibaba-provider-payment9003 38 | cloudalibaba-provider-payment9004 39 | cloudalibaba-consumer-nacos-order84 40 | 41 | 42 | 43 | 44 | 45 | UTF-8 46 | 1.8 47 | 1.8 48 | 4.12 49 | 1.2.17 50 | 1.16.18 51 | 8.0.19 52 | 1.1.16 53 | 2.2.2.RELEASE 54 | Hoxton.SR1 55 | 2.1.0.RELEASE 56 | 1.3.0 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-dependencies 66 | ${spring.boot.version} 67 | pom 68 | import 69 | 70 | 71 | 72 | org.springframework.cloud 73 | spring-cloud-dependencies 74 | ${spring.cloud.version} 75 | pom 76 | import 77 | 78 | 79 | 80 | com.alibaba.cloud 81 | spring-cloud-alibaba-dependencies 82 | ${spring.cloud.alibaba.version} 83 | pom 84 | import 85 | 86 | 87 | mysql 88 | mysql-connector-java 89 | ${mysql.version} 90 | 91 | 92 | com.alibaba 93 | druid 94 | ${druid.version} 95 | 96 | 97 | org.projectlombok 98 | lombok 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | org.springframework.boot 107 | spring-boot-maven-plugin 108 | 2.2.2.RELEASE 109 | 110 | true 111 | true 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | nexus-aliyun 121 | Nexus aliyun 122 | http://maven.aliyun.com/nexus/content/groups/public 123 | 124 | true 125 | 126 | 127 | false 128 | 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /sql/cloud2020.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost8 5 | Source Server Type : MySQL 6 | Source Server Version : 80019 7 | Source Host : localhost:3306 8 | Source Schema : cloud2020 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 80019 12 | File Encoding : 65001 13 | 14 | Date: 19/03/2020 17:09:49 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for payment 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `payment`; 24 | CREATE TABLE `payment` ( 25 | `id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '主键', 26 | `serial` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '支付流水号', 27 | PRIMARY KEY (`id`) USING BTREE 28 | ) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '支付表' ROW_FORMAT = Dynamic; 29 | 30 | -- ---------------------------- 31 | -- Records of payment 32 | -- ---------------------------- 33 | INSERT INTO `payment` VALUES (1, '胡志鹏'); 34 | INSERT INTO `payment` VALUES (2, 'hzp'); 35 | INSERT INTO `payment` VALUES (3, '皮卡丘'); 36 | INSERT INTO `payment` VALUES (4, 'pikachu'); 37 | INSERT INTO `payment` VALUES (5, 'pikachu'); 38 | INSERT INTO `payment` VALUES (6, 'pikachu'); 39 | 40 | SET FOREIGN_KEY_CHECKS = 1; 41 | --------------------------------------------------------------------------------