├── cloud-api-commons ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── wenren │ └── springcloud │ └── entities │ ├── CommonResult.java │ └── Payment.java ├── cloud-config-center3344 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── ConfigCenterMain3344.java │ └── resources │ └── application.yml ├── cloud-config-client3355 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── ConfigClientMain3355.java │ │ └── controller │ │ └── ConfigClientController.java │ └── resources │ └── bootstrap.yml ├── cloud-config-client3366 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── ConfigClientMain3366.java │ │ └── controller │ │ └── ConfigClientController.java │ └── resources │ └── bootstrap.yml ├── cloud-consumer-consul-order80 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── OrderConsulMain80.java │ │ ├── config │ │ └── ApplicationContextConfig.java │ │ └── controller │ │ └── OrderConsulController.java │ └── resources │ └── application.yml ├── cloud-consumer-feign-hystrix-order80 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── OrderHystrixMain80.java │ │ ├── controller │ │ └── OrderHystrixController.java │ │ └── service │ │ ├── PaymentFallbackService.java │ │ └── PaymentHystrixService.java │ └── resources │ └── application.yml ├── cloud-consumer-feign-order80 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── OrderFeignMain80.java │ │ ├── config │ │ └── FeignConfig.java │ │ ├── controller │ │ └── OrderFeignController.java │ │ └── service │ │ └── PaymentFeignService.java │ └── resources │ └── application.yml ├── cloud-consumer-hystrix-dashboard9001 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── HystrixDashboardMain9001.java │ └── resources │ └── application.yml ├── cloud-consumer-order80 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── wenren │ │ │ ├── myrule │ │ │ └── MySelfRule.java │ │ │ └── springcloud │ │ │ ├── OrderMain80.java │ │ │ ├── config │ │ │ └── ApplicationContextConfig.java │ │ │ ├── controller │ │ │ └── OrderController.java │ │ │ └── lb │ │ │ ├── LoadBalancer.java │ │ │ └── MyLB.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── T.java ├── cloud-consumer-zk-order80 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── OrderZKMain80.java │ │ ├── config │ │ └── ApplicationContextConfig.java │ │ └── controller │ │ └── OrderZKController.java │ └── resources │ └── application.yml ├── cloud-eureka-server7001 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── EurekaMain7001.java │ └── resources │ └── application.yml ├── cloud-eureka-server7002 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── EurekaMain7002.java │ └── resources │ └── application.yml ├── cloud-gateway-gateway9527 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── wenren │ │ │ └── springcloud │ │ │ ├── GateWayMain9527.java │ │ │ ├── config │ │ │ └── GateWayConfig.java │ │ │ └── filter │ │ │ └── MyLogGateWayFilter.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ ├── T2.java │ └── com │ └── wenren │ └── test │ └── ZonedDateTimeDemo.java ├── cloud-provider-consul-payment8006 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── PaymentMain8006.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloud-provider-hystrix-payment8001 ├── pom.xml ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── wenren │ │ │ └── springcloud │ │ │ ├── PaymentHystrixMain8001.java │ │ │ ├── controller │ │ │ └── PaymentController.java │ │ │ └── service │ │ │ └── PaymentService.java │ │ └── resources │ │ └── application.yml └── 线程组202004.jmx ├── cloud-provider-payment8001 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── PaymentMain8001.java │ │ ├── controller │ │ └── PaymenController.java │ │ ├── dao │ │ └── PaymentDao.java │ │ ├── impl │ │ └── PaymentServiceImpl.java │ │ └── service │ │ └── PaymentService.java │ └── resources │ ├── application.yml │ └── mapper │ └── PaymentMapper.xml ├── cloud-provider-payment8002 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── PaymentMain8002.java │ │ ├── controller │ │ └── PaymenController.java │ │ ├── dao │ │ └── PaymentDao.java │ │ ├── impl │ │ └── PaymentServiceImpl.java │ │ └── service │ │ └── PaymentService.java │ └── resources │ ├── application.yml │ └── mapper │ └── PaymentMapper.xml ├── cloud-provider-zk-payment8004 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── PaymentMain8004.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloud-stream-rabbitmq-consumer8802 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── StreamMQMain8802.java │ │ └── controller │ │ └── ReceiveMessageListenerController.java │ └── resources │ └── application.yml ├── cloud-stream-rabbitmq-consumer8803 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── StreamMQMain8803.java │ │ └── controller │ │ └── ReceiveMessageListenerController.java │ └── resources │ └── application.yml ├── cloud-stream-rabbitmq-provider8801 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── StreamMQMain8801.java │ │ ├── controller │ │ └── SendMessageController.java │ │ └── service │ │ ├── IMessageProvider.java │ │ └── impl │ │ └── MessageProviderImpl.java │ └── resources │ └── application.yml ├── cloudalibaba-config-nacos-client3377 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── alibaba │ │ ├── NacosConfigClientMain3377.java │ │ └── controller │ │ └── ConfigClientController.java │ └── resources │ ├── application.yml │ └── bootstrap.yml ├── cloudalibaba-consumer-nacos-order83 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── OrderNacosMain83.java │ │ ├── config │ │ └── ApplicationContextBean.java │ │ └── controller │ │ └── OrderNacosController.java │ └── resources │ └── application.yml ├── cloudalibaba-consumer-nacos-order84 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ ├── OrderNacosMain84.java │ │ ├── config │ │ └── ApplicationContextBean.java │ │ ├── controller │ │ └── CircleBreakerController.java │ │ └── service │ │ ├── PaymentFallbackService.java │ │ └── PaymentService.java │ └── resources │ └── application.yml ├── cloudalibaba-provider-payment9001 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── alibaba │ │ ├── PaymentMain9001.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloudalibaba-provider-payment9002 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── alibaba │ │ ├── PaymentMain9002.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloudalibaba-provider-payment9003 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── alibaba │ │ ├── PaymentMain9003.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloudalibaba-provider-payment9004 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── alibaba │ │ ├── PaymentMain9004.java │ │ └── controller │ │ └── PaymentController.java │ └── resources │ └── application.yml ├── cloudalibaba-sentinel-service8401 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── alibaba │ │ ├── MainApp8401.java │ │ ├── controller │ │ ├── FlowLimitController.java │ │ └── RateLimitController.java │ │ └── myhandler │ │ └── CustomerBlockHandler.java │ └── resources │ └── application.yml ├── pom.xml ├── seata-account-service2003 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── alibaba │ │ ├── SeataAccountMainApp2003.java │ │ ├── config │ │ ├── DataSourceProxyConfig.java │ │ └── MyBatisConfig.java │ │ ├── controller │ │ └── AccountController.java │ │ ├── dao │ │ └── AccountDao.java │ │ ├── domain │ │ ├── Account.java │ │ └── CommonResult.java │ │ └── service │ │ ├── AccountService.java │ │ └── impl │ │ └── AccountServiceImpl.java │ └── resources │ ├── application.yml │ ├── file.conf │ ├── mapper │ └── AccountMapper.xml │ └── registry.conf ├── seata-order-service2001 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── alibaba │ │ ├── SeataOrderMainApp2001.java │ │ ├── config │ │ ├── DataSourceProxyConfig.java │ │ └── MyBatisConfig.java │ │ ├── controller │ │ └── OrderController.java │ │ ├── dao │ │ └── OrderDao.java │ │ ├── domain │ │ ├── CommonResult.java │ │ └── Order.java │ │ └── service │ │ ├── AccountService.java │ │ ├── OrderService.java │ │ ├── StorageService.java │ │ └── impl │ │ └── OrderServiceImpl.java │ └── resources │ ├── application.yml │ ├── file.conf │ ├── mapper │ └── OrderMapper.xml │ └── registry.conf ├── seata-storage-service2002 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── wenren │ │ └── springcloud │ │ └── alibaba │ │ ├── SeataStorageMainApp2002.java │ │ ├── config │ │ ├── DataSourceProxyConfig.java │ │ └── MyBatisConfig.java │ │ ├── controller │ │ └── StorageController.java │ │ ├── dao │ │ └── StorageDao.java │ │ ├── domain │ │ ├── CommonResult.java │ │ └── Storage.java │ │ └── service │ │ ├── StorageService.java │ │ └── impl │ │ └── StorageServiceImpl.java │ └── resources │ ├── application.yml │ ├── file.conf │ ├── mapper │ └── StorageMapper.xml │ └── registry.conf ├── sentinel-dashboard-1.7.2.jar └── zipkin-server-2.12.9-exec.jar /cloud-api-commons/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-api-commons 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-devtools 18 | runtime 19 | true 20 | 21 | 22 | org.projectlombok 23 | lombok 24 | true 25 | 26 | 27 | cn.hutool 28 | hutool-all 29 | 5.1.0 30 | 31 | 32 | -------------------------------------------------------------------------------- /cloud-api-commons/src/main/java/com/wenren/springcloud/entities/CommonResult.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.entities; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-03-24-14:19 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | public class CommonResult { 16 | private Integer code; 17 | private String message; 18 | private T data; 19 | 20 | public CommonResult(Integer code, String message) { 21 | this(code, message, null); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /cloud-api-commons/src/main/java/com/wenren/springcloud/entities/Payment.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.entities; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-03-24-14:19 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | public class Payment implements Serializable { 18 | private Long id; 19 | private String serial; 20 | } 21 | -------------------------------------------------------------------------------- /cloud-config-center3344/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-config-center3344 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-bus-amqp 19 | 20 | 21 | org.springframework.cloud 22 | spring-cloud-config-server 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-client 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-actuator 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-devtools 39 | runtime 40 | true 41 | 42 | 43 | org.projectlombok 44 | lombok 45 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | -------------------------------------------------------------------------------- /cloud-config-center3344/src/main/java/com/wenren/springcloud/ConfigCenterMain3344.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-04-16-16:45 11 | */ 12 | @SpringBootApplication 13 | @EnableConfigServer 14 | public class ConfigCenterMain3344 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(ConfigCenterMain3344.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-config-center3344/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3344 3 | 4 | spring: 5 | application: 6 | name: cloud-config-center #注册进Eureka服务器的微服务名 7 | cloud: 8 | config: 9 | server: 10 | git: 11 | uri: https://github.com/wenren0819/Spring-Cloud-Config.git #用下面的地址访问不了 12 | #uri: git@github.com:wenren0819/Spring-Cloud-Config.git #GitHub上面的git仓库名字 13 | ####搜索目录 14 | search-paths: 15 | - Spring-Cloud-Config 16 | ####读取分支 17 | label: master 18 | 19 | #RabbitMQ相关配置 20 | rabbitmq: 21 | host: 123.125.5.26 22 | port: 5672 23 | username: guest 24 | password: guest 25 | 26 | #服务注册到eureka地址 27 | eureka: 28 | client: 29 | service-url: 30 | defaultZone: http://localhost:7001/eureka 31 | 32 | #RabbitMQ相关配置,暴露bus刷新配置的端点 33 | management: 34 | endpoints: #暴露bus刷新配置的端点 35 | web: 36 | exposure: 37 | include: 'bus-refresh' 38 | -------------------------------------------------------------------------------- /cloud-config-client3355/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-config-client3355 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-bus-amqp 19 | 20 | 21 | org.springframework.cloud 22 | spring-cloud-starter-config 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-client 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-actuator 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-devtools 39 | runtime 40 | true 41 | 42 | 43 | org.projectlombok 44 | lombok 45 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | -------------------------------------------------------------------------------- /cloud-config-client3355/src/main/java/com/wenren/springcloud/ConfigClientMain3355.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-04-26-15:12 11 | */ 12 | @SpringBootApplication 13 | @EnableEurekaClient 14 | public class ConfigClientMain3355 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(ConfigClientMain3355.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-config-client3355/src/main/java/com/wenren/springcloud/controller/ConfigClientController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-04-26-15:14 12 | */ 13 | @RestController 14 | @RefreshScope 15 | public class ConfigClientController { 16 | @Value("${server.port}") 17 | private String serverPort; 18 | 19 | @Value("${config.info}") 20 | private String configInfo; 21 | 22 | @GetMapping("/configInfo") 23 | public String getConfigInfo() { 24 | return "serverPort: " + serverPort + "\t\n\n configInfo: " + configInfo; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /cloud-config-client3355/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3355 3 | 4 | spring: 5 | application: 6 | name: config-client 7 | cloud: 8 | #Config客户端配置 9 | config: 10 | label: master #分支名称 11 | name: config #配置文件名称 12 | profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml 13 | uri: http://localhost:3344 #配置中心地址 14 | 15 | #RabbitMQ相关配置 15672是web管理界面的端口;5672是MQ访问的端口 16 | rabbitmq: 17 | host: 123.125.5.26 18 | port: 5672 19 | username: guest 20 | password: guest 21 | 22 | #服务注册到eureka地址 23 | eureka: 24 | client: 25 | service-url: 26 | defaultZone: http://localhost:7001/eureka 27 | 28 | #暴露监控端点 29 | management: 30 | endpoints: 31 | web: 32 | exposure: 33 | include: "*" # 'refresh' -------------------------------------------------------------------------------- /cloud-config-client3366/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-config-client3366 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-bus-amqp 19 | 20 | 21 | org.springframework.cloud 22 | spring-cloud-starter-config 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-client 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-actuator 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-devtools 39 | runtime 40 | true 41 | 42 | 43 | org.projectlombok 44 | lombok 45 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | -------------------------------------------------------------------------------- /cloud-config-client3366/src/main/java/com/wenren/springcloud/ConfigClientMain3366.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-04-26-15:12 11 | */ 12 | @SpringBootApplication 13 | @EnableEurekaClient 14 | public class ConfigClientMain3366 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(ConfigClientMain3366.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-config-client3366/src/main/java/com/wenren/springcloud/controller/ConfigClientController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-04-26-15:14 12 | */ 13 | @RestController 14 | @RefreshScope 15 | public class ConfigClientController { 16 | @Value("${server.port}") 17 | private String serverPort; 18 | 19 | @Value("${config.info}") 20 | private String configInfo; 21 | 22 | @GetMapping("/configInfo") 23 | public String getConfigInfo() { 24 | return "serverPort: " + serverPort + "\t\n\n configInfo: " + configInfo; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /cloud-config-client3366/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 3366 3 | 4 | spring: 5 | application: 6 | name: config-client 7 | cloud: 8 | #Config客户端配置 9 | config: 10 | label: master #分支名称 11 | name: config #配置文件名称 12 | profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml 13 | uri: http://localhost:3344 #配置中心地址 14 | 15 | #RabbitMQ相关配置 15672是web管理界面的端口;5672是MQ访问的端口 16 | rabbitmq: 17 | host: 123.125.5.26 18 | port: 5672 19 | username: guest 20 | password: guest 21 | 22 | 23 | #服务注册到eureka地址 24 | eureka: 25 | client: 26 | service-url: 27 | defaultZone: http://localhost:7001/eureka 28 | 29 | #暴露监控端点 30 | management: 31 | endpoints: 32 | web: 33 | exposure: 34 | include: "*" 35 | -------------------------------------------------------------------------------- /cloud-consumer-consul-order80/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-consumer-consul-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 | 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 | true 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | -------------------------------------------------------------------------------- /cloud-consumer-consul-order80/src/main/java/com/wenren/springcloud/OrderConsulMain80.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-03-31-15:56 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient //该注解用于向使用consul或zookeeper作为注册中心时注册服务 14 | public class OrderConsulMain80 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(OrderConsulMain80.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-consumer-consul-order80/src/main/java/com/wenren/springcloud/config/ApplicationContextConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-03-31-15:59 12 | */ 13 | @Configuration 14 | public class ApplicationContextConfig { 15 | @Bean 16 | @LoadBalanced 17 | public RestTemplate getRestTemplate() { 18 | return new RestTemplate(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloud-consumer-consul-order80/src/main/java/com/wenren/springcloud/controller/OrderConsulController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | import javax.annotation.Resource; 9 | 10 | /** 11 | * @author wangyang 12 | * @version V1.0 13 | * @create 2020-03-30-15:42 14 | */ 15 | @RestController 16 | @Slf4j 17 | public class OrderConsulController { 18 | public static final String INVOKE_URL = "http://cloud-provider-consul-payment"; 19 | 20 | @Resource 21 | private RestTemplate restTemplate; 22 | 23 | @GetMapping(value = "/consumer/payment/consul") 24 | public String paymentInfo() { 25 | String result = restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class); 26 | return result; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /cloud-consumer-consul-order80/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | ###服务端口号 2 | server: 3 | port: 80 4 | 5 | spring: 6 | application: 7 | name: cloud-consumer-consul-order 8 | ###consul注册中心地址 9 | cloud: 10 | consul: 11 | host: 192.168.0.135 12 | port: 8500 13 | discovery: 14 | #hostname:127.0.0.1 15 | service-name: ${spring.application.name} -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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 | 32 | com.wenren.springcloud 33 | cloud-api-commons 34 | ${project.version} 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-web 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-actuator 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-devtools 47 | runtime 48 | true 49 | 50 | 51 | org.projectlombok 52 | lombok 53 | true 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-starter-test 58 | test 59 | 60 | 61 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/src/main/java/com/wenren/springcloud/OrderHystrixMain80.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 6 | import org.springframework.cloud.openfeign.EnableFeignClients; 7 | 8 | /** 9 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-04-09-15:49 12 | */ 13 | @SpringBootApplication 14 | @EnableFeignClients 15 | @EnableHystrix 16 | public class OrderHystrixMain80 { 17 | public static void main(String[] args) { 18 | SpringApplication.run(OrderHystrixMain80.class, args); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/src/main/java/com/wenren/springcloud/controller/OrderHystrixController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; 4 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 5 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; 6 | import com.wenren.springcloud.service.PaymentHystrixService; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import javax.annotation.Resource; 13 | 14 | /** 15 | * @author wangyang 16 | * @version V1.0 17 | * @create 2020-04-09-15:53 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 | String result = paymentHystrixService.paymentInfo_OK(id); 29 | log.info("*****result:" + result); 30 | return result; 31 | } 32 | 33 | @GetMapping("/consumer/payment/hystrix/timeout/{id}") 34 | @HystrixCommand 35 | /*@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = { 36 | @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500") 37 | })*/ 38 | public String paymentInfo_TimeOut(@PathVariable("id") Integer id) { 39 | int age = 10 / 0; 40 | String result = paymentHystrixService.paymentInfo_TimeOut(id); 41 | log.info("*****result:" + result); 42 | return result; 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异常处理信息,请稍后再试,/(ToT)/~~"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/src/main/java/com/wenren/springcloud/service/PaymentFallbackService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.service; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | /** 6 | * @author wangyang 7 | * @version V1.0 8 | * @create 2020-04-10-11:40 9 | */ 10 | @Component 11 | public class PaymentFallbackService implements PaymentHystrixService { 12 | @Override 13 | public String paymentInfo_OK(Integer id) { 14 | return "------PaymentFallbackService fall back-paymentInfo_OK, o(╥﹏╥)o"; 15 | } 16 | 17 | @Override 18 | public String paymentInfo_TimeOut(Integer id) { 19 | return "------PaymentFallbackService fall back-paymentInfo_TimeOut, o(╥﹏╥)o"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/src/main/java/com/wenren/springcloud/service/PaymentHystrixService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-04-09-15:52 12 | */ 13 | @Component 14 | @FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallbackService.class) 15 | public interface PaymentHystrixService { 16 | @GetMapping("/payment/hystrix/ok/{id}") 17 | String paymentInfo_OK(@PathVariable("id") Integer id); 18 | 19 | @GetMapping("/payment/hystrix/timeout/{id}") 20 | String paymentInfo_TimeOut(@PathVariable("id") Integer id); 21 | } 22 | -------------------------------------------------------------------------------- /cloud-consumer-feign-hystrix-order80/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 80 3 | 4 | eureka: 5 | client: 6 | register-with-eureka: false 7 | service-url: 8 | defaultZone: http://eureka7001.com:7001/eureka/ 9 | 10 | feign: 11 | hystrix: 12 | enabled: true 13 | 14 | #如果配置了ferign-hystrix,就要配置ribbon(默认1秒)和hystrix的超时时间,且ribbon超时时间>hystrix超时时间,否则ribbon超时也会自动降级 15 | ribbon: 16 | ReadTimeout: 5000 17 | ConnectTimeout: 5000 18 | 19 | hystrix: 20 | command: 21 | default: 22 | execution: 23 | isolation: 24 | thread: 25 | timeoutInMilliseconds: 4000 26 | 27 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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 | 27 | com.wenren.springcloud 28 | cloud-api-commons 29 | ${project.version} 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-web 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-actuator 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-devtools 42 | runtime 43 | true 44 | 45 | 46 | org.projectlombok 47 | lombok 48 | true 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-starter-test 53 | test 54 | 55 | 56 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/src/main/java/com/wenren/springcloud/OrderFeignMain80.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-04-04-22:57 11 | */ 12 | @SpringBootApplication 13 | @EnableFeignClients 14 | public class OrderFeignMain80 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(OrderFeignMain80.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/src/main/java/com/wenren/springcloud/config/FeignConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.config; 2 | 3 | import feign.Logger; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-04-05-22:42 11 | */ 12 | @Configuration 13 | public class FeignConfig { 14 | @Bean 15 | Logger.Level feignLoggerLevel() { 16 | return Logger.Level.FULL; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/src/main/java/com/wenren/springcloud/controller/OrderFeignController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import com.wenren.springcloud.entities.CommonResult; 4 | import com.wenren.springcloud.entities.Payment; 5 | import com.wenren.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 | * @author wangyang 15 | * @version V1.0 16 | * @create 2020-04-04-23:17 17 | */ 18 | @RestController 19 | @Slf4j 20 | public class OrderFeignController { 21 | @Resource 22 | private PaymentFeignService paymentFeignService; 23 | 24 | @GetMapping(value = "/consumer/payment/get/{id}") 25 | public CommonResult getPaymentById(@PathVariable("id") Long id) { 26 | return paymentFeignService.getPaymentById(id); 27 | } 28 | 29 | @GetMapping(value = "/consumer/payment/feign/timeout") 30 | public String paymentFeignTimeout() { 31 | //openfeign-ribbon,客户端一般默认等待1秒钟 32 | return paymentFeignService.paymentFeignTimeout(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/src/main/java/com/wenren/springcloud/service/PaymentFeignService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.service; 2 | 3 | import com.wenren.springcloud.entities.CommonResult; 4 | import com.wenren.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 | * @author wangyang 12 | * @version V1.0 13 | * @create 2020-04-04-23:03 14 | */ 15 | @Component 16 | @FeignClient("CLOUD-PAYMENT-SERVICE") 17 | public interface PaymentFeignService { 18 | @GetMapping(value = "/payment/get/{id}") 19 | CommonResult getPaymentById(@PathVariable("id") Long id); 20 | 21 | @GetMapping(value = "payment/feign/timeout") 22 | String paymentFeignTimeout(); 23 | } 24 | -------------------------------------------------------------------------------- /cloud-consumer-feign-order80/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 80 3 | 4 | eureka: 5 | client: 6 | register-with-eureka: false 7 | service-url: 8 | defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ 9 | 10 | #设置feign客户端超时时间(OpenFeign默认支持ribbon) 11 | ribbon: 12 | #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间 13 | ReadTimeout: 5000 14 | #指的是建立连接后从服务器读取到可用资源所用的时间 15 | ConnectTimeout: 5000 16 | 17 | logging: 18 | level: 19 | #feign日志以什么级别监控哪个接口 20 | com.wenren.springcloud.service.PaymentFeignService: debug -------------------------------------------------------------------------------- /cloud-consumer-hystrix-dashboard9001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-consumer-hystrix-dashboard9001 13 | 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-starter-netflix-hystrix-dashboard 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-actuator 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-devtools 26 | runtime 27 | true 28 | 29 | 30 | org.projectlombok 31 | lombok 32 | true 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | -------------------------------------------------------------------------------- /cloud-consumer-hystrix-dashboard9001/src/main/java/com/wenren/springcloud/HystrixDashboardMain9001.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-04-13-15:48 11 | */ 12 | @SpringBootApplication 13 | @EnableHystrixDashboard 14 | public class HystrixDashboardMain9001 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(HystrixDashboardMain9001.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-consumer-hystrix-dashboard9001/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9001 -------------------------------------------------------------------------------- /cloud-consumer-order80/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.wenren.springcloud 7 | cloud2020 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-consumer-order80 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-zipkin 19 | 20 | 21 | org.springframework.cloud 22 | spring-cloud-starter-netflix-eureka-client 23 | 24 | 25 | 26 | com.wenren.springcloud 27 | cloud-api-commons 28 | ${project.version} 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-actuator 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-devtools 41 | runtime 42 | true 43 | 44 | 45 | org.projectlombok 46 | lombok 47 | true 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-starter-test 52 | test 53 | 54 | 55 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/com/wenren/myrule/MySelfRule.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-04-02-14:19 12 | */ 13 | @Configuration 14 | public class MySelfRule { 15 | @Bean 16 | public IRule myRule() { 17 | return new RandomRule();//定义为随机 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/com/wenren/springcloud/OrderMain80.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 2 | 3 | import com.wenren.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 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-03-24-11:16 13 | */ 14 | @SpringBootApplication 15 | @EnableEurekaClient 16 | //@RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = MySelfRule.class) 17 | public class OrderMain80 { 18 | public static void main(String[] args) { 19 | SpringApplication.run(OrderMain80.class, args); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/com/wenren/springcloud/config/ApplicationContextConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-03-24-12:05 12 | */ 13 | @Configuration 14 | public class ApplicationContextConfig { 15 | @Bean 16 | //@LoadBalanced 17 | public RestTemplate getRestTemplate() { 18 | return new RestTemplate(); 19 | } 20 | } 21 | //applicationContext.xml 22 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/com/wenren/springcloud/lb/LoadBalancer.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.lb; 2 | 3 | import org.springframework.cloud.client.ServiceInstance; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-04-03-11:46 11 | */ 12 | public interface LoadBalancer { 13 | ServiceInstance instances(List serviceInstances); 14 | } 15 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/java/com/wenren/springcloud/lb/MyLB.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-04-03-11:48 13 | */ 14 | @Component 15 | public class MyLB implements LoadBalancer { 16 | private AtomicInteger atomicInteger = new AtomicInteger(0); 17 | 18 | public final int getAndIncrement() { 19 | int current; 20 | int next; 21 | do { 22 | current = this.atomicInteger.get(); 23 | next = current >= 2147483647 ? 0 : current + 1; 24 | } while (!this.atomicInteger.compareAndSet(current, next)); 25 | System.out.println("*****第几次访问,次数next:" + next); 26 | return next; 27 | } 28 | 29 | //负载均衡算法:rest接口第几次请求数 % 服务器集群总数量 = 实际调用服务器位置下标,每次服务重启动后rest接口计数从1开始。 30 | @Override 31 | public ServiceInstance instances(List serviceInstances) { 32 | int index = getAndIncrement() % serviceInstances.size(); 33 | 34 | return serviceInstances.get(index); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /cloud-consumer-order80/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 80 3 | 4 | spring: 5 | application: 6 | name: cloud-order-service 7 | zipkin: 8 | base-url: http://localhost:9411 9 | sleuth: 10 | sampler: 11 | probability: 1 #采样率介于0到1之间,1则表示全部采集 12 | 13 | eureka: 14 | client: 15 | #表示是否将自己注册进EurekaServer默认为true。 16 | register-with-eureka: true 17 | #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置true才能配合ribbon使用负载均衡 18 | fetch-registry: true 19 | service-url: 20 | #defaultZone: http://localhost:7001/eureka 21 | defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版 -------------------------------------------------------------------------------- /cloud-consumer-order80/src/test/java/T.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | import java.util.UUID; 3 | 4 | /** 5 | * @author wangyang 6 | * @version V1.0 7 | * @create 2020-04-03-11:52 8 | */ 9 | public class T { 10 | public static void main(String[] args) { 11 | /*System.out.println(Integer.MAX_VALUE); 12 | BigDecimal amount = new BigDecimal(1051); 13 | BigDecimal rate = new BigDecimal(0.9817); 14 | BigDecimal result = amount.multiply(rate).divide(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_DOWN); 15 | System.out.println(result.multiply(new BigDecimal(100))); 16 | System.out.println(UUID.randomUUID().toString().replace("-", "")); 17 | System.out.println(UUID.randomUUID().toString().replace("-", "")); 18 | System.out.println(UUID.randomUUID().toString().replace("-",a ""));*/ 19 | 20 | String a = String.valueOf((new BigDecimal("35002.70")).multiply(new BigDecimal(100)).intValue()); 21 | System.out.println(a); 22 | String b = String.format("%.2f", Long.valueOf(a) / 100.0); 23 | System.out.println(b); 24 | String c = String.valueOf(BigDecimal.valueOf(new Double(("35002.70"))).multiply(new BigDecimal(100)).longValue()); 25 | System.out.println(c); 26 | 27 | System.out.println((new Double(("35002.70")) * 100)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /cloud-consumer-zk-order80/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-consumer-zk-order80 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | 22 | com.wenren.springcloud 23 | cloud-api-commons 24 | ${project.version} 25 | 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-zookeeper-discovery 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-devtools 34 | runtime 35 | true 36 | 37 | 38 | org.projectlombok 39 | lombok 40 | true 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | -------------------------------------------------------------------------------- /cloud-consumer-zk-order80/src/main/java/com/wenren/springcloud/OrderZKMain80.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-03-30-15:38 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class OrderZKMain80 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(OrderZKMain80.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-consumer-zk-order80/src/main/java/com/wenren/springcloud/config/ApplicationContextConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-03-30-15:40 12 | */ 13 | @Configuration 14 | public class ApplicationContextConfig { 15 | @Bean 16 | @LoadBalanced 17 | public RestTemplate getRestTemplate() { 18 | return new RestTemplate(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloud-consumer-zk-order80/src/main/java/com/wenren/springcloud/controller/OrderZKController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | import javax.annotation.Resource; 9 | 10 | /** 11 | * @author wangyang 12 | * @version V1.0 13 | * @create 2020-03-30-15:42 14 | */ 15 | @RestController 16 | @Slf4j 17 | public class OrderZKController { 18 | public static final String INVOKE_URL = "http://cloud-provider-zk-payment"; 19 | 20 | @Resource 21 | private RestTemplate restTemplate; 22 | 23 | @GetMapping(value = "/consumer/payment/zk") 24 | public String paymentInfo() { 25 | String result = restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class); 26 | return result; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /cloud-consumer-zk-order80/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 80 3 | 4 | spring: 5 | application: 6 | name: cloud-consumer-zk-order 7 | cloud: 8 | zookeeper: 9 | connect-string: 123.125.5.26:2182 -------------------------------------------------------------------------------- /cloud-eureka-server7001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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 | com.wenren.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 | true 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | -------------------------------------------------------------------------------- /cloud-eureka-server7001/src/main/java/com/wenren/springcloud/EurekaMain7001.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-03-25-14:14 11 | */ 12 | @SpringBootApplication 13 | @EnableEurekaServer 14 | public class EurekaMain7001 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(EurekaMain7001.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-eureka-server7001/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7001 3 | 4 | eureka: 5 | instance: 6 | hostname: eureka7001.com #eureka服务端的实例名称 7 | client: 8 | #false表示不向注册中心注册自己 9 | register-with-eureka: false 10 | #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 11 | fetch-registry: false 12 | service-url: 13 | #集群指向其它eureka 14 | #defaultZone: http://eureka7002.com:7002/eureka/ 15 | #单机就是自己 16 | defaultZone: http://eureka7001.com:7001/eureka/ 17 | server: 18 | #关闭自我保护机制,保证不可用服务被及时剔除 19 | enable-self-preservation: false 20 | eviction-interval-timer-in-ms: 2000 -------------------------------------------------------------------------------- /cloud-eureka-server7002/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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 | com.wenren.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 | true 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | -------------------------------------------------------------------------------- /cloud-eureka-server7002/src/main/java/com/wenren/springcloud/EurekaMain7002.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-03-26-12:04 11 | */ 12 | @SpringBootApplication 13 | @EnableEurekaServer 14 | public class EurekaMain7002 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(EurekaMain7002.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-eureka-server7002/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7002 3 | 4 | eureka: 5 | instance: 6 | hostname: eureka7002.com #eureka服务端的实例名称 7 | client: 8 | #false表示不向注册中心注册自己 9 | register-with-eureka: false 10 | #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 11 | fetch-registry: false 12 | service-url: 13 | defaultZone: http://eureka7001.com:7001/eureka/ -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-gateway-gateway9527 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-gateway 19 | 20 | 21 | 22 | org.springframework.cloud 23 | spring-cloud-starter-netflix-eureka-client 24 | 25 | 26 | 27 | com.wenren.springcloud 28 | cloud-api-commons 29 | ${project.version} 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-devtools 35 | runtime 36 | true 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | true 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/main/java/com/wenren/springcloud/GateWayMain9527.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-04-15-11:30 11 | */ 12 | @SpringBootApplication 13 | @EnableEurekaClient 14 | public class GateWayMain9527 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(GateWayMain9527.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/main/java/com/wenren/springcloud/config/GateWayConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-04-15-13:41 12 | */ 13 | @Configuration 14 | public class GateWayConfig { 15 | /** 16 | * 配置了一个id为route-name的路由规则, 17 | * 当访问地址http://localhost:9527/guonei时会自动转发到地址:http://news.baidu.com/guonei 18 | * 19 | * @param routeLocatorBuilder 20 | * @return 21 | */ 22 | @Bean 23 | public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) { 24 | RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes(); 25 | //http://news.baidu.com/ 26 | routes.route("path_route_wenren", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build(); 27 | 28 | return routes.build(); 29 | } 30 | 31 | @Bean 32 | public RouteLocator customRouteLocator2(RouteLocatorBuilder routeLocatorBuilder) { 33 | RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes(); 34 | //http://news.baidu.com/ 35 | routes.route("path_route_wenren", r -> r.path("/guoji").uri("http://news.baidu.com/guoji")).build(); 36 | 37 | return routes.build(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/main/java/com/wenren/springcloud/filter/MyLogGateWayFilter.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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.stereotype.Component; 9 | import org.springframework.web.server.ServerWebExchange; 10 | import reactor.core.publisher.Mono; 11 | 12 | import java.util.Date; 13 | 14 | /** 15 | * @author wangyang 16 | * @version V1.0 17 | * @create 2020-04-16-13:52 18 | */ 19 | @Component 20 | @Slf4j 21 | public class MyLogGateWayFilter implements GlobalFilter, Ordered { 22 | @Override 23 | public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { 24 | log.info("**************come in MyLogGateWayFilter:" + new Date()); 25 | String uname = exchange.getRequest().getQueryParams().getFirst("uname"); 26 | if (uname == null) { 27 | log.info("**************用户名为null,非法用户,o(╥﹏╥)o"); 28 | exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); 29 | return exchange.getResponse().setComplete(); 30 | } 31 | return chain.filter(exchange); 32 | } 33 | 34 | @Override 35 | public int getOrder() { 36 | return 0; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9527 3 | 4 | spring: 5 | application: 6 | name: cloud-gateway 7 | cloud: 8 | gateway: 9 | discovery: 10 | locator: 11 | enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由 12 | routes: 13 | - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 14 | #uri: http://localhost:8001 15 | uri: lb://cloud-payment-service #匹配后提供服务的路由地址 16 | predicates: 17 | - Path=/payment/get/** #断言,路径相匹配的进行路由 18 | 19 | - id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 20 | #uri: http://localhost:8001 21 | uri: lb://cloud-payment-service #匹配后提供服务的路由地址 22 | predicates: 23 | - Path=/payment/lb/** #断言,路径相匹配的进行路由 24 | - After=2020-04-15T15:20:23.855+08:00[Asia/Shanghai] #断言,路径相匹配的进行路由 25 | #- Before=2020-04-15T15:20:23.855+08:00[Asia/Shanghai] #断言,路径相匹配的进行路由 26 | #- Between=2020-04-15T15:20:23.855+08:00[Asia/Shanghai], 2020-05-15T15:20:23.855+08:00[Asia/Shanghai] 27 | #- Cookie=username, wenren 28 | #- Header=X-Request-Id, \d+ #请求头要有X-Request-Id属性并且值为整数的正则表达式 29 | #- Host=**.wenren.com 30 | #- Method=GET 31 | #- Query=username, \d+ #要有参数名username并且值还要是整数才能路由 32 | #ilters: 33 | #- AddRequestParameter=X-Request-Id,1024 #过滤器工厂会在匹配的请求头加上一对请求头,名称为X-Request-Id值为1024 34 | 35 | eureka: 36 | instance: 37 | hostname: cloud-gateway-service 38 | client: #服务提供者provider注册进eureka服务列表内 39 | service-url: 40 | register-with-eureka: true 41 | fetch-registry: true 42 | defaultZone: http://eureka7001.com:7001/eureka 43 | 44 | #id: 我们自定义的路由ID,保持唯一 45 | #uri:目标服务地址 46 | #predicate:路由条件,Predicate接受一个输入参数返回一个布尔值。 47 | # 该属性包含多种默认方法来将Predicate组合成其他复杂的逻辑(比如:与、或、非) -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/test/java/T2.java: -------------------------------------------------------------------------------- 1 | import java.time.ZonedDateTime; 2 | 3 | /** 4 | * @author wangyang 5 | * @version V1.0 6 | * @create 2020-04-15-15:13 7 | */ 8 | public class T2 { 9 | public static void main(String[] args) { 10 | ZonedDateTime zbj = ZonedDateTime.now();//默认时区 11 | System.out.println(zbj); 12 | //2020-04-15T15:20:23.855+08:00[Asia/Shanghai] 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /cloud-gateway-gateway9527/src/test/java/com/wenren/test/ZonedDateTimeDemo.java: -------------------------------------------------------------------------------- 1 | package com.wenren.test; 2 | 3 | import java.time.ZoneId; 4 | import java.time.ZonedDateTime; 5 | 6 | /** 7 | * @author wangyang 8 | * @version V1.0 9 | * @create 2020-04-15-15:16 10 | */ 11 | public class ZonedDateTimeDemo { 12 | public static void main(String[] args) { 13 | ZonedDateTime zbj = ZonedDateTime.now();//默认时区 14 | System.out.println(zbj); 15 | // ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York"));//用指定时区获取当前时间 16 | // System.out.println(zny); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-provider-consul-payment8006/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-provider-consul-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 | 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 | true 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | -------------------------------------------------------------------------------- /cloud-provider-consul-payment8006/src/main/java/com/wenren/springcloud/PaymentMain8006.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-03-31-15:27 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class PaymentMain8006 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(PaymentMain8006.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-provider-consul-payment8006/src/main/java/com/wenren/springcloud/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import java.util.UUID; 9 | 10 | /** 11 | * @author wangyang 12 | * @version V1.0 13 | * @create 2020-03-31-15:28 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 paymentConsul() { 23 | return "springcloud with consul:" + serverPort + "\t" + UUID.randomUUID().toString(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /cloud-provider-consul-payment8006/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | ###服务端口号 2 | server: 3 | port: 8006 4 | 5 | spring: 6 | application: 7 | name: cloud-provider-consul-payment 8 | ###consul注册中心地址 9 | cloud: 10 | consul: 11 | host: 192.168.0.135 12 | port: 8500 13 | discovery: 14 | #hostname:127.0.0.1 15 | service-name: ${spring.application.name} 16 | -------------------------------------------------------------------------------- /cloud-provider-hystrix-payment8001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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 | 27 | com.wenren.springcloud 28 | cloud-api-commons 29 | ${project.version} 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-web 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-actuator 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-devtools 42 | runtime 43 | true 44 | 45 | 46 | org.projectlombok 47 | lombok 48 | true 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-starter-test 53 | test 54 | 55 | 56 | -------------------------------------------------------------------------------- /cloud-provider-hystrix-payment8001/src/main/java/com/wenren/springcloud/PaymentHystrixMain8001.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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.netflix.eureka.EnableEurekaClient; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | /** 12 | * @author wangyang 13 | * @version V1.0 14 | * @create 2020-04-09-11:56 15 | */ 16 | @SpringBootApplication 17 | @EnableEurekaClient 18 | @EnableCircuitBreaker 19 | public class PaymentHystrixMain8001 { 20 | public static void main(String[] args) { 21 | SpringApplication.run(PaymentHystrixMain8001.class, args); 22 | } 23 | 24 | /** 25 | * 此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后坑 26 | * ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream", 27 | * 只要在自己的项目里配置上下面的servlet就可以了 28 | */ 29 | @Bean 30 | public ServletRegistrationBean getServlet() { 31 | HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); 32 | ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); 33 | registrationBean.setLoadOnStartup(1); 34 | registrationBean.addUrlMappings("/hystrix.stream"); 35 | registrationBean.setName("HystrixMetricsStreamServlet"); 36 | return registrationBean; 37 | } 38 | } -------------------------------------------------------------------------------- /cloud-provider-hystrix-payment8001/src/main/java/com/wenren/springcloud/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import com.wenren.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 | * @author wangyang 14 | * @version V1.0 15 | * @create 2020-04-09-12:03 16 | */ 17 | @RestController 18 | @Slf4j 19 | public class PaymentController { 20 | @Resource 21 | private PaymentService paymentService; 22 | 23 | @Value("${server.port}") 24 | private String serverPort; 25 | 26 | @GetMapping("/payment/hystrix/ok/{id}") 27 | public String paymentInfo_OK(@PathVariable("id") Integer id) { 28 | String result = paymentService.paymentInfo_OK(id); 29 | log.info("*****result:" + result); 30 | return result; 31 | } 32 | 33 | @GetMapping("/payment/hystrix/timeout/{id}") 34 | public String paymentInfo_TimeOut(@PathVariable("id") Integer id) { 35 | String result = paymentService.paymentInfo_TimeOut(id); 36 | log.info("*****result:" + result); 37 | return result; 38 | } 39 | 40 | //======服务熔断 41 | @GetMapping("/payment/circuit/{id}") 42 | public String paymentCircuitBreaker(@PathVariable("id") Integer id) { 43 | String result = paymentService.paymentCircuitBreaker(id); 44 | log.info("*****result:" + result); 45 | return result; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /cloud-provider-hystrix-payment8001/src/main/java/com/wenren/springcloud/service/PaymentService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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.http.ResponseEntity; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | 10 | import java.util.concurrent.TimeUnit; 11 | 12 | /** 13 | * @author wangyang 14 | * @version V1.0 15 | * @create 2020-04-09-11:58 16 | */ 17 | @Service 18 | public class PaymentService { 19 | /** 20 | * 正常访问,肯定OK 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 | @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = { 30 | @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") 31 | }) 32 | public String paymentInfo_TimeOut(Integer id) { 33 | // int age = 10/0; 34 | int timeNumber = 3000; 35 | try { 36 | TimeUnit.MILLISECONDS.sleep(timeNumber); 37 | } catch (InterruptedException e) { 38 | e.printStackTrace(); 39 | } 40 | return "线程池:" + Thread.currentThread().getName() + " paymentInfo_TimeOut,id:" + id + "\t" + "O(∩_∩)O哈哈~" + " 耗时(秒)" + timeNumber; 41 | } 42 | 43 | public String paymentInfo_TimeOutHandler(Integer id) { 44 | return "线程池:" + Thread.currentThread().getName() + " 8001系统繁忙或者运行报错,请稍后再试,id:" + id + "\t" + "o(╥﹏╥)o"; 45 | } 46 | 47 | //======服务熔断 48 | @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = { 49 | @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//是否开启断路器 50 | @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),//请求次数 51 | @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),//时间窗口期 52 | @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),//失败率达到多少后跳闸 53 | }) 54 | public String paymentCircuitBreaker(@PathVariable("id") Integer id) { 55 | if (id < 0) { 56 | throw new RuntimeException("******id 不能负数"); 57 | } 58 | String serialNumber = IdUtil.simpleUUID(); 59 | 60 | return Thread.currentThread().getName() + "\t" + "调动成功,流水号:" + serialNumber; 61 | } 62 | 63 | public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id) { 64 | return "id 不能负数,请稍后再试,/(ToT)/~~ id:" + id; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /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 | 7 | eureka: 8 | client: 9 | register-with-eureka: true 10 | fetch-registry: true 11 | service-url: 12 | defaultZone: http://localhost:7001/eureka 13 | #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka -------------------------------------------------------------------------------- /cloud-provider-payment8001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.wenren.springcloud 7 | cloud2020 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-provider-payment8001 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-zipkin 19 | 20 | 21 | 22 | org.springframework.cloud 23 | spring-cloud-starter-netflix-eureka-client 24 | 25 | 26 | 27 | com.wenren.springcloud 28 | cloud-api-commons 29 | ${project.version} 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-web 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-actuator 38 | 39 | 40 | org.mybatis.spring.boot 41 | mybatis-spring-boot-starter 42 | 43 | 44 | com.alibaba 45 | druid-spring-boot-starter 46 | 1.1.10 47 | 48 | 49 | 50 | mysql 51 | mysql-connector-java 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-starter-jdbc 57 | 58 | 59 | org.springframework.boot 60 | spring-boot-devtools 61 | runtime 62 | true 63 | 64 | 65 | org.projectlombok 66 | lombok 67 | true 68 | 69 | 70 | org.springframework.boot 71 | spring-boot-starter-test 72 | test 73 | 74 | 75 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/java/com/wenren/springcloud/PaymentMain8001.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 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.netflix.eureka.EnableEurekaClient; 7 | 8 | /** 9 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-03-20-14:41 12 | */ 13 | @SpringBootApplication 14 | @EnableEurekaClient 15 | @EnableDiscoveryClient 16 | public class PaymentMain8001 { 17 | public static void main(String[] args) { 18 | SpringApplication.run(PaymentMain8001.class, args); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/java/com/wenren/springcloud/controller/PaymenController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import com.wenren.springcloud.entities.CommonResult; 4 | import com.wenren.springcloud.entities.Payment; 5 | import com.wenren.springcloud.service.PaymentService; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.cloud.client.ServiceInstance; 9 | import org.springframework.cloud.client.discovery.DiscoveryClient; 10 | import org.springframework.web.bind.annotation.*; 11 | 12 | import javax.annotation.Resource; 13 | import java.util.List; 14 | import java.util.concurrent.TimeUnit; 15 | 16 | /** 17 | * @author wangyang 18 | * @version V1.0 19 | * @create 2020-03-23-14:28 20 | */ 21 | @RestController 22 | @Slf4j 23 | public class PaymenController { 24 | @Resource 25 | private PaymentService paymentService; 26 | 27 | @Value("${server.port}") 28 | private String serverPort; 29 | 30 | @Resource 31 | private DiscoveryClient discoveryClient; 32 | 33 | @PostMapping(value = "/payment/create") 34 | public CommonResult create(@RequestBody Payment payment) { 35 | int result = paymentService.create(payment); 36 | log.info("******插入结果:" + result); 37 | 38 | if (result > 0) { 39 | return new CommonResult(200, "插入数据成功,serverPort:" + serverPort, result); 40 | } else { 41 | return new CommonResult(444, "插入数据失败", null); 42 | } 43 | } 44 | 45 | @GetMapping(value = "/payment/get/{id}") 46 | public CommonResult getPaymentById(@PathVariable("id") Long id) { 47 | Payment payment = paymentService.getPaymentById(id); 48 | log.info("******插入结果:" + payment + "\t" + "O(∩_∩)O哈哈~"); 49 | 50 | if (payment != null) { 51 | return new CommonResult(200, "查询成功,serverPort:" + serverPort, payment); 52 | } else { 53 | return new CommonResult(444, "没有对应记录,查询ID:" + id, null); 54 | } 55 | } 56 | 57 | @GetMapping(value = "/payment/discovery") 58 | public Object discovery() { 59 | List services = discoveryClient.getServices(); 60 | for (String service : services) { 61 | log.info("*****service:" + service); 62 | } 63 | 64 | List instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); 65 | for (ServiceInstance instance : instances) { 66 | log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri()); 67 | } 68 | 69 | return this.discoveryClient; 70 | } 71 | 72 | @GetMapping(value = "/payment/lb") 73 | public String getPaymentLB() { 74 | return serverPort; 75 | } 76 | 77 | @GetMapping(value = "payment/feign/timeout") 78 | public String paymentFeignTimeout() { 79 | try { 80 | TimeUnit.SECONDS.sleep(3); 81 | } catch (InterruptedException e) { 82 | e.printStackTrace(); 83 | } 84 | 85 | return serverPort; 86 | } 87 | 88 | @GetMapping("/payment/zipkin") 89 | public String paymentZipkin() { 90 | return "Hi, i'm paymentzipkin server fall back, welcome to wenren home, O(∩_∩)O哈哈~"; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/java/com/wenren/springcloud/dao/PaymentDao.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.dao; 2 | 3 | import com.wenren.springcloud.entities.Payment; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-03-23-14:01 11 | */ 12 | @Mapper 13 | public interface PaymentDao { 14 | public int create(Payment payment); 15 | 16 | public Payment getPaymentById(@Param("id") Long id); 17 | } 18 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/java/com/wenren/springcloud/impl/PaymentServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.impl; 2 | 3 | import com.wenren.springcloud.dao.PaymentDao; 4 | import com.wenren.springcloud.entities.Payment; 5 | import com.wenren.springcloud.service.PaymentService; 6 | import org.apache.ibatis.annotations.Param; 7 | import org.springframework.stereotype.Service; 8 | 9 | import javax.annotation.Resource; 10 | 11 | /** 12 | * @author wangyang 13 | * @version V1.0 14 | * @create 2020-03-23-14:25 15 | */ 16 | @Service 17 | public class PaymentServiceImpl implements PaymentService { 18 | @Resource 19 | private PaymentDao paymentDao; 20 | 21 | @Override 22 | public int create(Payment payment) { 23 | return paymentDao.create(payment); 24 | } 25 | 26 | @Override 27 | public Payment getPaymentById(@Param("id") Long id) { 28 | return paymentDao.getPaymentById(id); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/java/com/wenren/springcloud/service/PaymentService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.service; 2 | 3 | import com.wenren.springcloud.entities.Payment; 4 | import org.apache.ibatis.annotations.Param; 5 | 6 | /** 7 | * @author wangyang 8 | * @version V1.0 9 | * @create 2020-03-23-14:21 10 | */ 11 | public interface PaymentService { 12 | public int create(Payment payment); 13 | 14 | public Payment getPaymentById(@Param("id") Long id); 15 | } 16 | -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8001 3 | spring: 4 | application: 5 | name: cloud-payment-service 6 | zipkin: 7 | base-url: http://localhost:9411 8 | sleuth: 9 | sampler: 10 | probability: 1 #采样率介于0到1之间,1则表示全部采集 11 | datasource: 12 | type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型 13 | driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包 14 | url: jdbc:mysql://123.125.5.26:6306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false #公司公网 15 | #url: jdbc:mysql://192.168.0.135:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false #公司内网 16 | #url: jdbc:mysql://192.168.123.135:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false #家 17 | username: root 18 | password: Perfect1 19 | 20 | eureka: 21 | client: #客户端注册进eureka服务列表内 22 | #表示是否将自己注册进EurekaServer默认为true。 23 | register-with-eureka: true 24 | #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置true才能配合ribbon使用负载均衡 25 | fetch-registry: true 26 | service-url: 27 | #单机版 28 | #defaultZone: http://localhost:7001/eureka 29 | #集群版 30 | defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka 31 | 32 | instance: 33 | instance-id: payment8001 34 | #访问路径可以显示IP地址 35 | prefer-ip-address: true 36 | #心跳检测与持续时间 37 | #开发时设置小些,保证服务关闭后注册中心能及时剔除服务 38 | #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒) 39 | lease-renewal-interval-in-seconds: 1 40 | #Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务 41 | lease-expiration-duration-in-seconds: 2 42 | 43 | mybatis: 44 | mapper-locations: classpath:mapper/*.xml 45 | type-aliases-package: com.wenren.springcloud.entities #所有Entity别名类所在包 -------------------------------------------------------------------------------- /cloud-provider-payment8001/src/main/resources/mapper/PaymentMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | insert into payment (serial) values (#{serial}); 7 | 8 | 9 | 10 | 11 | 12 | 13 | 16 | 17 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-provider-payment8002 13 | 14 | 15 | 16 | 17 | 18 | org.springframework.cloud 19 | spring-cloud-starter-netflix-eureka-client 20 | 21 | 22 | 23 | com.wenren.springcloud 24 | cloud-api-commons 25 | ${project.version} 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 | org.mybatis.spring.boot 37 | mybatis-spring-boot-starter 38 | 39 | 40 | com.alibaba 41 | druid-spring-boot-starter 42 | 1.1.10 43 | 44 | 45 | 46 | mysql 47 | mysql-connector-java 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-starter-jdbc 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-devtools 57 | runtime 58 | true 59 | 60 | 61 | org.projectlombok 62 | lombok 63 | true 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-starter-test 68 | test 69 | 70 | 71 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/java/com/wenren/springcloud/PaymentMain8002.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 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.netflix.eureka.EnableEurekaClient; 7 | 8 | /** 9 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-03-26-14:52 12 | */ 13 | @SpringBootApplication 14 | @EnableEurekaClient 15 | @EnableDiscoveryClient 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/com/wenren/springcloud/controller/PaymenController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import com.wenren.springcloud.entities.CommonResult; 4 | import com.wenren.springcloud.entities.Payment; 5 | import com.wenren.springcloud.service.PaymentService; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.cloud.client.ServiceInstance; 9 | import org.springframework.cloud.client.discovery.DiscoveryClient; 10 | import org.springframework.web.bind.annotation.*; 11 | 12 | import javax.annotation.Resource; 13 | import java.util.List; 14 | 15 | /** 16 | * @author wangyang 17 | * @version V1.0 18 | * @create 2020-03-23-14:28 19 | */ 20 | @RestController 21 | @Slf4j 22 | public class PaymenController { 23 | @Resource 24 | private PaymentService paymentService; 25 | 26 | @Value("${server.port}") 27 | private String serverPort; 28 | 29 | @Resource 30 | private DiscoveryClient discoveryClient; 31 | 32 | @PostMapping(value = "/payment/create") 33 | public CommonResult create(@RequestBody Payment payment) { 34 | int result = paymentService.create(payment); 35 | log.info("******插入结果:" + result); 36 | 37 | if (result > 0) { 38 | return new CommonResult(200, "插入数据成功,serverPort:" + serverPort, result); 39 | } else { 40 | return new CommonResult(444, "插入数据失败", null); 41 | } 42 | } 43 | 44 | @GetMapping(value = "/payment/get/{id}") 45 | public CommonResult create(@PathVariable("id") Long id) { 46 | Payment payment = paymentService.getPaymentById(id); 47 | log.info("******插入结果:" + payment + "\t" + "O(∩_∩)O哈哈~"); 48 | 49 | if (payment != null) { 50 | return new CommonResult(200, "查询成功,serverPort:" + serverPort, payment); 51 | } else { 52 | return new CommonResult(444, "没有对应记录,查询ID:" + id, null); 53 | } 54 | } 55 | 56 | @GetMapping(value = "/payment/discovery") 57 | public Object discovery() { 58 | List services = discoveryClient.getServices(); 59 | for (String service : services) { 60 | log.info("*****service:" + service); 61 | } 62 | 63 | List instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); 64 | for (ServiceInstance instance : instances) { 65 | log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri()); 66 | } 67 | 68 | return this.discoveryClient; 69 | } 70 | 71 | @GetMapping(value = "/payment/lb") 72 | public String getPaymentLB() { 73 | return serverPort; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/java/com/wenren/springcloud/dao/PaymentDao.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.dao; 2 | 3 | import com.wenren.springcloud.entities.Payment; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-03-23-14:01 11 | */ 12 | @Mapper 13 | public interface PaymentDao { 14 | public int create(Payment payment); 15 | 16 | public Payment getPaymentById(@Param("id") Long id); 17 | } 18 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/java/com/wenren/springcloud/impl/PaymentServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.impl; 2 | 3 | import com.wenren.springcloud.dao.PaymentDao; 4 | import com.wenren.springcloud.entities.Payment; 5 | import com.wenren.springcloud.service.PaymentService; 6 | import org.apache.ibatis.annotations.Param; 7 | import org.springframework.stereotype.Service; 8 | 9 | import javax.annotation.Resource; 10 | 11 | /** 12 | * @author wangyang 13 | * @version V1.0 14 | * @create 2020-03-23-14:25 15 | */ 16 | @Service 17 | public class PaymentServiceImpl implements PaymentService { 18 | @Resource 19 | private PaymentDao paymentDao; 20 | 21 | @Override 22 | public int create(Payment payment) { 23 | return paymentDao.create(payment); 24 | } 25 | 26 | @Override 27 | public Payment getPaymentById(@Param("id") Long id) { 28 | return paymentDao.getPaymentById(id); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/java/com/wenren/springcloud/service/PaymentService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.service; 2 | 3 | import com.wenren.springcloud.entities.Payment; 4 | import org.apache.ibatis.annotations.Param; 5 | 6 | /** 7 | * @author wangyang 8 | * @version V1.0 9 | * @create 2020-03-23-14:21 10 | */ 11 | public interface PaymentService { 12 | public int create(Payment payment); 13 | 14 | public Payment getPaymentById(@Param("id") Long id); 15 | } 16 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8002 3 | spring: 4 | application: 5 | name: cloud-payment-service 6 | datasource: 7 | type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型 8 | driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包 9 | url: jdbc:mysql://123.125.5.26:6306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false #公司公网 10 | #url: jdbc:mysql://192.168.0.135:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false #公司内网 11 | #url: jdbc:mysql://192.168.123.135:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false #家 12 | username: root 13 | password: Perfect1 14 | 15 | eureka: 16 | client: #客户端注册进eureka服务列表内 17 | #表示是否将自己注册进EurekaServer默认为true。 18 | register-with-eureka: true 19 | #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置true才能配合ribbon使用负载均衡 20 | fetch-registry: true 21 | service-url: 22 | #defaultZone: http://localhost:7001/eureka 23 | defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版 24 | instance: 25 | instance-id: payment8002 26 | prefer-ip-address: true #访问路径可以显示IP地址 27 | 28 | mybatis: 29 | mapper-locations: classpath:mapper/*.xml 30 | type-aliases-package: com.wenren.springcloud.entities #所有Entity别名类所在包 31 | -------------------------------------------------------------------------------- /cloud-provider-payment8002/src/main/resources/mapper/PaymentMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | insert into payment (serial) values (#{serial}); 7 | 8 | 9 | 10 | 11 | 12 | 13 | 16 | 17 | -------------------------------------------------------------------------------- /cloud-provider-zk-payment8004/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-provider-zk-payment8004 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | 22 | com.wenren.springcloud 23 | cloud-api-commons 24 | ${project.version} 25 | 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-zookeeper-discovery 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-devtools 34 | runtime 35 | true 36 | 37 | 38 | org.projectlombok 39 | lombok 40 | true 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | -------------------------------------------------------------------------------- /cloud-provider-zk-payment8004/src/main/java/com/wenren/springcloud/PaymentMain8004.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-03-28-14:33 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务 14 | public class PaymentMain8004 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(PaymentMain8004.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-provider-zk-payment8004/src/main/java/com/wenren/springcloud/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import java.util.UUID; 9 | 10 | /** 11 | * @author wangyang 12 | * @version V1.0 13 | * @create 2020-03-28-14:38 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 paymentzk() { 23 | return "springcloud with zookeeper:" + serverPort + "\t" + UUID.randomUUID().toString(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /cloud-provider-zk-payment8004/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | #8004表示注册到zookeeper服务的支付服务提供者端口号 2 | server: 3 | port: 8004 4 | #服务别名----注册zookeeper到注册中心名称 5 | spring: 6 | application: 7 | name: cloud-provider-zk-payment 8 | cloud: 9 | zookeeper: 10 | connect-string: 123.125.5.26:2182 11 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8802/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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.boot 21 | spring-boot-starter-actuator 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-netflix-eureka-client 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-stream-rabbit 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-devtools 35 | runtime 36 | true 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | true 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8802/src/main/java/com/wenren/springcloud/StreamMQMain8802.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; 6 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 7 | 8 | /** 9 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-05-08-14:14 12 | */ 13 | @SpringBootApplication(exclude = {RabbitAutoConfiguration.class}) 14 | public class StreamMQMain8802 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(StreamMQMain8802.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8802/src/main/java/com/wenren/springcloud/controller/ReceiveMessageListenerController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 12 | * @version V1.0 13 | * @create 2020-05-08-14:15 14 | */ 15 | @Component 16 | @EnableBinding(Sink.class) 17 | public class ReceiveMessageListenerController { 18 | @Value("${server.port}") 19 | private String serverPort; 20 | 21 | @StreamListener(Sink.INPUT) 22 | public void input(Message message) { 23 | System.out.println("消费者1号,----->接收到的消息: " + message.getPayload() + "\t port: " + serverPort); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /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: 123.125.5.26 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: wenrenA 25 | eureka: 26 | client: 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: receive-8802.com #在信息列表时显示主机名称 33 | prefer-ip-address: true #访问的路径变为IP地址 34 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8803/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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.boot 21 | spring-boot-starter-actuator 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-netflix-eureka-client 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-stream-rabbit 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-devtools 35 | runtime 36 | true 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | true 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8803/src/main/java/com/wenren/springcloud/StreamMQMain8803.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-09-15:08 11 | */ 12 | @SpringBootApplication(exclude = {RabbitAutoConfiguration.class}) 13 | public class StreamMQMain8803 { 14 | public static void main(String[] args) { 15 | SpringApplication.run(StreamMQMain8803.class, args); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-consumer8803/src/main/java/com/wenren/springcloud/controller/ReceiveMessageListenerController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 12 | * @version V1.0 13 | * @create 2020-05-09-15:08 14 | */ 15 | @Component 16 | @EnableBinding(Sink.class) 17 | public class ReceiveMessageListenerController { 18 | @Value("${server.port}") 19 | private String serverPort; 20 | 21 | @StreamListener(Sink.INPUT) 22 | public void input(Message message) { 23 | System.out.println("消费者2号,----->接收到的消息: " + message.getPayload() + "\t port: " + serverPort); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /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: 123.125.5.26 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: wenrenA 25 | eureka: 26 | client: 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: receive-8803.com #在信息列表时显示主机名称 33 | prefer-ip-address: true #访问的路径变为IP地址 34 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloud-stream-rabbitmq-provider8801 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-actuator 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-netflix-eureka-client 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-stream-rabbit 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-devtools 35 | runtime 36 | true 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | true 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/src/main/java/com/wenren/springcloud/StreamMQMain8801.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-07-16:09 11 | */ 12 | //多environment下关闭,不然会在服务器启动时默认加载Top Level的RabbitMQ配置,会报错,但不影响会使用。 13 | @SpringBootApplication(exclude = {RabbitAutoConfiguration.class}) 14 | public class StreamMQMain8801 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(StreamMQMain8801.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/src/main/java/com/wenren/springcloud/controller/SendMessageController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import com.wenren.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 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-05-07-16:32 13 | */ 14 | @RestController 15 | public class SendMessageController { 16 | @Resource 17 | private IMessageProvider messageProvider; 18 | 19 | @GetMapping(value = "/sendMessage") 20 | public String sendMessage() { 21 | return messageProvider.send(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/src/main/java/com/wenren/springcloud/service/IMessageProvider.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.service; 2 | 3 | /** 4 | * @author wangyang 5 | * @version V1.0 6 | * @create 2020-05-07-16:20 7 | */ 8 | public interface IMessageProvider { 9 | public String send(); 10 | } 11 | -------------------------------------------------------------------------------- /cloud-stream-rabbitmq-provider8801/src/main/java/com/wenren/springcloud/service/impl/MessageProviderImpl.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.service.impl; 2 | 3 | import com.wenren.springcloud.service.IMessageProvider; 4 | import org.springframework.cloud.stream.annotation.EnableBinding; 5 | import org.springframework.cloud.stream.messaging.Source; 6 | import org.springframework.messaging.MessageChannel; 7 | import org.springframework.messaging.support.MessageBuilder; 8 | 9 | import javax.annotation.Resource; 10 | import java.util.UUID; 11 | 12 | /** 13 | * @author wangyang 14 | * @version V1.0 15 | * @create 2020-05-07-16:21 16 | */ 17 | @EnableBinding(Source.class) //定义消息的推送管道 18 | public class MessageProviderImpl implements IMessageProvider { 19 | @Resource 20 | private MessageChannel output; //消息发送通道 21 | 22 | @Override 23 | public String send() { 24 | String serial = UUID.randomUUID().toString(); 25 | output.send(MessageBuilder.withPayload(serial).build()); //创建并发送消息 26 | System.out.println("*****serial: " + serial); 27 | return null; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /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: 123.125.5.26 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: 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地址 -------------------------------------------------------------------------------- /cloudalibaba-config-nacos-client3377/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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 | true 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | -------------------------------------------------------------------------------- /cloudalibaba-config-nacos-client3377/src/main/java/com/wenren/springcloud/alibaba/NacosConfigClientMain3377.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-14-14:44 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class NacosConfigClientMain3377 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(NacosConfigClientMain3377.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloudalibaba-config-nacos-client3377/src/main/java/com/wenren/springcloud/alibaba/controller/ConfigClientController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-05-14-14:44 12 | */ 13 | @RestController 14 | @RefreshScope //支持Nacos的动态刷新功能。 15 | public class ConfigClientController { 16 | @Value("${config.info}") 17 | private String configInfo; 18 | 19 | @GetMapping("/config/info") 20 | public String getConfigInfo() { 21 | return configInfo; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /cloudalibaba-config-nacos-client3377/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev #表示开发环境 4 | #active: test #表示测试环境 5 | #active: info 6 | -------------------------------------------------------------------------------- /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: 192.168.0.135:8848 #Nacos服务注册中心地址 12 | config: 13 | server-addr: 192.168.0.135:8848 #Nacos作为配置中心地址 14 | file-extension: yaml #指定yaml格式的配置 15 | group: DEV_GROUP 16 | #group: TEST_GROUP 17 | namespace: 29ca61d8-db3e-4ec9-882f-92e73bf5bb53 18 | 19 | # ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension} 20 | # nacos-config-client-dev.yaml 21 | 22 | # nacos-config-client-test.yaml ---> config.info -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order83/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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.wenren.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 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order83/src/main/java/com/wenren/springcloud/OrderNacosMain83.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-14-11:55 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class OrderNacosMain83 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(OrderNacosMain83.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order83/src/main/java/com/wenren/springcloud/config/ApplicationContextBean.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-05-14-11:57 12 | */ 13 | @Configuration 14 | public class ApplicationContextBean { 15 | @Bean 16 | @LoadBalanced 17 | public RestTemplate getRestTemplate() { 18 | return new RestTemplate(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order83/src/main/java/com/wenren/springcloud/controller/OrderNacosController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.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 | * @author wangyang 14 | * @version V1.0 15 | * @create 2020-05-14-11:59 16 | */ 17 | @RestController 18 | @Slf4j 19 | public class OrderNacosController { 20 | @Resource 21 | private RestTemplate restTemplate; 22 | 23 | @Value("${service-url.nacos-user-service}") 24 | private String serverURL; 25 | 26 | @GetMapping(value = "/consumer/payment/nacos/{id}") 27 | public String paymentInfo(@PathVariable("id") Long id) { 28 | return restTemplate.getForObject(serverURL + "/payment/nacos/" + id, String.class); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order83/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 83 3 | 4 | spring: 5 | application: 6 | name: nacos-order-consumer 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: 192.168.0.135:8848 #配置Nacos地址 11 | 12 | #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者) 13 | service-url: 14 | nacos-user-service: http://nacos-payment-provider -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloudalibaba-consumer-nacos-order84 13 | 14 | 15 | 16 | 17 | com.alibaba.cloud 18 | spring-cloud-starter-alibaba-nacos-discovery 19 | 20 | 21 | 22 | com.wenren.springcloud 23 | cloud-api-commons 24 | ${project.version} 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 | org.projectlombok 59 | lombok 60 | true 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-test 65 | test 66 | 67 | 68 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/java/com/wenren/springcloud/OrderNacosMain84.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud; 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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-05-14-11:55 12 | */ 13 | @SpringBootApplication 14 | @EnableDiscoveryClient 15 | @EnableFeignClients 16 | public class OrderNacosMain84 { 17 | public static void main(String[] args) { 18 | SpringApplication.run(OrderNacosMain84.class, args); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/java/com/wenren/springcloud/config/ApplicationContextBean.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-05-14-11:57 12 | */ 13 | @Configuration 14 | public class ApplicationContextBean { 15 | @Bean 16 | @LoadBalanced 17 | public RestTemplate getRestTemplate() { 18 | return new RestTemplate(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/java/com/wenren/springcloud/controller/CircleBreakerController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.controller; 2 | 3 | import com.alibaba.csp.sentinel.annotation.SentinelResource; 4 | import com.alibaba.csp.sentinel.slots.block.BlockException; 5 | import com.wenren.springcloud.entities.CommonResult; 6 | import com.wenren.springcloud.entities.Payment; 7 | import com.wenren.springcloud.service.PaymentService; 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 | * @author wangyang 19 | * @version V1.0 20 | * @create 2020-05-21-11:53 21 | */ 22 | @RestController 23 | @Slf4j 24 | public class CircleBreakerController { 25 | public static final String SERVICE_URL = "http://nacos-payment-provider"; 26 | 27 | @Resource 28 | private RestTemplate restTemplate; 29 | 30 | @RequestMapping("/consumer/fallback/{id}") 31 | // @SentinelResource(value = "fallback") //没有配置 32 | // @SentinelResource(value = "fallback", fallback = "handlerFallback") //fallback只负责业务异常 33 | // @SentinelResource(value = "fallback", blockHandler = "blockHandler") //blockHandler只负责Sentinel控制台配置违规 34 | // @SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler") 35 | @SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler", 36 | exceptionsToIgnore = {IllegalArgumentException.class}) 37 | public CommonResult fallback(@PathVariable Long id) { 38 | CommonResult result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id); 39 | 40 | if (id == 4) { 41 | throw new IllegalArgumentException("IllegalArgumentException,非法参数异常......"); 42 | } else if (result.getData() == null) { 43 | throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常"); 44 | } 45 | 46 | return result; 47 | } 48 | 49 | //本例是fallback 50 | public CommonResult handlerFallback(@PathVariable Long id, Throwable e) { 51 | Payment payment = new Payment(id, "null"); 52 | return new CommonResult<>(444, "兜底异常handlerFallback,exception内容:" + e.getMessage(), payment); 53 | } 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/openfeign/paymentSQL/{id}") 66 | public CommonResult paymentSQL(@PathVariable("id") Long id) { 67 | return paymentService.paymentSQL(id); 68 | } 69 | } -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/java/com/wenren/springcloud/service/PaymentFallbackService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.service; 2 | 3 | import com.wenren.springcloud.entities.CommonResult; 4 | import com.wenren.springcloud.entities.Payment; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-21-15:01 11 | */ 12 | @Component 13 | public class PaymentFallbackService implements PaymentService { 14 | @Override 15 | public CommonResult paymentSQL(Long id) { 16 | return new CommonResult<>(44444, "服务降级返回,---PaymentFallbackService", new Payment(id, "errorSerial")); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/java/com/wenren/springcloud/service/PaymentService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.service; 2 | 3 | import com.wenren.springcloud.entities.CommonResult; 4 | import com.wenren.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 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-05-21-14:53 13 | */ 14 | @FeignClient(value = "nacos-payment-provider", fallback = PaymentFallbackService.class) 15 | public interface PaymentService { 16 | @GetMapping(value = "/paymentSQL/{id}") 17 | public CommonResult paymentSQL(@PathVariable("id") Long id); 18 | } 19 | -------------------------------------------------------------------------------- /cloudalibaba-consumer-nacos-order84/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 84 3 | 4 | spring: 5 | application: 6 | name: nacos-order-consumer 7 | cloud: 8 | nacos: 9 | discovery: 10 | server-addr: 192.168.0.135:1111 #配置Nacos地址 11 | sentinel: 12 | transport: 13 | dashboard: localhost:8080 #配置Sentinel Dashboard地址 14 | port: 8719 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口 15 | 16 | #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者) 17 | service-url: 18 | nacos-user-service: http://nacos-payment-provider 19 | 20 | #激活Sentinel对Feign的支持 21 | feign: 22 | sentinel: 23 | enabled: true 24 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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 | true 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9001/src/main/java/com/wenren/springcloud/alibaba/PaymentMain9001.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-13-17:35 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class PaymentMain9001 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(PaymentMain9001.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9001/src/main/java/com/wenren/springcloud/alibaba/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-05-13-17:36 12 | */ 13 | @RestController 14 | public class PaymentController { 15 | @Value("${server.port}") 16 | private String serverPort; 17 | 18 | @GetMapping(value = "/payment/nacos/{id}") 19 | public String getPayment(@PathVariable("id") Integer id) { 20 | return "nacos registry, serverPort: " + serverPort + "\t id" + id; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /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: 192.168.0.135:8848 #配置Nacos地址 11 | 12 | management: 13 | endpoints: 14 | web: 15 | exposure: 16 | include: '*' 17 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9002/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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 | true 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9002/src/main/java/com/wenren/springcloud/alibaba/PaymentMain9002.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-14-09:59 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class PaymentMain9002 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(PaymentMain9002.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9002/src/main/java/com/wenren/springcloud/alibaba/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-05-14-09:59 12 | */ 13 | @RestController 14 | public class PaymentController { 15 | @Value("${server.port}") 16 | private String serverPort; 17 | 18 | @GetMapping(value = "/payment/nacos/{id}") 19 | public String getPayment(@PathVariable("id") Integer id) { 20 | return "nacos registry, serverPort: " + serverPort + "\t id" + id; 21 | } 22 | } -------------------------------------------------------------------------------- /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: 192.168.0.135:8848 #配置Nacos地址 11 | server-addr: 123.125.5.26:1111 #换成Nginx的1111端口,做集群 12 | 13 | management: 14 | endpoints: 15 | web: 16 | exposure: 17 | include: '*' 18 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9003/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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 | 22 | com.wenren.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 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9003/src/main/java/com/wenren/springcloud/alibaba/PaymentMain9003.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-21-11:25 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class PaymentMain9003 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(PaymentMain9003.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9003/src/main/java/com/wenren/springcloud/alibaba/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.controller; 2 | 3 | import com.wenren.springcloud.entities.CommonResult; 4 | import com.wenren.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 | * @author wangyang 14 | * @version V1.0 15 | * @create 2020-05-21-11:26 16 | */ 17 | @RestController 18 | public class PaymentController { 19 | @Value("${server.port}") 20 | private String serverPort; 21 | 22 | public static HashMap hashMap = new HashMap<>(); 23 | 24 | static { 25 | hashMap.put(1L, new Payment(1L, "8229b054d47542ac9dbe36c8091f4a26")); 26 | hashMap.put(2L, new Payment(2L, "31d86d8a000f4491a20ffc990c81de30")); 27 | hashMap.put(3L, new Payment(3L, "06511bfcdf2747cbb433c15cff4f5c0b")); 28 | } 29 | 30 | @GetMapping(value = "/paymentSQL/{id}") 31 | public CommonResult paymentSQL(@PathVariable("id") Long id) { 32 | Payment payment = hashMap.get(id); 33 | CommonResult result = new CommonResult<>(200, "from mysql,serverPort: " + serverPort, payment); 34 | return result; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /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: 192.168.0.135:1111 #配置Nacos地址 11 | 12 | management: 13 | endpoints: 14 | web: 15 | exposure: 16 | include: '*' 17 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9004/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.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 | 22 | com.wenren.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 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9004/src/main/java/com/wenren/springcloud/alibaba/PaymentMain9004.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-21-11:25 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class PaymentMain9004 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(PaymentMain9004.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloudalibaba-provider-payment9004/src/main/java/com/wenren/springcloud/alibaba/controller/PaymentController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.controller; 2 | 3 | import com.wenren.springcloud.entities.CommonResult; 4 | import com.wenren.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 | * @author wangyang 14 | * @version V1.0 15 | * @create 2020-05-21-11:26 16 | */ 17 | @RestController 18 | public class PaymentController { 19 | @Value("${server.port}") 20 | private String serverPort; 21 | 22 | public static HashMap hashMap = new HashMap<>(); 23 | 24 | static { 25 | hashMap.put(1L, new Payment(1L, "8229b054d47542ac9dbe36c8091f4a26")); 26 | hashMap.put(2L, new Payment(2L, "31d86d8a000f4491a20ffc990c81de30")); 27 | hashMap.put(3L, new Payment(3L, "06511bfcdf2747cbb433c15cff4f5c0b")); 28 | } 29 | 30 | @GetMapping(value = "/paymentSQL/{id}") 31 | public CommonResult paymentSQL(@PathVariable("id") Long id) { 32 | Payment payment = hashMap.get(id); 33 | CommonResult result = new CommonResult<>(200, "from mysql,serverPort: " + serverPort, payment); 34 | return result; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /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: 192.168.0.135:1111 #配置Nacos地址 11 | 12 | management: 13 | endpoints: 14 | web: 15 | exposure: 16 | include: '*' 17 | -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | cloudalibaba-sentinel-service8401 13 | 14 | 15 | 16 | 17 | com.alibaba.cloud 18 | spring-cloud-starter-alibaba-nacos-discovery 19 | 20 | 21 | 22 | com.wenren.springcloud 23 | cloud-api-commons 24 | ${project.version} 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 | org.projectlombok 59 | lombok 60 | true 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-test 65 | test 66 | 67 | 68 | -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/src/main/java/com/wenren/springcloud/alibaba/MainApp8401.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-18-14:21 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class MainApp8401 { 15 | public static void main(String[] args) { 16 | SpringApplication.run(MainApp8401.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/src/main/java/com/wenren/springcloud/alibaba/controller/FlowLimitController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.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 | import java.util.concurrent.TimeUnit; 11 | 12 | /** 13 | * @author wangyang 14 | * @version V1.0 15 | * @create 2020-05-18-14:21 16 | */ 17 | @RestController 18 | @Slf4j 19 | public class FlowLimitController { 20 | @GetMapping("/testA") 21 | public String testA() { 22 | //暂停毫秒 23 | // try { 24 | // TimeUnit.MILLISECONDS.sleep(800); 25 | // } catch (InterruptedException e) { 26 | // e.printStackTrace(); 27 | // } 28 | return "------testA"; 29 | } 30 | 31 | @GetMapping("/testB") 32 | public String testB() { 33 | log.info(Thread.currentThread().getName() + "\t" + "...testB"); 34 | return "------testB"; 35 | } 36 | 37 | @GetMapping("/testD") 38 | public String testD() { 39 | // //暂停几秒种线程 40 | // try { 41 | // TimeUnit.SECONDS.sleep(1); 42 | // } catch (InterruptedException e) { 43 | // e.printStackTrace(); 44 | // } 45 | // log.info("testD 测试RT"); 46 | log.info("testD 异常比例"); 47 | int age = 10 / 0; 48 | return "------testD"; 49 | } 50 | 51 | @GetMapping("/testE") 52 | public String testE() { 53 | log.info("testE 测试异常数"); 54 | int age = 10 / 0; 55 | return "------testE 测试异常数"; 56 | } 57 | 58 | @GetMapping("/testHotKey") 59 | @SentinelResource(value = "testHotKey", blockHandler = "deal_testHotKey") 60 | public String testHotKey(@RequestParam(value = "p1", required = false) String p1, 61 | @RequestParam(value = "p2", required = false) String p2) { 62 | int age = 10 / 0; 63 | return "------testHotKey"; 64 | } 65 | 66 | public String deal_testHotKey(String p1, String p2, BlockException exception) { 67 | //Sentinel系统默认的提示:Blocked by Sentinel(flow limiting) 68 | return "------deal_testHotKey,o(╥﹏╥)o"; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/src/main/java/com/wenren/springcloud/alibaba/controller/RateLimitController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.controller; 2 | 3 | import com.alibaba.csp.sentinel.annotation.SentinelResource; 4 | import com.alibaba.csp.sentinel.slots.block.BlockException; 5 | import com.wenren.springcloud.alibaba.myhandler.CustomerBlockHandler; 6 | import com.wenren.springcloud.entities.CommonResult; 7 | import com.wenren.springcloud.entities.Payment; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | /** 12 | * @author wangyang 13 | * @version V1.0 14 | * @create 2020-05-20-14:06 15 | */ 16 | @RestController 17 | public class RateLimitController { 18 | @GetMapping("/byResource") 19 | @SentinelResource(value = "byResource", blockHandler = "handleException") 20 | public CommonResult byResource() { 21 | return new CommonResult(200, "按资源名称限流测试OK", new Payment(2020L, "serial001")); 22 | } 23 | 24 | public CommonResult handleException(BlockException exception) { 25 | return new CommonResult(444, exception.getClass().getCanonicalName() + "\t 服务不可用"); 26 | } 27 | 28 | @GetMapping("/rateLimit/byUrl") 29 | @SentinelResource(value = "byUrl") 30 | public CommonResult byUrl() { 31 | return new CommonResult(200, "按url限流测试OK", new Payment(2020L, "serial002")); 32 | } 33 | 34 | //CustomerBlockHandler 35 | @GetMapping("/rateLimit/customerBlockHandler") 36 | @SentinelResource(value = "customerBlockHandler", 37 | blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handlerException2") 38 | public CommonResult customerBlockHandler() { 39 | return new CommonResult(200, "按客户自定义测试OK", new Payment(2020L, "serial003")); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /cloudalibaba-sentinel-service8401/src/main/java/com/wenren/springcloud/alibaba/myhandler/CustomerBlockHandler.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.myhandler; 2 | 3 | import com.alibaba.csp.sentinel.slots.block.BlockException; 4 | import com.wenren.springcloud.entities.CommonResult; 5 | 6 | /** 7 | * @author wangyang 8 | * @version V1.0 9 | * @create 2020-05-20-15:17 10 | */ 11 | public class CustomerBlockHandler { 12 | public static CommonResult handlerException(BlockException exception) { 13 | return new CommonResult(4444, "按客户自定义,global handerException----1"); 14 | } 15 | 16 | public static CommonResult handlerException2(BlockException exception) { 17 | return new CommonResult(4444, "按客户自定义,global handerException----2"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /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: 192.168.0.135:1111 #Nacos服务注册中心地址 11 | sentinel: 12 | transport: 13 | dashboard: localhost:8080 #配置Sentinel Dashboard地址 14 | port: 8719 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口 15 | datasource: 16 | ds1: 17 | nacos: 18 | server-addr: 192.168.0.135:1111 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 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/java/com/wenren/springcloud/alibaba/SeataAccountMainApp2003.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.cloud.openfeign.EnableFeignClients; 8 | 9 | /** 10 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-05-25-14:18 13 | */ 14 | @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //取消数据源的自动创建 15 | @EnableDiscoveryClient 16 | @EnableFeignClients 17 | public class SeataAccountMainApp2003 { 18 | public static void main(String[] args) { 19 | SpringApplication.run(SeataAccountMainApp2003.class, args); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/java/com/wenren/springcloud/alibaba/config/DataSourceProxyConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.config; 2 | 3 | import com.alibaba.druid.pool.DruidDataSource; 4 | import io.seata.rm.datasource.DataSourceProxy; 5 | import org.apache.ibatis.session.SqlSessionFactory; 6 | import org.mybatis.spring.SqlSessionFactoryBean; 7 | import org.mybatis.spring.transaction.SpringManagedTransactionFactory; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.boot.context.properties.ConfigurationProperties; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 13 | 14 | import javax.sql.DataSource; 15 | 16 | /** 17 | * @author wangyang 18 | * @version V1.0 19 | * @create 2020-05-25-14:43 20 | * 使用Seata对数据源进行代理 21 | */ 22 | @Configuration 23 | public class DataSourceProxyConfig { 24 | @Value("${mybatis.mapperLocations}") 25 | private String mapperLocations; 26 | 27 | @Bean 28 | @ConfigurationProperties(prefix = "spring.datasource") 29 | public DataSource druidDatasource() { 30 | return new DruidDataSource(); 31 | } 32 | 33 | @Bean 34 | public DataSourceProxy dataSourceProxy(DataSource dataSource) { 35 | return new DataSourceProxy(dataSource); 36 | } 37 | 38 | @Bean 39 | public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception { 40 | SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 41 | sqlSessionFactoryBean.setDataSource(dataSourceProxy); 42 | sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); 43 | sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory()); 44 | 45 | return sqlSessionFactoryBean.getObject(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/java/com/wenren/springcloud/alibaba/config/MyBatisConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.config; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | /** 7 | * @author wangyang 8 | * @version V1.0 9 | * @create 2020-05-25-14:43 10 | */ 11 | @Configuration 12 | @MapperScan({"com.wenren.springcloud.alibaba.dao"}) 13 | public class MyBatisConfig { 14 | } 15 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/java/com/wenren/springcloud/alibaba/controller/AccountController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.controller; 2 | 3 | import com.wenren.springcloud.alibaba.domain.CommonResult; 4 | import com.wenren.springcloud.alibaba.service.AccountService; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import javax.annotation.Resource; 10 | import java.math.BigDecimal; 11 | 12 | /** 13 | * @author wangyang 14 | * @version V1.0 15 | * @create 2020-05-25-16:01 16 | */ 17 | @RestController 18 | public class AccountController { 19 | @Resource 20 | private AccountService accountService; 21 | 22 | /** 23 | * 扣减账户余额 24 | * 25 | * @param userId 26 | * @param money 27 | * @return 28 | */ 29 | @PostMapping(value = "/account/decrease") 30 | CommonResult decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money) { 31 | accountService.decrease(userId, money); 32 | 33 | return new CommonResult(200, "扣减库存成功"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/java/com/wenren/springcloud/alibaba/dao/AccountDao.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.dao; 2 | 3 | import org.apache.ibatis.annotations.Mapper; 4 | import org.apache.ibatis.annotations.Param; 5 | 6 | import java.math.BigDecimal; 7 | 8 | /** 9 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-05-25-13:53 12 | */ 13 | @Mapper 14 | public interface AccountDao { 15 | /** 16 | * 扣减账户余额 17 | * 18 | * @param userId 19 | * @param money 20 | */ 21 | void decrease(@Param("userId") Long userId, @Param("money") BigDecimal money); 22 | } 23 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/java/com/wenren/springcloud/alibaba/domain/Account.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.math.BigDecimal; 8 | 9 | /** 10 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-05-25-13:46 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | public class Account { 18 | /** 19 | * id 20 | */ 21 | private Long id; 22 | /** 23 | * 用户id 24 | */ 25 | private Long userId; 26 | /** 27 | * 总额度 28 | */ 29 | private BigDecimal total; 30 | /** 31 | * 已用余额 32 | */ 33 | private BigDecimal used; 34 | /** 35 | * 剩余可用额度 36 | */ 37 | private BigDecimal residue; 38 | } 39 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/java/com/wenren/springcloud/alibaba/domain/CommonResult.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-25-13:47 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | public class CommonResult { 16 | private Integer code; 17 | private String message; 18 | private T data; 19 | 20 | public CommonResult(Integer code, String message) { 21 | this(code, message, null); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/java/com/wenren/springcloud/alibaba/service/AccountService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.service; 2 | 3 | import java.math.BigDecimal; 4 | 5 | /** 6 | * @author wangyang 7 | * @version V1.0 8 | * @create 2020-05-25-15:57 9 | */ 10 | public interface AccountService { 11 | /** 12 | * 扣减账户余额 13 | * 14 | * @param userId 用户id 15 | * @param money 金额 16 | */ 17 | void decrease(Long userId, BigDecimal money); 18 | } 19 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/java/com/wenren/springcloud/alibaba/service/impl/AccountServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.service.impl; 2 | 3 | import com.wenren.springcloud.alibaba.dao.AccountDao; 4 | import com.wenren.springcloud.alibaba.service.AccountService; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.stereotype.Service; 7 | 8 | import javax.annotation.Resource; 9 | import java.math.BigDecimal; 10 | import java.util.concurrent.TimeUnit; 11 | 12 | /** 13 | * @author wangyang 14 | * @version V1.0 15 | * @create 2020-05-25-15:59 16 | * 业务实现类 17 | */ 18 | @Service 19 | @Slf4j 20 | public class AccountServiceImpl implements AccountService { 21 | @Resource 22 | private AccountDao accountDao; 23 | 24 | /** 25 | * 扣减账户余额 26 | * 27 | * @param userId 28 | * @param money 29 | */ 30 | @Override 31 | public void decrease(Long userId, BigDecimal money) { 32 | log.info("------>account-service中扣减账户余额开始"); 33 | //模拟超时异常,全局事务回滚 34 | //暂停几秒钟线程 35 | /*try { 36 | TimeUnit.SECONDS.sleep(20); 37 | } catch (InterruptedException e) { 38 | e.printStackTrace(); 39 | }*/ 40 | accountDao.decrease(userId, money); 41 | log.info("------>account-service中扣减账户余额结束"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 2003 3 | 4 | spring: 5 | application: 6 | name: seata-account-service 7 | cloud: 8 | alibaba: 9 | seata: 10 | tx-service-group: fsp_tx_group #自定义事务组名称需要与seata-server中的对应 11 | nacos: 12 | discovery: 13 | server-addr: 192.168.0.135:1111 14 | datasource: 15 | driver-class-name: com.mysql.jdbc.Driver 16 | url: jdbc:mysql://192.168.0.135:3306/seata_account 17 | username: root 18 | password: Perfect1 19 | 20 | logging: 21 | level: 22 | io: 23 | seata: info 24 | 25 | mybatis: 26 | mapperLocations: classpath:mapper/*.xml 27 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/resources/mapper/AccountMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | update 14 | t_account 15 | set 16 | used = used + #{money}, residue = residue - #{money} 17 | where user_id = #{userId}; 18 | 19 | 20 | -------------------------------------------------------------------------------- /seata-account-service2003/src/main/resources/registry.conf: -------------------------------------------------------------------------------- 1 | registry { 2 | # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa 3 | type = "nacos" 4 | 5 | nacos { 6 | serverAddr = "192.168.0.135:1111" 7 | namespace = "" 8 | cluster = "default" 9 | } 10 | eureka { 11 | serviceUrl = "http://localhost:8761/eureka" 12 | application = "default" 13 | weight = "1" 14 | } 15 | redis { 16 | serverAddr = "localhost:6379" 17 | db = "0" 18 | } 19 | zk { 20 | cluster = "default" 21 | serverAddr = "127.0.0.1:2181" 22 | session.timeout = 6000 23 | connect.timeout = 2000 24 | } 25 | consul { 26 | cluster = "default" 27 | serverAddr = "127.0.0.1:8500" 28 | } 29 | etcd3 { 30 | cluster = "default" 31 | serverAddr = "http://localhost:2379" 32 | } 33 | sofa { 34 | serverAddr = "127.0.0.1:9603" 35 | application = "default" 36 | region = "DEFAULT_ZONE" 37 | datacenter = "DefaultDataCenter" 38 | cluster = "default" 39 | group = "SEATA_GROUP" 40 | addressWaitTime = "3000" 41 | } 42 | file { 43 | name = "file.conf" 44 | } 45 | } 46 | 47 | config { 48 | # file、nacos 、apollo、zk、consul、etcd3 49 | type = "file" 50 | 51 | nacos { 52 | serverAddr = "localhost" 53 | namespace = "" 54 | } 55 | consul { 56 | serverAddr = "127.0.0.1:8500" 57 | } 58 | apollo { 59 | app.id = "seata-server" 60 | apollo.meta = "http://192.168.1.204:8801" 61 | } 62 | zk { 63 | serverAddr = "127.0.0.1:2181" 64 | session.timeout = 6000 65 | connect.timeout = 2000 66 | } 67 | etcd3 { 68 | serverAddr = "http://localhost:2379" 69 | } 70 | file { 71 | name = "file.conf" 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /seata-order-service2001/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | cloud2020 7 | com.wenren.springcloud 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | seata-order-service2001 13 | 14 | 15 | 16 | 17 | com.alibaba.cloud 18 | spring-cloud-starter-alibaba-nacos-discovery 19 | 20 | 21 | 22 | com.alibaba.cloud 23 | spring-cloud-starter-alibaba-seata 24 | 25 | 26 | seata-all 27 | io.seata 28 | 29 | 30 | 31 | 32 | seata-all 33 | io.seata 34 | 1.0.0 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.mybatis.spring.boot 53 | mybatis-spring-boot-starter 54 | 55 | 56 | com.alibaba 57 | druid-spring-boot-starter 58 | 1.1.10 59 | 60 | 61 | mysql 62 | mysql-connector-java 63 | 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-devtools 68 | runtime 69 | true 70 | 71 | 72 | org.projectlombok 73 | lombok 74 | true 75 | 76 | 77 | org.springframework.boot 78 | spring-boot-starter-test 79 | test 80 | 81 | 82 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/SeataOrderMainApp2001.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.cloud.openfeign.EnableFeignClients; 8 | 9 | /** 10 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-05-25-14:18 13 | */ 14 | @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //取消数据源的自动创建 15 | @EnableDiscoveryClient 16 | @EnableFeignClients 17 | public class SeataOrderMainApp2001 { 18 | public static void main(String[] args) { 19 | SpringApplication.run(SeataOrderMainApp2001.class, args); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/config/DataSourceProxyConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.config; 2 | 3 | import com.alibaba.druid.pool.DruidDataSource; 4 | import io.seata.rm.datasource.DataSourceProxy; 5 | import org.apache.ibatis.session.SqlSessionFactory; 6 | import org.mybatis.spring.SqlSessionFactoryBean; 7 | import org.mybatis.spring.transaction.SpringManagedTransactionFactory; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.boot.context.properties.ConfigurationProperties; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 13 | 14 | import javax.sql.DataSource; 15 | 16 | /** 17 | * @author wangyang 18 | * @version V1.0 19 | * @create 2020-05-25-14:43 20 | * 使用Seata对数据源进行代理 21 | */ 22 | @Configuration 23 | public class DataSourceProxyConfig { 24 | @Value("${mybatis.mapperLocations}") 25 | private String mapperLocations; 26 | 27 | @Bean 28 | @ConfigurationProperties(prefix = "spring.datasource") 29 | public DataSource druidDatasource() { 30 | return new DruidDataSource(); 31 | } 32 | 33 | @Bean 34 | public DataSourceProxy dataSourceProxy(DataSource dataSource) { 35 | return new DataSourceProxy(dataSource); 36 | } 37 | 38 | @Bean 39 | public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception { 40 | SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 41 | sqlSessionFactoryBean.setDataSource(dataSourceProxy); 42 | sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); 43 | sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory()); 44 | 45 | return sqlSessionFactoryBean.getObject(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/config/MyBatisConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.config; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | /** 7 | * @author wangyang 8 | * @version V1.0 9 | * @create 2020-05-25-14:43 10 | */ 11 | @Configuration 12 | @MapperScan({"com.wenren.springcloud.alibaba.dao"}) 13 | public class MyBatisConfig { 14 | } 15 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.controller; 2 | 3 | import com.wenren.springcloud.alibaba.domain.CommonResult; 4 | import com.wenren.springcloud.alibaba.domain.Order; 5 | import com.wenren.springcloud.alibaba.service.OrderService; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import javax.annotation.Resource; 10 | 11 | /** 12 | * @author wangyang 13 | * @version V1.0 14 | * @create 2020-05-25-14:39 15 | */ 16 | @RestController 17 | public class OrderController { 18 | @Resource 19 | private OrderService orderService; 20 | 21 | @GetMapping("/order/create") 22 | public CommonResult create(Order order) { 23 | orderService.create(order); 24 | 25 | return new CommonResult(200, "订单创建成功"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/dao/OrderDao.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.dao; 2 | 3 | import com.wenren.springcloud.alibaba.domain.Order; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-25-13:53 11 | */ 12 | @Mapper 13 | public interface OrderDao { 14 | //1、新建订单 15 | void create(Order order); 16 | 17 | //2、修改订单状态,从0改为1 18 | void update(@Param("userId") Long userId, @Param("status") Integer status); 19 | } 20 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/domain/CommonResult.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-25-13:47 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | public class CommonResult { 16 | private Integer code; 17 | private String message; 18 | private T data; 19 | 20 | public CommonResult(Integer code, String message) { 21 | this(code, message, null); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/domain/Order.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.math.BigDecimal; 8 | 9 | /** 10 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-05-25-13:46 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | public class Order { 18 | /** 19 | * id 20 | */ 21 | private Long id; 22 | /** 23 | * 用户id 24 | */ 25 | private Long userId; 26 | /** 27 | * 产品id 28 | */ 29 | private Long productId; 30 | /** 31 | * 数量 32 | */ 33 | private Integer count; 34 | /** 35 | * 金额 36 | */ 37 | private BigDecimal money; 38 | /** 39 | * 订单状态:0:创建中;1:已完结 40 | */ 41 | private Integer status; 42 | } 43 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/service/AccountService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.service; 2 | 3 | import com.wenren.springcloud.alibaba.domain.CommonResult; 4 | import org.springframework.cloud.openfeign.FeignClient; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | 8 | import java.math.BigDecimal; 9 | 10 | /** 11 | * @author wangyang 12 | * @version V1.0 13 | * @create 2020-05-25-14:15 14 | */ 15 | @FeignClient(value = "seata-account-service") 16 | public interface AccountService { 17 | @PostMapping(value = "/account/decrease") 18 | CommonResult decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money); 19 | } 20 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/service/OrderService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.service; 2 | 3 | import com.wenren.springcloud.alibaba.domain.Order; 4 | 5 | /** 6 | * @author wangyang 7 | * @version V1.0 8 | * @create 2020-05-25-14:13 9 | */ 10 | public interface OrderService { 11 | void create(Order order); 12 | } 13 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/service/StorageService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.service; 2 | 3 | import com.wenren.springcloud.alibaba.domain.CommonResult; 4 | import org.springframework.cloud.openfeign.FeignClient; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | 8 | /** 9 | * @author wangyang 10 | * @version V1.0 11 | * @create 2020-05-25-14:15 12 | */ 13 | @FeignClient(value = "seata-storage-service") 14 | public interface StorageService { 15 | @PostMapping(value = "/storage/decrease") 16 | CommonResult decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count); 17 | } 18 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/java/com/wenren/springcloud/alibaba/service/impl/OrderServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.service.impl; 2 | 3 | import com.wenren.springcloud.alibaba.dao.OrderDao; 4 | import com.wenren.springcloud.alibaba.domain.Order; 5 | import com.wenren.springcloud.alibaba.service.AccountService; 6 | import com.wenren.springcloud.alibaba.service.OrderService; 7 | import com.wenren.springcloud.alibaba.service.StorageService; 8 | import io.seata.spring.annotation.GlobalTransactional; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.springframework.stereotype.Service; 11 | 12 | import javax.annotation.Resource; 13 | 14 | /** 15 | * @author wangyang 16 | * @version V1.0 17 | * @create 2020-05-25-14:14 18 | */ 19 | @Service 20 | @Slf4j 21 | public class OrderServiceImpl implements OrderService { 22 | @Resource 23 | private OrderDao orderDao; 24 | 25 | @Resource 26 | private StorageService storageService; 27 | 28 | @Resource 29 | private AccountService accountService; 30 | 31 | /** 32 | * 创建订单->调用库存服务扣减库存->调用账户服务扣减账户余额->修改订单状态 33 | * 简单说:下订单->扣库存->减余额->改状态 34 | * 35 | * @param order 36 | */ 37 | @Override 38 | @GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class) 39 | public void create(Order order) { 40 | //1、新建订单 41 | log.info("------>开始新建订单"); 42 | orderDao.create(order); 43 | //2、扣减库存 44 | log.info("------>订单微服务开始调用库存,做扣减start"); 45 | storageService.decrease(order.getProductId(), order.getCount()); 46 | log.info("------>订单微服务开始调用库存,做扣减end"); 47 | //3、扣减账户 48 | log.info("------>订单微服务开始调用账户,做扣减start"); 49 | accountService.decrease(order.getUserId(), order.getMoney()); 50 | log.info("------>订单微服务开始调用账户,做扣减end"); 51 | //4、修改订单状态,从0到1,1代表已经完成 52 | log.info("------>修改订单状态start"); 53 | orderDao.update(order.getUserId(), 0); 54 | log.info("------>修改订单状态end"); 55 | 56 | log.info("------>下订单结束了,O(∩_∩)O哈哈~"); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 2001 3 | 4 | spring: 5 | application: 6 | name: seata-order-service 7 | cloud: 8 | alibaba: 9 | seata: 10 | tx-service-group: fsp_tx_group #自定义事务组名称需要与seata-server中的对应 11 | nacos: 12 | discovery: 13 | server-addr: 192.168.0.135:1111 14 | datasource: 15 | driver-class-name: com.mysql.jdbc.Driver 16 | url: jdbc:mysql://192.168.0.135:3306/seata_order 17 | username: root 18 | password: Perfect1 19 | 20 | feign: 21 | hystrix: 22 | enabled: false 23 | 24 | logging: 25 | level: 26 | io: 27 | seata: info 28 | 29 | mybatis: 30 | mapperLocations: classpath:mapper/*.xml 31 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/resources/mapper/OrderMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | insert into t_order(id, user_id, product_id, count, money, status) 15 | values (null,#{userId}, #{productId}, #{count}, #{money}, 0); 16 | 17 | 18 | 19 | update t_order set status = 1 20 | where user_id = #{userId} and status = #{status}; 21 | 22 | 23 | -------------------------------------------------------------------------------- /seata-order-service2001/src/main/resources/registry.conf: -------------------------------------------------------------------------------- 1 | registry { 2 | # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa 3 | type = "nacos" 4 | 5 | nacos { 6 | serverAddr = "192.168.0.135:1111" 7 | namespace = "" 8 | cluster = "default" 9 | } 10 | eureka { 11 | serviceUrl = "http://localhost:8761/eureka" 12 | application = "default" 13 | weight = "1" 14 | } 15 | redis { 16 | serverAddr = "localhost:6379" 17 | db = "0" 18 | } 19 | zk { 20 | cluster = "default" 21 | serverAddr = "127.0.0.1:2181" 22 | session.timeout = 6000 23 | connect.timeout = 2000 24 | } 25 | consul { 26 | cluster = "default" 27 | serverAddr = "127.0.0.1:8500" 28 | } 29 | etcd3 { 30 | cluster = "default" 31 | serverAddr = "http://localhost:2379" 32 | } 33 | sofa { 34 | serverAddr = "127.0.0.1:9603" 35 | application = "default" 36 | region = "DEFAULT_ZONE" 37 | datacenter = "DefaultDataCenter" 38 | cluster = "default" 39 | group = "SEATA_GROUP" 40 | addressWaitTime = "3000" 41 | } 42 | file { 43 | name = "file.conf" 44 | } 45 | } 46 | 47 | config { 48 | # file、nacos 、apollo、zk、consul、etcd3 49 | type = "file" 50 | 51 | nacos { 52 | serverAddr = "localhost" 53 | namespace = "" 54 | } 55 | consul { 56 | serverAddr = "127.0.0.1:8500" 57 | } 58 | apollo { 59 | app.id = "seata-server" 60 | apollo.meta = "http://192.168.1.204:8801" 61 | } 62 | zk { 63 | serverAddr = "127.0.0.1:2181" 64 | session.timeout = 6000 65 | connect.timeout = 2000 66 | } 67 | etcd3 { 68 | serverAddr = "http://localhost:2379" 69 | } 70 | file { 71 | name = "file.conf" 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/java/com/wenren/springcloud/alibaba/SeataStorageMainApp2002.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.cloud.openfeign.EnableFeignClients; 8 | 9 | /** 10 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-05-26-10:10 13 | */ 14 | @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //取消数据源的自动创建 15 | @EnableDiscoveryClient 16 | @EnableFeignClients 17 | public class SeataStorageMainApp2002 { 18 | public static void main(String[] args) { 19 | SpringApplication.run(SeataStorageMainApp2002.class, args); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/java/com/wenren/springcloud/alibaba/config/DataSourceProxyConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.config; 2 | 3 | import com.alibaba.druid.pool.DruidDataSource; 4 | import io.seata.rm.datasource.DataSourceProxy; 5 | import org.apache.ibatis.session.SqlSessionFactory; 6 | import org.mybatis.spring.SqlSessionFactoryBean; 7 | import org.mybatis.spring.transaction.SpringManagedTransactionFactory; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.boot.context.properties.ConfigurationProperties; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 13 | 14 | import javax.sql.DataSource; 15 | 16 | /** 17 | * @author wangyang 18 | * @version V1.0 19 | * @create 2020-05-25-14:43 20 | * 使用Seata对数据源进行代理 21 | */ 22 | @Configuration 23 | public class DataSourceProxyConfig { 24 | @Value("${mybatis.mapperLocations}") 25 | private String mapperLocations; 26 | 27 | @Bean 28 | @ConfigurationProperties(prefix = "spring.datasource") 29 | public DataSource druidDatasource() { 30 | return new DruidDataSource(); 31 | } 32 | 33 | @Bean 34 | public DataSourceProxy dataSourceProxy(DataSource dataSource) { 35 | return new DataSourceProxy(dataSource); 36 | } 37 | 38 | @Bean 39 | public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception { 40 | SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 41 | sqlSessionFactoryBean.setDataSource(dataSourceProxy); 42 | sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); 43 | sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory()); 44 | 45 | return sqlSessionFactoryBean.getObject(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/java/com/wenren/springcloud/alibaba/config/MyBatisConfig.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.config; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | /** 7 | * @author wangyang 8 | * @version V1.0 9 | * @create 2020-05-25-14:43 10 | */ 11 | @Configuration 12 | @MapperScan({"com.wenren.springcloud.alibaba.dao"}) 13 | public class MyBatisConfig { 14 | } 15 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/java/com/wenren/springcloud/alibaba/controller/StorageController.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.controller; 2 | 3 | import com.wenren.springcloud.alibaba.domain.CommonResult; 4 | import com.wenren.springcloud.alibaba.service.StorageService; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import javax.annotation.Resource; 10 | 11 | /** 12 | * @author wangyang 13 | * @version V1.0 14 | * @create 2020-05-26-10:55 15 | */ 16 | @RestController 17 | public class StorageController { 18 | @Resource 19 | private StorageService storageService; 20 | 21 | /** 22 | * 扣减库存 23 | * 24 | * @param productId 25 | * @param count 26 | * @return 27 | */ 28 | @PostMapping(value = "/storage/decrease") 29 | CommonResult decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count) { 30 | storageService.decrease(productId, count); 31 | 32 | return new CommonResult(200, "扣减账户成功"); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/java/com/wenren/springcloud/alibaba/dao/StorageDao.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.dao; 2 | 3 | import org.apache.ibatis.annotations.Mapper; 4 | import org.apache.ibatis.annotations.Param; 5 | 6 | /** 7 | * @author wangyang 8 | * @version V1.0 9 | * @create 2020-05-26-10:11 10 | */ 11 | @Mapper 12 | public interface StorageDao { 13 | /** 14 | * 扣减库存 15 | * 16 | * @param productId 17 | * @param count 18 | */ 19 | void decrease(@Param("productId") Long productId, @Param("count") Integer count); 20 | } 21 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/java/com/wenren/springcloud/alibaba/domain/CommonResult.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | * @author wangyang 9 | * @version V1.0 10 | * @create 2020-05-25-13:47 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | public class CommonResult { 16 | private Integer code; 17 | private String message; 18 | private T data; 19 | 20 | public CommonResult(Integer code, String message) { 21 | this(code, message, null); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/java/com/wenren/springcloud/alibaba/domain/Storage.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.math.BigDecimal; 8 | 9 | /** 10 | * @author wangyang 11 | * @version V1.0 12 | * @create 2020-05-25-13:46 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | public class Storage { 18 | /** 19 | * id 20 | */ 21 | private Long id; 22 | /** 23 | * 产品id 24 | */ 25 | private Long productId; 26 | /** 27 | * 总库存 28 | */ 29 | private Integer total; 30 | /** 31 | * 已用库存 32 | */ 33 | private Integer used; 34 | /** 35 | * 剩余库存 36 | */ 37 | private Integer residue; 38 | } 39 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/java/com/wenren/springcloud/alibaba/service/StorageService.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.service; 2 | 3 | /** 4 | * @author wangyang 5 | * @version V1.0 6 | * @create 2020-05-26-10:18 7 | */ 8 | public interface StorageService { 9 | /** 10 | * 扣减库存 11 | * 12 | * @param productId 13 | * @param count 14 | */ 15 | void decrease(Long productId, Integer count); 16 | } 17 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/java/com/wenren/springcloud/alibaba/service/impl/StorageServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.wenren.springcloud.alibaba.service.impl; 2 | 3 | import com.wenren.springcloud.alibaba.dao.StorageDao; 4 | import com.wenren.springcloud.alibaba.service.StorageService; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.stereotype.Service; 7 | 8 | import javax.annotation.Resource; 9 | 10 | /** 11 | * @author wangyang 12 | * @version V1.0 13 | * @create 2020-05-26-10:20 14 | */ 15 | @Service 16 | @Slf4j 17 | public class StorageServiceImpl implements StorageService { 18 | @Resource 19 | private StorageDao storageDao; 20 | 21 | /** 22 | * 扣减库存 23 | * 24 | * @param productId 25 | * @param count 26 | */ 27 | @Override 28 | public void decrease(Long productId, Integer count) { 29 | log.info("------>storage-service中扣减库存开始"); 30 | storageDao.decrease(productId,count); 31 | log.info("------>storage-service中扣减库存结束"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 2002 3 | 4 | spring: 5 | application: 6 | name: seata-storage-service 7 | cloud: 8 | alibaba: 9 | seata: 10 | tx-service-group: fsp_tx_group #自定义事务组名称需要与seata-server中的对应 11 | nacos: 12 | discovery: 13 | server-addr: 192.168.0.135:1111 14 | datasource: 15 | driver-class-name: com.mysql.jdbc.Driver 16 | url: jdbc:mysql://192.168.0.135:3306/seata_storage 17 | username: root 18 | password: Perfect1 19 | 20 | logging: 21 | level: 22 | io: 23 | seata: info 24 | 25 | mybatis: 26 | mapperLocations: classpath:mapper/*.xml 27 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/resources/mapper/StorageMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | update 14 | t_storage 15 | set 16 | used = used + #{count}, residue = residue - #{count} 17 | where product_id = #{productId}; 18 | 19 | 20 | -------------------------------------------------------------------------------- /seata-storage-service2002/src/main/resources/registry.conf: -------------------------------------------------------------------------------- 1 | registry { 2 | # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa 3 | type = "nacos" 4 | 5 | nacos { 6 | serverAddr = "192.168.0.135:1111" 7 | namespace = "" 8 | cluster = "default" 9 | } 10 | eureka { 11 | serviceUrl = "http://localhost:8761/eureka" 12 | application = "default" 13 | weight = "1" 14 | } 15 | redis { 16 | serverAddr = "localhost:6379" 17 | db = "0" 18 | } 19 | zk { 20 | cluster = "default" 21 | serverAddr = "127.0.0.1:2181" 22 | session.timeout = 6000 23 | connect.timeout = 2000 24 | } 25 | consul { 26 | cluster = "default" 27 | serverAddr = "127.0.0.1:8500" 28 | } 29 | etcd3 { 30 | cluster = "default" 31 | serverAddr = "http://localhost:2379" 32 | } 33 | sofa { 34 | serverAddr = "127.0.0.1:9603" 35 | application = "default" 36 | region = "DEFAULT_ZONE" 37 | datacenter = "DefaultDataCenter" 38 | cluster = "default" 39 | group = "SEATA_GROUP" 40 | addressWaitTime = "3000" 41 | } 42 | file { 43 | name = "file.conf" 44 | } 45 | } 46 | 47 | config { 48 | # file、nacos 、apollo、zk、consul、etcd3 49 | type = "file" 50 | 51 | nacos { 52 | serverAddr = "localhost" 53 | namespace = "" 54 | } 55 | consul { 56 | serverAddr = "127.0.0.1:8500" 57 | } 58 | apollo { 59 | app.id = "seata-server" 60 | apollo.meta = "http://192.168.1.204:8801" 61 | } 62 | zk { 63 | serverAddr = "127.0.0.1:2181" 64 | session.timeout = 6000 65 | connect.timeout = 2000 66 | } 67 | etcd3 { 68 | serverAddr = "http://localhost:2379" 69 | } 70 | file { 71 | name = "file.conf" 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /sentinel-dashboard-1.7.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenren0819/Spring-Cloud-2020/daba69dca3fd323b2983dac1834129a8ea1706c4/sentinel-dashboard-1.7.2.jar -------------------------------------------------------------------------------- /zipkin-server-2.12.9-exec.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenren0819/Spring-Cloud-2020/daba69dca3fd323b2983dac1834129a8ea1706c4/zipkin-server-2.12.9-exec.jar --------------------------------------------------------------------------------