├── .gitignore ├── LICENSE ├── README.md ├── chapter02 ├── .gitignore ├── spring-cloud-alibaba-nacos-consumer-reactive │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── NacosReactiveConsumer.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-alibaba-nacos-consumer │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── deep │ │ │ │ └── in │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── NacosConsumer.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── NacosConsumerTests.java ├── spring-cloud-alibaba-nacos-provider │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── deep │ │ │ │ └── in │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── NacosProvider.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── NacosProviderTests.java ├── spring-cloud-eureka-nacos-consumer │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── EurekaNacosConsumer.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-eureka-nacos-provider │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── EurekaNacosProvider.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-netflix-eureka-consumer │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── deep │ │ │ │ └── in │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── EurekaConsumer.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── EurekaConsumerTests.java ├── spring-cloud-netflix-eureka-provider │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── deep │ │ │ │ └── in │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── EurekaProvider.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── EurekaProviderTests.java └── spring-cloud-netflix-eureka-server │ ├── README.md │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── EurekaServer.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── deep │ └── in │ └── spring │ └── cloud │ └── EurekaServerTests.java ├── chapter03 ├── .gitignore ├── spring-cloud-alibaba-dubbo-order │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── Order.java │ │ │ ├── OrderApplication.java │ │ │ ├── OrderService.java │ │ │ └── OrderServiceImpl.java │ │ └── resources │ │ ├── application.yml │ │ └── bootstrap.yml ├── spring-cloud-alibaba-dubbo-user │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── Order.java │ │ │ ├── OrderService.java │ │ │ └── UserApplication.java │ │ └── resources │ │ ├── application.yml │ │ └── bootstrap.yml ├── spring-cloud-alibaba-nacos-consumer-openfeign │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── MyOpenFeignConfiguration.java │ │ │ └── NacosConsumer4OpenFeign.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-alibaba-nacos-consumer-ribbon │ ├── README.md │ ├── pom.xml │ ├── scripts │ │ └── echo.sh │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── MyLoadBalancerConfiguration.java │ │ │ ├── NacosConsumer4Ribbon.java │ │ │ └── ServerStatsController.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-alibaba-nacos-consumer-sclb │ ├── README.md │ ├── pom.xml │ ├── scripts │ │ ├── custom-servicechooser.sh │ │ └── sc-loadbalance-test.sh │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── MyLoadBalancerConfiguration.java │ │ │ ├── NacosConsumer4SCLB.java │ │ │ ├── RandomLoadBalancer.java │ │ │ └── RandomServiceInstanceChooser.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-alibaba-nacos-provider4-lb │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── NacosProvider4LoadBalance.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-nacos-consumer-ribbonenhance │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── GrayInterceptor.java │ │ │ ├── GrayRequestInterceptor.java │ │ │ ├── GrayRule.java │ │ │ ├── NacosRibbonEnhanceConsumer.java │ │ │ ├── RibbonRequestContext.java │ │ │ └── RibbonRequestContextHolder.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-nacos-gray-provider │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── NacosGrayProvider.java │ │ └── resources │ │ └── application.properties └── spring-cloud-nacos-normal-provider │ ├── README.md │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── NacosNormalProvider.java │ └── resources │ └── application.properties ├── chapter04 ├── .gitignore ├── profile-properties │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── DefaultUserService.java │ │ │ ├── MockService.java │ │ │ ├── ProfilePropertiesApplication.java │ │ │ ├── UserService.java │ │ │ └── UserServiceImpl.java │ │ └── resources │ │ ├── application-default.properties │ │ ├── application-dev.properties │ │ ├── application-prod.properties │ │ └── application.properties ├── spring-cloud-alibaba-nacos-configuration │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── BookProperties.java │ │ │ └── NacosConfiguration.java │ │ └── resources │ │ └── bootstrap.properties ├── spring-cloud-config-client-refresh │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── ConfigClientRefreshApplication.java │ │ └── resources │ │ └── bootstrap.properties ├── spring-cloud-config-client-service-registry │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── ConfigClientServiceRegistryApplication.java │ │ └── resources │ │ └── bootstrap.properties ├── spring-cloud-config-client │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── ConfigClientApplication.java │ │ └── resources │ │ └── bootstrap.properties ├── spring-cloud-config-server-file │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── ConfigServerFileApplication.java │ │ └── resources │ │ ├── application.properties │ │ ├── book-prod.properties │ │ ├── book.properties │ │ └── master │ │ ├── book.properties │ │ └── master │ │ └── book.properties ├── spring-cloud-config-server-jdbc │ ├── README.md │ ├── pom.xml │ ├── sql │ │ └── properties.sql │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── ConfigServerJDBCApplication.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-config-server-service-registry │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── ConfigServerServiceRegistryApplication.java │ │ └── resources │ │ ├── application.properties │ │ ├── book-prod.properties │ │ ├── book.properties │ │ └── master │ │ ├── book.properties │ │ └── master │ │ └── book.properties └── spring-cloud-nacos-consumer-ribbonenhance-dynamicupdate │ ├── README.md │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ ├── GrayInterceptor.java │ │ ├── GrayRequestInterceptor.java │ │ ├── GrayRule.java │ │ ├── NacosRibbonEnhanceConsumer.java │ │ ├── RibbonRequestContext.java │ │ ├── RibbonRequestContextHolder.java │ │ └── TrafficRule.java │ └── resources │ ├── application.properties │ └── bootstrap.properties ├── chapter05 ├── .gitignore ├── alibaba-sentinel-spring-cloud-gateway │ ├── README.md │ ├── pom.xml │ ├── scripts │ │ └── gateway.sh │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SentinelSpringCloudGatewayApplication.java │ │ └── resources │ │ ├── application.yaml │ │ └── gateway.json ├── alibaba-sentinel-zuul │ ├── README.md │ ├── pom.xml │ ├── scripts │ │ └── gateway.sh │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SentinelZuulApplication.java │ │ └── resources │ │ ├── application.properties │ │ └── gateway.json ├── alibaba-sentinel │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── SentinelCircuitBreakerDemo.java │ │ │ ├── SentinelFlowControlApplication.java │ │ │ ├── SentinelOpenFeignApplication.java │ │ │ ├── SentinelRestTemplateApplication.java │ │ │ └── SentinelSpringCloudCircuitBreakerApplication.java │ │ └── resources │ │ ├── application-flowcontrol.properties │ │ ├── application-openfeign.properties │ │ ├── application-resttemplate.properties │ │ ├── application.properties │ │ ├── degraderule-openfeign.json │ │ ├── degraderule-resttemplate.json │ │ └── flowcontrol.json ├── delivery-service │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── DeliveryApplication.java │ │ └── resources │ │ ├── application.properties │ │ └── degraderule-openfeign.json ├── my-circuit-breaker │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── CircuitBreaker.java │ │ │ ├── Config.java │ │ │ ├── Counter.java │ │ │ ├── DegradeException.java │ │ │ └── State.java │ │ └── test │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── test │ │ └── CircuitBreakerTest.java ├── netflix-hystrix-dashboard │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── HystrixDashboard.java │ │ └── resources │ │ └── application.properties ├── netflix-hystrix-spring-cloud-gateway │ ├── README.md │ ├── pom.xml │ ├── scripts │ │ ├── provider1.sh │ │ ├── provider2.sh │ │ └── provider3.sh │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── HystrixSpringCloudGatewayApplication.java │ │ └── resources │ │ └── application.yaml ├── netflix-hystrix-zuul │ ├── README.md │ ├── pom.xml │ ├── scripts │ │ ├── provider1.sh │ │ ├── provider2.sh │ │ └── provider3.sh │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── HystrixZuulApplication.java │ │ └── resources │ │ └── application.properties ├── netflix-hystrix │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── HystrixCircuitBreakerDemo.java │ │ │ ├── HystrixCommandApplication.java │ │ │ ├── HystrixFlowControlApplication.java │ │ │ ├── HystrixKeyApplication.java │ │ │ ├── HystrixOpenFeignApplication.java │ │ │ ├── HystrixSpringCloudCircuitBreakerApplication.java │ │ │ └── command │ │ │ ├── CircuitBreakerRestCommand.java │ │ │ ├── HelloWorldCommand.java │ │ │ └── TimeoutRestCommand.java │ │ └── resources │ │ ├── application-flowcontrol.properties │ │ ├── application-openfeign.properties │ │ └── application.properties ├── order-service │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── OrderApplication.java │ │ └── resources │ │ └── application.properties ├── resilience4j-spring-cloud │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── Resilience4JSpringCloudCircuitBreakerApplication.java ├── resilience4j │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── R4jBulkhead.java │ │ │ ├── R4jCircuitBreaker.java │ │ │ ├── R4jCircuitBreakerDetail.java │ │ │ ├── R4jRateLimiter.java │ │ │ ├── Resilience4JOpenFeign2Application.java │ │ │ ├── Resilience4JOpenFeign3Application.java │ │ │ └── Resilience4JOpenFeignApplication.java │ │ └── resources │ │ ├── application-openfeign2r4j.properties │ │ └── application.properties └── sms-service │ ├── README.md │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── SMSApplication.java │ └── resources │ └── application.properties ├── chapter06 ├── .gitignore ├── spring-cloud-stream-consumer-error │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SCSErrorConsumeApplication.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-stream-consumer-retry │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SCSRetryApplication.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-stream-custom-bindingtargetfactory │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SCSCustomBindingTargetFactoryApplication.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-stream-metrics │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SCSMetricsApplication.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-stream-polling-consumer │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SCSPollingConsumerApplication.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-stream-producer-error │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SCSErrorProduceApplication.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-stream-producer-partition │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ ├── MyPartitionKeyExtractor.java │ │ │ ├── MyPartitionSelector.java │ │ │ └── SCSProducerPartitionApplication.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-stream │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SCSApplication.java │ │ └── resources │ │ └── application.properties ├── spring-integration │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── SpringIntegrationApplication.java └── spring-messaging │ ├── README.md │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ ├── MyPollableChannel.java │ │ ├── MySubscribableChannel.java │ │ ├── PollableChannelApp.java │ │ ├── SubscribableChannelApp.java │ │ └── WebSocketApplication.java │ └── resources │ ├── application.properties │ └── static │ └── client.html ├── chapter07 ├── .gitignore ├── bus-common │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ ├── CustomEvent.java │ │ └── User.java ├── spring-cloud-bus-node1 │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SCBNode1.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-bus-node2 │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SCBNode2.java │ │ └── resources │ │ └── application.properties └── spring-cloud-bus-node3 │ ├── README.md │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── SCBNode3.java │ └── resources │ └── application.properties ├── chapter08 ├── .gitignore ├── batch-processing │ ├── spring-cloud-task-simple │ │ ├── README.md │ │ ├── pom.xml │ │ └── src │ │ │ ├── main │ │ │ ├── java │ │ │ │ └── deep │ │ │ │ │ └── in │ │ │ │ │ └── spring │ │ │ │ │ └── cloud │ │ │ │ │ └── SpringCloudTaskSimpleApplication.java │ │ │ └── resources │ │ │ │ └── application.properties │ │ │ └── test │ │ │ └── java │ │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SpringCloudTaskSimpleApplicationTests.java │ ├── task-01 │ │ ├── README.md │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── Task01Application.java │ ├── task-02 │ │ ├── README.md │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── Task02Application.java │ ├── task-03 │ │ ├── README.md │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── Task03Application.java │ ├── task-04 │ │ ├── README.md │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── Task04Application.java │ ├── task-05 │ │ ├── README.md │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── Task05Application.java │ ├── task-06 │ │ ├── README.md │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── Task06Application.java │ └── task-error │ │ ├── README.md │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── TaskErrorApplication.java ├── scdf-common │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── CreditCardRecord.java ├── spring-batch │ ├── README.md │ ├── pom.xml │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── deep │ │ │ │ └── in │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── BatchApplication.java │ │ │ └── resources │ │ │ ├── application.properties │ │ │ ├── schema.sql │ │ │ └── turnover.json │ └── turnover.json ├── spring-cloud-deployer-local │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── SpringCloudDeployerLocalApplication.java ├── spring-cloud-skipper │ ├── hello-skipper-v1 │ │ ├── README.md │ │ ├── helloskipper-1.0.0 │ │ │ ├── package.yml │ │ │ ├── templates │ │ │ │ └── helloskipper.yml │ │ │ └── values.yml │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── HelloSkipperApplication.java │ ├── hello-skipper-v2 │ │ ├── README.md │ │ ├── helloskipper-1.0.1 │ │ │ ├── package.yml │ │ │ ├── templates │ │ │ │ └── helloskipper.yml │ │ │ └── values.yml │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── HelloSkipperApplication.java │ └── hello-skipper-v3 │ │ ├── README.md │ │ ├── helloskipper-1.0.2 │ │ ├── package.yml │ │ ├── templates │ │ │ └── helloskipper.yml │ │ └── values.yml │ │ ├── helloskipper-upgrade.yml │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── HelloSkipperApplication.java ├── spring-cloud-task │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── MyTaskApplication.java │ │ └── resources │ │ └── application.properties └── stream-processing │ ├── creditcard-fraud-detection-sink │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── CreditCardSinkApplication.java │ └── creditcard-source │ ├── README.md │ ├── pom.xml │ └── src │ └── main │ └── java │ └── deep │ └── in │ └── spring │ └── cloud │ └── CreditCardSourceApplication.java ├── chapter09 ├── .gitignore ├── nacos-provider │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── NacosProvider.java │ │ └── resources │ │ └── application.properties ├── spring-cloud-gateway-serviceregistry │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SpringCloudGatewayServiceRegistryApplication.java │ │ └── resources │ │ └── application.yaml ├── spring-cloud-gateway │ ├── README.md │ ├── pom.xml │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── deep │ │ │ │ └── in │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── SpringCloudGatewayApplication.java │ │ │ └── resources │ │ │ └── application.yaml │ └── url.sh └── zuul-gateway │ ├── README.md │ ├── pom.xml │ ├── src │ └── main │ │ ├── java │ │ └── deep │ │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── ZuulApplication.java │ │ └── resources │ │ └── application.properties │ └── url.sh └── chapter10 ├── .gitignore ├── java-function ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── deep │ └── in │ └── spring │ └── cloud │ ├── ConsumerApplication.java │ ├── FunctionApplication.java │ ├── JavaFunctionApplication.java │ └── SupplierApplication.java ├── spring-cloud-function-aws ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── deep │ └── in │ └── spring │ └── cloud │ ├── SpringCloudFunctionAWSApplication.java │ └── UserHandlers.java ├── spring-cloud-function-gcp ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── SpringCloudFunctionGCPApplication.java │ └── resources │ └── META-INF │ └── MANIFEST.MF ├── spring-cloud-function-stream-multifuncs ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── SpringCloudFunctionStreamMultiFuncsApplication.java │ └── resources │ └── application.properties ├── spring-cloud-function-stream-withbindingannotation ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── SpringCloudFunctionStreamWithBindingAnnotationApplication.java │ └── resources │ └── application.properties ├── spring-cloud-function-stream ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── SpringCloudFunctionStreamApplication.java │ └── resources │ └── application.properties ├── spring-cloud-function-task ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── deep │ │ └── in │ │ └── spring │ │ └── cloud │ │ └── SpringCloudFunctionTaskApplication.java │ └── resources │ └── application.properties ├── spring-cloud-function-web ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── deep │ └── in │ └── spring │ └── cloud │ └── SpringCloudFunctionWebApplication.java ├── spring-cloud-function ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ ├── deep │ │ └── in │ │ │ └── spring │ │ │ └── cloud │ │ │ └── SpringCloudFunctionApplication.java │ └── functions │ │ ├── CustomFunction.java │ │ └── MyConsumer.java │ └── resources │ └── application.properties └── spring-cloud-functional-application ├── README.md ├── pom.xml └── src └── main ├── java └── deep │ └── in │ └── spring │ └── cloud │ └── SpringCloudFunctionalApplication.java └── resources └── application.properties /README.md: -------------------------------------------------------------------------------- 1 | # 《深入理解 Spring Cloud 与实战》 2 | 3 | ## 目录 4 | 5 | 第 1 章 Spring Cloud生态 6 | 7 | 第 2 章 服务注册与服务发现 8 | 9 | 第 3 章 负载均衡与服务调用 10 | 11 | 第 4 章 配置管理 12 | 13 | 第 5 章 熔断器 14 | 15 | 第 6 章 Spring生态消息驱动 16 | 17 | 第 7 章 消息总线 18 | 19 | 第 8 章 Spring Cloud Data Flow 20 | 21 | 第 9 章 网关 22 | 23 | 第 10 章 Spring Cloud 与 Serverless 24 | 25 | ## 覆盖技术栈 26 | 27 | - Spring Boot 28 | - Alibaba Nacos 29 | - Netflix Eureka 30 | - Spring Cloud LoadBalancer 31 | - Netflix Ribbon 32 | - OpenFeign 33 | - Dubbo Spring Cloud 34 | - Spring Cloud Config Client/Server 35 | - Spring Cloud Circuit Breaker 36 | - Alibaba Sentinel 37 | - Netflix Hystrix 38 | - Resilience4J 39 | - Spring Messaing 40 | - Spring Integration 41 | - Spring Cloud Stream 42 | - Spring Cloud Bus 43 | - Spring Cloud Data Flow 44 | - Spring Cloud Task 45 | - Spring Cloud Skipper 46 | - Spring Cloud Deployer 47 | - Spring Batch 48 | - Spring Cloud Gateway 49 | - Spring Cloud Function 50 | 51 | ## 案例讲解 52 | 53 | - Spring Cloud 双注册/双订阅完成服务平滑迁移 54 | - Netflix Ribbon 流量控制完成灰度发布 55 | - Netflix Ribbon 流量控制条件动态生效 56 | - Sentinel 保护应用防止服务雪崩 57 | - Spring Messaging 处理 WebSocket 请求 58 | - Spring Cloud Bus 配置 Spring Cloud Config 完成配置动态刷新 59 | - 流处理案例:Spring Cloud Stream 完成信用卡反欺诈系统 60 | - 批处理案例:Spring Cloud Task 完成统计GitHub仓库的各项指标数据 61 | - Spring Cloud Function & GCP Cloud Functions 体验 Serverless 62 | 63 | 64 | 售卖地址: https://item.jd.com/13043028.html 。 感谢支持 65 | -------------------------------------------------------------------------------- /chapter02/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-alibaba-nacos-consumer-reactive/README.md: -------------------------------------------------------------------------------- 1 | ## 2.4.1 ReactiveDiscoveryClient 相关的知识 2 | 3 | 使用 ReactiveDiscoveryClient 配合 Alibaba Nacos 进行服务发现 -------------------------------------------------------------------------------- /chapter02/spring-cloud-alibaba-nacos-consumer-reactive/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-consumer 2 | server.port=8081 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 4 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-alibaba-nacos-consumer/README.md: -------------------------------------------------------------------------------- 1 | ## 2.2 使用 Alibaba Nacos 体验第一个 Spring Cloud 微服务应用 2 | 3 | 2.2.3 启动 Nacos Discovery Consumer 进行服务发现对应的例子 -------------------------------------------------------------------------------- /chapter02/spring-cloud-alibaba-nacos-consumer/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-consumer 2 | server.port=8081 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 4 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-alibaba-nacos-consumer/src/test/java/deep/in/spring/cloud/NacosConsumerTests.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class NacosConsumerTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-alibaba-nacos-provider/README.md: -------------------------------------------------------------------------------- 1 | ## 2.2 使用 Alibaba Nacos 体验第一个 Spring Cloud 微服务应用 2 | 3 | 2.2.2 启动 Nacos Discovery Provider 进行服务注册对应的例子 -------------------------------------------------------------------------------- /chapter02/spring-cloud-alibaba-nacos-provider/src/main/java/deep/in/spring/cloud/NacosProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import javax.servlet.http.HttpServletRequest; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.web.bind.annotation.GetMapping; 24 | import org.springframework.web.bind.annotation.RestController; 25 | 26 | @SpringBootApplication 27 | public class NacosProvider { 28 | 29 | public static void main(String[] args) { 30 | SpringApplication.run(NacosProvider.class, args); 31 | } 32 | 33 | @RestController 34 | class EchoController { 35 | 36 | @GetMapping("/echo") 37 | public String echo(HttpServletRequest request) { 38 | return "echo: " + request.getParameter("name"); 39 | } 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-alibaba-nacos-provider/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=my-provider 2 | server.port=8080 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 4 | 5 | management.endpoints.web.exposure.include=* -------------------------------------------------------------------------------- /chapter02/spring-cloud-alibaba-nacos-provider/src/test/java/deep/in/spring/cloud/NacosProviderTests.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class NacosProviderTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-eureka-nacos-consumer/README.md: -------------------------------------------------------------------------------- 1 | ## 2.5 双注册双订阅模式 2 | 3 | 支持双订阅(Nacos、Eureka)的应用 -------------------------------------------------------------------------------- /chapter02/spring-cloud-eureka-nacos-consumer/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=my-consumer 2 | server.port=8083 3 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka 4 | spring.cloud.nacos.discovery.server-addr=localhost:8848 5 | 6 | spring.autoconfigure.exclude=org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration,org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration 7 | ServiceInstanceChooser -------------------------------------------------------------------------------- /chapter02/spring-cloud-eureka-nacos-provider/README.md: -------------------------------------------------------------------------------- 1 | ## 2.5 双注册双订阅模式 2 | 3 | 支持双注册(Nacos、Eureka)的应用 -------------------------------------------------------------------------------- /chapter02/spring-cloud-eureka-nacos-provider/src/main/java/deep/in/spring/cloud/EurekaNacosProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import javax.servlet.http.HttpServletRequest; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 24 | import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; 25 | import org.springframework.web.bind.annotation.GetMapping; 26 | import org.springframework.web.bind.annotation.RestController; 27 | 28 | /** 29 | * @author Jim 30 | */ 31 | @SpringBootApplication 32 | @EnableConfigurationProperties(AutoServiceRegistrationProperties.class) 33 | public class EurekaNacosProvider { 34 | 35 | public static void main(String[] args) { 36 | SpringApplication.run(EurekaNacosProvider.class, args); 37 | } 38 | 39 | @RestController 40 | class EchoController { 41 | 42 | @GetMapping("/echo") 43 | public String echo(HttpServletRequest request) { 44 | return "echo: " + request.getParameter("name"); 45 | } 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-eureka-nacos-provider/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=my-provider 2 | server.port=8082 3 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka 4 | spring.cloud.nacos.discovery.server-addr=localhost:8848 5 | 6 | spring.autoconfigure.exclude=org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration,org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration 7 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-consumer/README.md: -------------------------------------------------------------------------------- 1 | ## 2.3 使用 Netflix Eureka 替换 Alibaba Nacos 注册中心 2 | 3 | 2.3.3 启动 Eureka Discovery Consumer 进行服务发现对应的例子 -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-consumer/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=eureka-consumer 2 | server.port=8081 3 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka 4 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-consumer/src/test/java/deep/in/spring/cloud/EurekaConsumerTests.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class EurekaConsumerTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-provider/README.md: -------------------------------------------------------------------------------- 1 | ## 2.3 使用 Netflix Eureka 替换 Alibaba Nacos 注册中心 2 | 3 | 2.3.2 启动 Eureka Discovery Provider 进行服务注册对应的例子 -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-provider/src/main/java/deep/in/spring/cloud/EurekaProvider.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @SpringBootApplication 11 | public class EurekaProvider { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(EurekaProvider.class, args); 15 | } 16 | 17 | @RestController 18 | class EchoController { 19 | 20 | @GetMapping("/echo") 21 | public String echo(HttpServletRequest request) { 22 | return "echo: " + request.getParameter("name"); 23 | } 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-provider/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=my-provider 2 | server.port=8080 3 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka 4 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-provider/src/test/java/deep/in/spring/cloud/EurekaProviderTests.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class EurekaProviderTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-server/README.md: -------------------------------------------------------------------------------- 1 | ## 2.3 使用 Netflix Eureka 替换 Alibaba Nacos 注册中心 2 | 3 | 2.3.1 启动 Eureka Server 对应的 Eureka Server 应用 -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.1.RELEASE 9 | 10 | 11 | deep.in.spring.cloud 12 | spring-cloud-netflix-eureka-server 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-netflix-eureka-server 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Hoxton.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-netflix-eureka-server 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-test 31 | test 32 | 33 | 34 | org.junit.vintage 35 | junit-vintage-engine 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-dependencies 46 | ${spring-cloud.version} 47 | pom 48 | import 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-server/src/main/java/deep/in/spring/cloud/EurekaServer.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 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 | @SpringBootApplication 8 | @EnableEurekaServer 9 | public class EurekaServer { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(EurekaServer.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=eureka-server 2 | 3 | server.port=8761 4 | 5 | eureka.client.register-with-eureka=false 6 | eureka.client.fetch-registry=false -------------------------------------------------------------------------------- /chapter02/spring-cloud-netflix-eureka-server/src/test/java/deep/in/spring/cloud/EurekaServerTests.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class EurekaServerTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /chapter03/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-order/README.md: -------------------------------------------------------------------------------- 1 | ## 3.6 Dubbo Spring Cloud: 服务调用的新选择 2 | 3 | Provider 应用: 使用 SpringMVC 注解暴露 Dubbo 服务和 Rest 服务。 -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-order/src/main/java/deep/in/spring/cloud/OrderApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | @SpringBootApplication 26 | public class OrderApplication { 27 | 28 | public static void main(String[] args) { 29 | SpringApplication.run(OrderApplication.class, args); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-order/src/main/java/deep/in/spring/cloud/OrderService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.List; 20 | 21 | /** 22 | * @author Jim 23 | */ 24 | public interface OrderService { 25 | 26 | List getAllOrders(String userId); 27 | 28 | Order findOrder(String orderId); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-order/src/main/java/deep/in/spring/cloud/OrderServiceImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | import java.util.stream.Collectors; 22 | 23 | import org.apache.dubbo.config.annotation.Service; 24 | import org.springframework.web.bind.annotation.GetMapping; 25 | import org.springframework.web.bind.annotation.RequestParam; 26 | import org.springframework.web.bind.annotation.RestController; 27 | 28 | /** 29 | * @author Jim 30 | */ 31 | @Service(version = "1.0.0") 32 | @RestController 33 | public class OrderServiceImpl implements OrderService { 34 | 35 | private static List orderList = new ArrayList<>(); 36 | 37 | static { 38 | orderList.add(Order.generate("jim")); 39 | orderList.add(Order.generate("jim")); 40 | orderList.add(Order.generate("test")); 41 | } 42 | 43 | @GetMapping("/allOrders") 44 | @Override 45 | public List getAllOrders(@RequestParam("userId") final String userId) { 46 | return orderList.stream().filter( 47 | order -> order.getUserId().equals(userId) 48 | ).collect(Collectors.toList()); 49 | } 50 | 51 | @GetMapping("/findOrder") 52 | @Override 53 | public Order findOrder(@RequestParam("orderId") String orderId) { 54 | return orderList.stream().filter( 55 | order -> order.getId().equals(orderId) 56 | ).findFirst().orElseGet(Order::error); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-order/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | dubbo: 2 | scan: 3 | base-packages: deep.in.spring.cloud 4 | protocols: 5 | dubbo: 6 | port: -1 7 | 8 | server: 9 | port: 8080 10 | 11 | logging: 12 | level: 13 | web: debug 14 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-order/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: sc-dubbo-provider 4 | main: 5 | allow-bean-definition-overriding: true 6 | cloud: 7 | nacos: 8 | discovery: 9 | enabled: true 10 | register-enabled: true 11 | server-addr: localhost:8848 12 | 13 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-user/README.md: -------------------------------------------------------------------------------- 1 | ## 3.6 Dubbo Spring Cloud: 服务调用的新选择 2 | 3 | Consumer 应用: 使用以下 3 种方式进行服务调用 4 | 5 | 1. 使用 Dubbo @Reference 调用 Dubbo 服务 6 | 2. 使用 OpenFeign 调用 Spring Cloud 服务 7 | 3. 使用 OpenFeign 配合 @DubboTransported 注解完成 Dubbo 调用 -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-user/src/main/java/deep/in/spring/cloud/Order.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.io.Serializable; 20 | import java.sql.Timestamp; 21 | import java.util.UUID; 22 | 23 | /** 24 | * @author Jim 25 | */ 26 | public class Order implements Serializable { 27 | 28 | private String id; 29 | 30 | private Timestamp createdTime; 31 | 32 | private String userId; 33 | 34 | public static Order generate(String userId) { 35 | Order order = new Order(); 36 | order.setId(UUID.randomUUID().toString()); 37 | order.setCreatedTime(new Timestamp(System.currentTimeMillis())); 38 | order.setUserId(userId); 39 | return order; 40 | } 41 | 42 | public static Order error() { 43 | Order order = new Order(); 44 | order.setId("-1"); 45 | order.setCreatedTime(new Timestamp(System.currentTimeMillis())); 46 | order.setUserId("none"); 47 | return order; 48 | } 49 | 50 | public String getId() { 51 | return id; 52 | } 53 | 54 | public void setId(String id) { 55 | this.id = id; 56 | } 57 | 58 | public Timestamp getCreatedTime() { 59 | return createdTime; 60 | } 61 | 62 | public void setCreatedTime(Timestamp createdTime) { 63 | this.createdTime = createdTime; 64 | } 65 | 66 | public String getUserId() { 67 | return userId; 68 | } 69 | 70 | public void setUserId(String userId) { 71 | this.userId = userId; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-user/src/main/java/deep/in/spring/cloud/OrderService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.List; 20 | 21 | /** 22 | * @author Jim 23 | */ 24 | public interface OrderService { 25 | 26 | List getAllOrders(String userId); 27 | 28 | Order findOrder(String orderId); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-user/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | dubbo: 2 | cloud: 3 | subscribed-services: sc-dubbo-provider 4 | protocols: 5 | dubbo: 6 | port: -1 7 | consumer: 8 | check: false 9 | 10 | server: 11 | port: 0 12 | 13 | logging: 14 | level: 15 | web: debug 16 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-dubbo-user/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: sc-dubbo-consumer 4 | main: 5 | allow-bean-definition-overriding: true 6 | cloud: 7 | nacos: 8 | discovery: 9 | enabled: true 10 | register-enabled: true 11 | server-addr: localhost:8848 12 | 13 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-openfeign/README.md: -------------------------------------------------------------------------------- 1 | ## 3.5 OpenFeign: 声明式 Rest 客户端 2 | 3 | 使用 OpenFeign Rest 客户端进行服务调用的例子 -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-openfeign/src/main/java/deep/in/spring/cloud/MyOpenFeignConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import feign.Contract; 20 | import feign.jaxrs.JAXRSContract; 21 | import org.springframework.context.annotation.Bean; 22 | 23 | /** 24 | * @author Jim 25 | */ 26 | public class MyOpenFeignConfiguration { 27 | 28 | @Bean 29 | public Contract myFeignContract() { 30 | return new JAXRSContract(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-openfeign/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-openfeign-consumer 2 | server.port=8086 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 4 | spring.cloud.loadbalancer.ribbon.enabled=false 5 | 6 | logging.level.web=debug 7 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-ribbon/README.md: -------------------------------------------------------------------------------- 1 | ## 3.3 Netflix Ribbon 负载均衡 2 | 3 | 使用 Netflix Ribbon 验证服务消费者调用提供者是否有负载均衡的效果 -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-ribbon/scripts/echo.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | n=1 3 | while [ $n -le 2500 ] 4 | do 5 | echo `curl -s http://localhost:8085/echo` 6 | let n++ 7 | done 8 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-ribbon/src/main/java/deep/in/spring/cloud/MyLoadBalancerConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import com.netflix.loadbalancer.IRule; 20 | import com.netflix.loadbalancer.RandomRule; 21 | import org.springframework.context.annotation.Bean; 22 | 23 | /** 24 | * @author Jim 25 | */ 26 | public class MyLoadBalancerConfiguration { 27 | 28 | @Bean 29 | public IRule iRule() { 30 | System.out.println("random rule active"); 31 | return new RandomRule(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-ribbon/src/main/java/deep/in/spring/cloud/ServerStatsController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import com.netflix.loadbalancer.AbstractLoadBalancer; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.cloud.netflix.ribbon.SpringClientFactory; 22 | import org.springframework.web.bind.annotation.GetMapping; 23 | import org.springframework.web.bind.annotation.RestController; 24 | 25 | /** 26 | * @author Jim 27 | */ 28 | @RestController 29 | public class ServerStatsController { 30 | 31 | @Autowired 32 | private SpringClientFactory springClientFactory; 33 | 34 | @GetMapping("/stats") 35 | public String stats() { 36 | AbstractLoadBalancer loadBalancer = 37 | springClientFactory.getInstance("nacos-provider-lb", AbstractLoadBalancer.class); 38 | return loadBalancer.getLoadBalancerStats().toString(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-ribbon/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-ribbon-consumer 2 | server.port=8085 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 4 | #niws.loadbalancer.nacos-provider-lb.connectionFailureCountThreshold=5 5 | #niws.loadbalancer.nacos-provider-lb.circuitTripMaxTimeoutSeconds=10 6 | ribbon.ReadTimeout=9000 7 | nacos-provider-lb.ribbon.ReadTimeout=8000 8 | #nacos-provider-lb.ribbon.GZipPayload=false 9 | #nacos-provider-lb.ribbon.OkToRetryOnAllOperations=true 10 | 11 | logging.level.web=debug 12 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-sclb/README.md: -------------------------------------------------------------------------------- 1 | ## 3.2 Spring Cloud LoadBalancer 负载均衡 2 | 3 | 使用 Spring Cloud LoadBalancer 验证服务消费者调用提供者是否有负载均衡的效果 -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-sclb/scripts/custom-servicechooser.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | while true 3 | do 4 | echo `curl -s -XGET http://localhost:8084/customChooser` 5 | done 6 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-sclb/scripts/sc-loadbalance-test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | while true 3 | do 4 | echo `curl -s -XGET http://localhost:8084/echo` 5 | done -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-sclb/src/main/java/deep/in/spring/cloud/MyLoadBalancerConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.cloud.client.ServiceInstance; 20 | import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer; 21 | import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; 22 | import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; 23 | import org.springframework.context.annotation.Bean; 24 | import org.springframework.core.env.Environment; 25 | 26 | /** 27 | * @author Jim 28 | */ 29 | public class MyLoadBalancerConfiguration { 30 | 31 | @Bean 32 | public ReactorLoadBalancer reactorServiceInstanceLoadBalancer( 33 | Environment environment, 34 | LoadBalancerClientFactory loadBalancerClientFactory) { 35 | String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); 36 | return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, 37 | ServiceInstanceListSupplier.class), name); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-sclb/src/main/java/deep/in/spring/cloud/RandomServiceInstanceChooser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.List; 20 | import java.util.Random; 21 | 22 | import org.springframework.cloud.client.ServiceInstance; 23 | import org.springframework.cloud.client.discovery.DiscoveryClient; 24 | import org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser; 25 | 26 | /** 27 | * @author Jim 28 | */ 29 | public class RandomServiceInstanceChooser implements ServiceInstanceChooser { 30 | 31 | private final DiscoveryClient discoveryClient; 32 | 33 | private final Random random; 34 | 35 | public RandomServiceInstanceChooser(DiscoveryClient discoveryClient) { 36 | this.discoveryClient = discoveryClient; 37 | random = new Random(); 38 | } 39 | 40 | @Override 41 | public ServiceInstance choose(String serviceId) { 42 | List serviceInstanceList = 43 | discoveryClient.getInstances(serviceId); 44 | return serviceInstanceList.get(random.nextInt(serviceInstanceList.size())); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-consumer-sclb/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-scl-consumer 2 | server.port=8084 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 4 | 5 | logging.level.web=debug 6 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-provider4-lb/README.md: -------------------------------------------------------------------------------- 1 | ## 3.2-3.3 Spring Cloud LoadBalancer/Netflix Ribbon 负载均衡 2 | 3 | 服务提供者应用。 4 | 5 | 为了验证负载均衡是否生效,需要修改应用的启动接口分别注册。 -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-provider4-lb/src/main/java/deep/in/spring/cloud/NacosProvider4LoadBalance.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import javax.servlet.http.HttpServletRequest; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.web.bind.annotation.GetMapping; 24 | import org.springframework.web.bind.annotation.RestController; 25 | 26 | @SpringBootApplication 27 | public class NacosProvider4LoadBalance { 28 | 29 | public static void main(String[] args) { 30 | SpringApplication.run(NacosProvider4LoadBalance.class, args); 31 | } 32 | 33 | @RestController 34 | class EchoController { 35 | 36 | @GetMapping("/") 37 | public String echo(HttpServletRequest request) { 38 | return request.getLocalAddr() + ":" + request.getLocalPort(); 39 | } 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-alibaba-nacos-provider4-lb/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-provider-lb 2 | #server.port=8080 3 | #server.port=8081 4 | server.port=8082 5 | #server.port=8083 6 | spring.cloud.nacos.discovery.server-addr=localhost:8848 7 | 8 | management.endpoints.web.exposure.include=* 9 | 10 | logging.level.web=debug 11 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-consumer-ribbonenhance/README.md: -------------------------------------------------------------------------------- 1 | ## 3.8 应用流量控制 2 | 3 | 消费端扩展 RestTemplate 和 OpenFeign 进行流量识别,并通过 ThreadLocal 透传结果。 -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-consumer-ribbonenhance/src/main/java/deep/in/spring/cloud/GrayInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.io.IOException; 20 | 21 | import org.springframework.http.HttpRequest; 22 | import org.springframework.http.client.ClientHttpRequestExecution; 23 | import org.springframework.http.client.ClientHttpRequestInterceptor; 24 | import org.springframework.http.client.ClientHttpResponse; 25 | import org.springframework.web.context.request.RequestContextHolder; 26 | import org.springframework.web.context.request.ServletRequestAttributes; 27 | 28 | /** 29 | * @author Jim 30 | */ 31 | public class GrayInterceptor implements ClientHttpRequestInterceptor { 32 | 33 | @Override 34 | public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) 35 | throws IOException { 36 | if (request.getHeaders().containsKey("Gray")) { 37 | String value = request.getHeaders().getFirst("Gray"); 38 | if (value.equals("true")) { 39 | RibbonRequestContextHolder.getCurrentContext().put("Gray", Boolean.TRUE.toString()); 40 | ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); 41 | attributes.setAttribute("Gray", Boolean.TRUE.toString(), 0); 42 | } 43 | } 44 | return execution.execute(request, body); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-consumer-ribbonenhance/src/main/java/deep/in/spring/cloud/GrayRequestInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import feign.RequestInterceptor; 20 | import feign.RequestTemplate; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | public class GrayRequestInterceptor implements RequestInterceptor { 26 | @Override 27 | public void apply(RequestTemplate template) { 28 | if (template.headers().containsKey("Gray")) { 29 | String value = template.headers().get("Gray").iterator().next(); 30 | if (value.equals("true")) { 31 | RibbonRequestContextHolder.getCurrentContext().put("Gray", Boolean.TRUE.toString()); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-consumer-ribbonenhance/src/main/java/deep/in/spring/cloud/RibbonRequestContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | public class RibbonRequestContext { 26 | 27 | private final Map attr = new HashMap<>(); 28 | 29 | public String put(String key, String value) { 30 | return attr.put(key, value); 31 | } 32 | 33 | public String remove(String key) { 34 | return attr.remove(key); 35 | } 36 | 37 | public String get(String key) { 38 | return attr.get(key); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-consumer-ribbonenhance/src/main/java/deep/in/spring/cloud/RibbonRequestContextHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | /** 20 | * @author Jim 21 | */ 22 | public class RibbonRequestContextHolder { 23 | 24 | private static ThreadLocal holder = new ThreadLocal() { 25 | @Override 26 | protected RibbonRequestContext initialValue() { 27 | return new RibbonRequestContext(); 28 | } 29 | }; 30 | 31 | public static RibbonRequestContext getCurrentContext() { 32 | return holder.get(); 33 | } 34 | 35 | public static void setCurrentContext(RibbonRequestContext context) { 36 | holder.set(context); 37 | } 38 | 39 | public static void clearContext() { 40 | holder.remove(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-consumer-ribbonenhance/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-ribbonenhanced-consumer 2 | server.port=8888 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 4 | 5 | feign.hystrix.enabled=true 6 | 7 | logging.level.web=debug 8 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-gray-provider/README.md: -------------------------------------------------------------------------------- 1 | ## 3.8 应用流量控制 2 | 3 | 灰度 Provider。 -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-gray-provider/src/main/java/deep/in/spring/cloud/NacosGrayProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import javax.servlet.http.HttpServletRequest; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.web.bind.annotation.GetMapping; 24 | import org.springframework.web.bind.annotation.RestController; 25 | 26 | /** 27 | * @author Jim 28 | */ 29 | @SpringBootApplication 30 | public class NacosGrayProvider { 31 | 32 | public static void main(String[] args) { 33 | SpringApplication.run(NacosGrayProvider.class, args); 34 | } 35 | 36 | @RestController 37 | class EchoController { 38 | 39 | @GetMapping("/") 40 | public String echo(HttpServletRequest request) { 41 | return request.getLocalAddr() + ":" + request.getLocalPort(); 42 | } 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-gray-provider/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-traffic-service 2 | server.port=9090 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 4 | spring.cloud.nacos.discovery.metadata.gray=true 5 | 6 | management.endpoints.web.exposure.include=* 7 | 8 | logging.level.web=debug 9 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-normal-provider/README.md: -------------------------------------------------------------------------------- 1 | ## 3.8 应用流量控制 2 | 3 | 正常 Provider。 -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-normal-provider/src/main/java/deep/in/spring/cloud/NacosNormalProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import javax.servlet.http.HttpServletRequest; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.web.bind.annotation.GetMapping; 24 | import org.springframework.web.bind.annotation.RestController; 25 | 26 | /** 27 | * @author Jim 28 | */ 29 | @SpringBootApplication 30 | public class NacosNormalProvider { 31 | 32 | public static void main(String[] args) { 33 | SpringApplication.run(NacosNormalProvider.class, args); 34 | } 35 | 36 | @RestController 37 | class EchoController { 38 | 39 | @GetMapping("/") 40 | public String echo(HttpServletRequest request) { 41 | return request.getLocalAddr() + ":" + request.getLocalPort(); 42 | } 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /chapter03/spring-cloud-nacos-normal-provider/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-traffic-service 2 | server.port=8080 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 4 | spring.cloud.nacos.discovery.metadata.gray=false 5 | 6 | management.endpoints.web.exposure.include=* 7 | 8 | logging.level.web=debug 9 | -------------------------------------------------------------------------------- /chapter04/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /chapter04/profile-properties/README.md: -------------------------------------------------------------------------------- 1 | ## 4.2 Spring/Spring Boot 与配置 2 | 3 | 体验基于 Profile 加载不同配置的场景。 -------------------------------------------------------------------------------- /chapter04/profile-properties/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.1.RELEASE 9 | 10 | 11 | deep.in.spring.cloud 12 | profile-properties 13 | 0.0.1-SNAPSHOT 14 | profile-properties 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-test 29 | test 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-maven-plugin 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 43 | 8 44 | 8 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /chapter04/profile-properties/src/main/java/deep/in/spring/cloud/DefaultUserService.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | import org.springframework.context.annotation.Profile; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | * @author Jim 8 | */ 9 | @Profile(value = "default") 10 | @Service 11 | public class DefaultUserService implements UserService { 12 | @Override 13 | public String findAll() { 14 | return "User=default"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /chapter04/profile-properties/src/main/java/deep/in/spring/cloud/MockService.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | import org.springframework.context.annotation.Profile; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | * @author Jim 8 | */ 9 | @Profile(value = "dev") 10 | @Service 11 | public class MockService implements UserService { 12 | @Override 13 | public String findAll() { 14 | return "User=mock"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /chapter04/profile-properties/src/main/java/deep/in/spring/cloud/UserService.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | /** 4 | * @author Jim 5 | */ 6 | public interface UserService { 7 | 8 | String findAll(); 9 | 10 | } 11 | -------------------------------------------------------------------------------- /chapter04/profile-properties/src/main/java/deep/in/spring/cloud/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | import org.springframework.context.annotation.Profile; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | * @author Jim 8 | */ 9 | @Profile(value = "prod") 10 | @Service 11 | public class UserServiceImpl implements UserService { 12 | @Override 13 | public String findAll() { 14 | return "User=prod"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /chapter04/profile-properties/src/main/resources/application-default.properties: -------------------------------------------------------------------------------- 1 | custom.welcome=Default Mock Data -------------------------------------------------------------------------------- /chapter04/profile-properties/src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- 1 | custom.welcome=Hello Mock Data -------------------------------------------------------------------------------- /chapter04/profile-properties/src/main/resources/application-prod.properties: -------------------------------------------------------------------------------- 1 | custom.welcome=Hello Spring Profile -------------------------------------------------------------------------------- /chapter04/profile-properties/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.profiles.active=dev -------------------------------------------------------------------------------- /chapter04/spring-cloud-alibaba-nacos-configuration/README.md: -------------------------------------------------------------------------------- 1 | ## 4.3.1 使用 Alibaba Nacos 体验配置的获取以及动态刷新 2 | 3 | 基于 Spring Cloud Alibaba Nacos Config 的例子。 -------------------------------------------------------------------------------- /chapter04/spring-cloud-alibaba-nacos-configuration/src/main/java/deep/in/spring/cloud/BookProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.context.properties.ConfigurationProperties; 20 | 21 | /** 22 | * @author Jim 23 | */ 24 | @ConfigurationProperties(prefix = "book") 25 | public class BookProperties { 26 | 27 | private String category; 28 | 29 | private String author; 30 | 31 | public String getCategory() { 32 | return category; 33 | } 34 | 35 | public void setCategory(String category) { 36 | this.category = category; 37 | } 38 | 39 | public String getAuthor() { 40 | return author; 41 | } 42 | 43 | public void setAuthor(String author) { 44 | this.author = author; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return "BookProperties{" + 50 | "category='" + category + '\'' + 51 | ", author='" + author + '\'' + 52 | '}'; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-alibaba-nacos-configuration/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-configuration-sample 2 | server.port=8080 3 | spring.cloud.nacos.config.server-addr=localhost:8848 4 | 5 | 6 | book.author=jim -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-client-refresh/README.md: -------------------------------------------------------------------------------- 1 | ## 4.4.2 Spring Cloud Config Client 2 | 3 | 使用 Spring Cloud Config Client 完成动态配置更新的例子。 -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-client-refresh/src/main/java/deep/in/spring/cloud/ConfigClientRefreshApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.beans.factory.annotation.Value; 20 | import org.springframework.boot.SpringApplication; 21 | import org.springframework.boot.autoconfigure.SpringBootApplication; 22 | import org.springframework.cloud.context.config.annotation.RefreshScope; 23 | import org.springframework.web.bind.annotation.GetMapping; 24 | import org.springframework.web.bind.annotation.RestController; 25 | 26 | /** 27 | * @author Jim 28 | */ 29 | @SpringBootApplication 30 | public class ConfigClientRefreshApplication { 31 | 32 | public static void main(String[] args) { 33 | SpringApplication.run(ConfigClientRefreshApplication.class, args); 34 | } 35 | 36 | @RestController 37 | @RefreshScope 38 | class ConfigurationController { 39 | 40 | @Value("${book.author:unknown}") 41 | String bookAuthor; 42 | 43 | @Value("${book.name:unknown}") 44 | String bookName; 45 | 46 | @Value("${book.category:unknown}") 47 | String bookCategory; 48 | 49 | @GetMapping("/config") 50 | public String config() { 51 | StringBuilder sb = new StringBuilder(); 52 | sb.append("bookAuthor=" + bookAuthor) 53 | .append("
bookName=" + bookName) 54 | .append("
bookCategory=" + bookCategory); 55 | return sb.toString(); 56 | } 57 | 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-client-refresh/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=sc-config-client-refresh 2 | spring.cloud.config.name=book 3 | spring.cloud.config.profile=prod 4 | spring.cloud.config.uri=http://localhost:8080/ 5 | 6 | server.port=8081 7 | 8 | management.endpoints.web.exposure.include=* -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-client-service-registry/README.md: -------------------------------------------------------------------------------- 1 | ## 4.4.3 Spring Cloud Config Client 与 Service Registry 整合 2 | 3 | Spring Cloud Config Client 和 Alibaba Nacos 注册中心整合的例子。 -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-client-service-registry/src/main/java/deep/in/spring/cloud/ConfigClientServiceRegistryApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 22 | import org.springframework.cloud.config.server.EnableConfigServer; 23 | 24 | /** 25 | * @author Jim 26 | */ 27 | @SpringBootApplication 28 | @EnableDiscoveryClient 29 | public class ServiceRegistryConfigClientServiceRegistryApplication { 30 | 31 | public static void main(String[] args) { 32 | SpringApplication.run(ConfigClientServiceRegistryApplication.class); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-client-service-registry/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=sc-config-client-service-registry 2 | spring.cloud.config.name=book 3 | spring.cloud.config.profile=prod 4 | spring.cloud.config.discovery.enabled=true 5 | spring.cloud.config.discovery.service-id=sc-config-server-service-registry 6 | 7 | server.port=8082 8 | 9 | management.endpoints.web.exposure.include=* -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-client/README.md: -------------------------------------------------------------------------------- 1 | ## 4.4.2 Spring Cloud Config Client 2 | 3 | 使用 Spring Cloud Config Client 完成配置读取的例子。 -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-client/src/main/java/deep/in/spring/cloud/ConfigClientApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.beans.factory.annotation.Autowired; 20 | import org.springframework.boot.CommandLineRunner; 21 | import org.springframework.boot.WebApplicationType; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.boot.builder.SpringApplicationBuilder; 24 | import org.springframework.context.annotation.Bean; 25 | import org.springframework.core.env.Environment; 26 | 27 | /** 28 | * @author Jim 29 | */ 30 | @SpringBootApplication 31 | public class ConfigClientApplication { 32 | 33 | public static void main(String[] args) { 34 | new SpringApplicationBuilder().web(WebApplicationType.NONE) 35 | .sources(ConfigClientApplication.class).run(args); 36 | } 37 | 38 | @Autowired 39 | Environment env; 40 | 41 | @Bean 42 | public CommandLineRunner runner() { 43 | return (args) -> { 44 | System.out.println( 45 | "book.categoty=" + env.getProperty("book.category", "unknown") 46 | ); 47 | System.out.println( 48 | "book.author=" + env.getProperty("book.author", "unknown") 49 | ); 50 | System.out.println( 51 | "book.name=" + env.getProperty("book.name", "unknown") 52 | ); 53 | }; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-client/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=sc-config-client 2 | spring.cloud.config.name=book 3 | spring.cloud.config.profile=prod 4 | spring.cloud.config.uri=http://localhost:8080/ -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-file/README.md: -------------------------------------------------------------------------------- 1 | ## 4.4.1 Spring Cloud Config Server 2 | 3 | 使用 File System 文件系统作为 Spring Cloud Config Server 里的 EnvironmentRepository 实现类的例子。 -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-file/src/main/java/deep/in/spring/cloud/ConfigServerFileApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | import org.springframework.cloud.config.server.EnableConfigServer; 22 | 23 | /** 24 | * @author Jim 25 | */ 26 | @SpringBootApplication 27 | @EnableConfigServer 28 | public class ConfigServerFileApplication { 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(ConfigServerFileApplication.class); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-file/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=sc-config-server-file 2 | server.port=8080 3 | 4 | spring.profiles.active=native 5 | 6 | spring.cloud.config.server.native.searchLocations=classpath:/ 7 | #spring.cloud.config.server.native.searchLocations=classpath:/{label} 8 | 9 | spring.cloud.config.server.native.version=1.0.0 -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-file/src/main/resources/book-prod.properties: -------------------------------------------------------------------------------- 1 | book.name=deep in spring cloud -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-file/src/main/resources/book.properties: -------------------------------------------------------------------------------- 1 | book.category=spring cloud -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-file/src/main/resources/master/book.properties: -------------------------------------------------------------------------------- 1 | book.author=jim -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-file/src/main/resources/master/master/book.properties: -------------------------------------------------------------------------------- 1 | book.publishYear=2020 -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-jdbc/README.md: -------------------------------------------------------------------------------- 1 | ## 4.4.1 Spring Cloud Config Server 2 | 3 | 使用 JDBC 数据库作为 Spring Cloud Config Server 里的 EnvironmentRepository 实现类的例子。 -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-jdbc/sql/properties.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `properties` ( 2 | `id` int(11) NOT NULL AUTO_INCREMENT, 3 | `key` varchar(50) DEFAULT NULL, 4 | `value` varchar(500) DEFAULT NULL, 5 | `application` varchar(50) DEFAULT NULL, 6 | `profile` varchar(50) DEFAULT NULL, 7 | `label` varchar(50) DEFAULT NULL, 8 | PRIMARY KEY (`id`) 9 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 10 | 11 | 12 | 13 | INSERT INTO `properties` VALUES (null, 'book.name', 'deep in spring cloud', 'book', 'prod', null); 14 | INSERT INTO `properties` VALUES (null, 'book.author', 'jim', 'book', 'prod', null); 15 | INSERT INTO `properties` VALUES (null, 'book.category', 'spring cloud', 'book', 'prod', null); 16 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-jdbc/src/main/java/deep/in/spring/cloud/ConfigServerJDBCApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | import org.springframework.cloud.config.server.EnableConfigServer; 22 | 23 | /** 24 | * @author Jim 25 | */ 26 | @SpringBootApplication 27 | @EnableConfigServer 28 | public class ConfigServerJDBCApplication { 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(ConfigServerJDBCApplication.class, args); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-jdbc/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=sc-config-server-jdbc 2 | server.port=8080 3 | 4 | spring.profiles.active=jdbc 5 | 6 | spring.cloud.config.server.jdbc.sql=SELECT `KEY`, VALUE from PROPERTIES where APPLICATION=? and `PROFILE`=? and LABEL=?; 7 | 8 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/{database}?useUnicode=true&characterEncoding=UTF-8 9 | spring.datasource.username={username} 10 | spring.datasource.password={password} 11 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-service-registry/README.md: -------------------------------------------------------------------------------- 1 | ## 4.4.3 Spring Cloud Config Client 与 Service Registry 整合 2 | 3 | Spring Cloud Config Client 和 Alibaba Nacos 注册中心整合的例子。 -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-service-registry/src/main/java/deep/in/spring/cloud/ConfigServerServiceRegistryApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 22 | import org.springframework.cloud.config.server.EnableConfigServer; 23 | 24 | /** 25 | * @author Jim 26 | */ 27 | @SpringBootApplication 28 | @EnableConfigServer 29 | @EnableDiscoveryClient 30 | public class ConfigServerServiceRegistryApplication { 31 | 32 | public static void main(String[] args) { 33 | SpringApplication.run(ConfigServerServiceRegistryApplication.class); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-service-registry/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=sc-config-server-service-registry 2 | server.port=8080 3 | 4 | 5 | spring.cloud.config.server.native.searchLocations=classpath:/ 6 | #spring.cloud.config.server.native.searchLocations=classpath:/{label} 7 | 8 | spring.cloud.config.server.native.version=1.0.0 9 | 10 | spring.cloud.nacos.server-addr=localhost:8848 -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-service-registry/src/main/resources/book-prod.properties: -------------------------------------------------------------------------------- 1 | book.name=deep in spring cloud -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-service-registry/src/main/resources/book.properties: -------------------------------------------------------------------------------- 1 | book.category=spring cloud -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-service-registry/src/main/resources/master/book.properties: -------------------------------------------------------------------------------- 1 | book.author=jim -------------------------------------------------------------------------------- /chapter04/spring-cloud-config-server-service-registry/src/main/resources/master/master/book.properties: -------------------------------------------------------------------------------- 1 | book.publishYear=2020 -------------------------------------------------------------------------------- /chapter04/spring-cloud-nacos-consumer-ribbonenhance-dynamicupdate/README.md: -------------------------------------------------------------------------------- 1 | ## 4.6 Spring Cloud 应用流量控制策略动态生效 2 | 3 | 需要配合 chapter03 的 spring-cloud-nacos-gray-provider 以及 spring-cloud-nacos-normal-provider 应用。 -------------------------------------------------------------------------------- /chapter04/spring-cloud-nacos-consumer-ribbonenhance-dynamicupdate/src/main/java/deep/in/spring/cloud/GrayRequestInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import feign.RequestInterceptor; 20 | import feign.RequestTemplate; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | public class GrayRequestInterceptor implements RequestInterceptor { 26 | 27 | private TrafficRule rule; 28 | 29 | public GrayRequestInterceptor(TrafficRule rule) { 30 | this.rule = rule; 31 | } 32 | 33 | @Override 34 | public void apply(RequestTemplate template) { 35 | if (rule.getType().equalsIgnoreCase("header")) { 36 | if (template.headers().containsKey(rule.getName())) { 37 | String value = template.headers().get(rule.getName()).iterator().next(); 38 | if (value.equals(rule.getValue())) { 39 | RibbonRequestContextHolder.getCurrentContext().put("Gray", Boolean.TRUE.toString()); 40 | } 41 | } 42 | } else if (rule.getType().equalsIgnoreCase("param")) { 43 | if (template.queries().containsKey(rule.getName())) { 44 | String value = template.queries().get(rule.getName()).iterator().next(); 45 | if (value.equals(rule.getValue())) { 46 | RibbonRequestContextHolder.getCurrentContext().put("Gray", Boolean.TRUE.toString()); 47 | } 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-nacos-consumer-ribbonenhance-dynamicupdate/src/main/java/deep/in/spring/cloud/RibbonRequestContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | public class RibbonRequestContext { 26 | 27 | private final Map attr = new HashMap<>(); 28 | 29 | public String put(String key, String value) { 30 | return attr.put(key, value); 31 | } 32 | 33 | public String remove(String key) { 34 | return attr.remove(key); 35 | } 36 | 37 | public String get(String key) { 38 | return attr.get(key); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-nacos-consumer-ribbonenhance-dynamicupdate/src/main/java/deep/in/spring/cloud/RibbonRequestContextHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | /** 20 | * @author Jim 21 | */ 22 | public class RibbonRequestContextHolder { 23 | 24 | private static ThreadLocal holder = new ThreadLocal() { 25 | @Override 26 | protected RibbonRequestContext initialValue() { 27 | return new RibbonRequestContext(); 28 | } 29 | }; 30 | 31 | public static RibbonRequestContext getCurrentContext() { 32 | return holder.get(); 33 | } 34 | 35 | public static void setCurrentContext(RibbonRequestContext context) { 36 | holder.set(context); 37 | } 38 | 39 | public static void clearContext() { 40 | holder.remove(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-nacos-consumer-ribbonenhance-dynamicupdate/src/main/java/deep/in/spring/cloud/TrafficRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.context.properties.ConfigurationProperties; 20 | 21 | /** 22 | * @author Jim 23 | */ 24 | @ConfigurationProperties(prefix = "traffic.rule") 25 | public class TrafficRule { 26 | 27 | private String type; 28 | 29 | private String name; 30 | 31 | private String value; 32 | 33 | public String getType() { 34 | return type; 35 | } 36 | 37 | public void setType(String type) { 38 | this.type = type; 39 | } 40 | 41 | public String getName() { 42 | return name; 43 | } 44 | 45 | public void setRateLimiterName(String name) { 46 | this.name = name; 47 | } 48 | 49 | public String getValue() { 50 | return value; 51 | } 52 | 53 | public void setValue(String value) { 54 | this.value = value; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "TrafficRule{" + 60 | "type='" + type + '\'' + 61 | ", name='" + name + '\'' + 62 | ", value='" + value + '\'' + 63 | '}'; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /chapter04/spring-cloud-nacos-consumer-ribbonenhance-dynamicupdate/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8888 2 | spring.cloud.nacos.discovery.server-addr=localhost:8848 3 | 4 | feign.hystrix.enabled=true -------------------------------------------------------------------------------- /chapter04/spring-cloud-nacos-consumer-ribbonenhance-dynamicupdate/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-ribbonenhanced-dynamicupdate-consumer 2 | spring.cloud.nacos.config.server-addr=localhost:8848 3 | -------------------------------------------------------------------------------- /chapter05/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel-spring-cloud-gateway/README.md: -------------------------------------------------------------------------------- 1 | ## 5.4.4 Sentinel 限流 2 | 3 | Sentinel 对 Spring Cloud Gateway 网关限流的例子。 -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel-spring-cloud-gateway/scripts/gateway.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | curl -v -H "LANG:zh-cn" http://localhost:8080/httpbin/status/500 3 | #* Trying ::1... 4 | #* TCP_NODELAY set 5 | #* Connected to localhost (::1) port 8080 (#0) 6 | #> GET /httpbin/status/500 HTTP/1.1 7 | #> Host: localhost:8080 8 | #> User-Agent: curl/7.54.0 9 | #> Accept: */* 10 | #> LANG:zh-cn 11 | #> 12 | #< HTTP/1.1 429 Too Many Requests 13 | #< Content-Type: application/json;charset=UTF-8 14 | #< Content-Length: 64 15 | #< 16 | #* Connection #0 to host localhost left intact 17 | #{"code":429,"message":"Blocked by Sentinel: ParamFlowException"} 18 | curl -v -H "LANG1:zh-cn" http://localhost:8080/httpbin/status/500 19 | #* Trying ::1... 20 | #* TCP_NODELAY set 21 | #* Connected to localhost (::1) port 8080 (#0) 22 | #> GET /httpbin/status/500 HTTP/1.1 23 | #> Host: localhost:8080 24 | #> User-Agent: curl/7.54.0 25 | #> Accept: */* 26 | #> LANG1:zh-cn 27 | #> 28 | #< HTTP/1.1 500 Internal Server Error 29 | #< Date: Wed, 26 Feb 2020 16:45:37 GMT 30 | #< Content-Type: text/html; charset=utf-8 31 | #< Content-Length: 0 32 | #< Server: gunicorn/19.9.0 33 | #< Access-Control-Allow-Origin: * 34 | #< Access-Control-Allow-Credentials: true 35 | -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel-spring-cloud-gateway/src/main/java/deep/in/spring/cloud/SentinelSpringCloudGatewayApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | @SpringBootApplication 26 | public class SentinelSpringCloudGatewayApplication { 27 | 28 | public static void main(String[] args) { 29 | SpringApplication.run(SentinelSpringCloudGatewayApplication.class, args); 30 | } 31 | 32 | //@Bean 33 | //public BlockRequestHandler blockRequestHandler() { 34 | // return new BlockRequestHandler() { 35 | // @Override 36 | // public Mono handleRequest(ServerWebExchange exchange, 37 | // Throwable t) { 38 | // return ServerResponse.status(444).contentType(MediaType.APPLICATION_JSON) 39 | // .body(fromValue("SCS Sentinel block")); 40 | // } 41 | // }; 42 | //} 43 | 44 | } 45 | -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel-spring-cloud-gateway/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | application: 5 | name: sentinel-spring-cloud-gateway 6 | cloud: 7 | gateway: 8 | enabled: true 9 | discovery: 10 | locator: 11 | lower-case-service-id: true 12 | routes: 13 | # Add your routes here. 14 | - id: httpbin_route 15 | uri: https://httpbin.org 16 | predicates: 17 | - Path=/httpbin/** 18 | filters: 19 | - RewritePath=/httpbin/(?.*), /$\{segment} 20 | 21 | sentinel: 22 | datasource.ds.file: 23 | file: "classpath: gateway.json" 24 | ruleType: gw-flow 25 | transport: 26 | dashboard: localhost:8080 27 | scg.fallback: 28 | mode: response 29 | response-status: 429 -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel-spring-cloud-gateway/src/main/resources/gateway.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "resource": "httpbin_route", 4 | "count": 0, 5 | "paramItem": { 6 | "parseStrategy": 2, 7 | "fieldName": "LANG" 8 | } 9 | } 10 | ] 11 | -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel-zuul/README.md: -------------------------------------------------------------------------------- 1 | ## 5.4.4 Sentinel 限流 2 | 3 | Sentinel 对 Netflix Zuul 网关限流的例子。 -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel-zuul/scripts/gateway.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | curl -H "LANG:zh-cn" http://localhost:8080/springcloud 3 | # {"code":403, "message":"Provider2 Block", "route":"my-provider2"} 4 | curl http://localhost:8080/dubbo 5 | # {"code":403, "message":"Provider1 Block", "route":"my-provider1"} 6 | curl http://localhost:8080/s-c-alibaba\?name\=2 7 | # {"code":403, "message":"Sentinel Block", "route":"my-provider3"} -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel-zuul/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8080 2 | spring.application.name=sentinel-zuul 3 | spring.cloud.sentinel.transport.dashboard=localhost:9090 4 | spring.cloud.sentinel.datasource.ds.file.file=classpath: gateway.json 5 | spring.cloud.sentinel.datasource.ds.file.ruleType=gw-flow 6 | 7 | 8 | zuul.routes.my-provider1.path=/dubbo/** 9 | zuul.routes.my-provider1.service-id=my-provider1 10 | 11 | zuul.routes.my-provider2.path=/springcloud/** 12 | zuul.routes.my-provider2.service-id=my-provider2 13 | 14 | zuul.routes.my-provider3.path=/s-c-alibaba/** 15 | zuul.routes.my-provider3.service-id=my-provider3 16 | 17 | spring.cloud.sentinel.zuul.order.pre=2000 18 | spring.cloud.sentinel.zuul.order.post=500 19 | spring.cloud.sentinel.zuul.order.error=-100 -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel-zuul/src/main/resources/gateway.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "resource": "my-provider1", 4 | "count": 0 5 | }, 6 | { 7 | "resource": "my-provider2", 8 | "count": 0, 9 | "paramItem": { 10 | "parseStrategy": 2, 11 | "fieldName": "LANG" 12 | } 13 | }, 14 | { 15 | "resource": "my-provider3", 16 | "count": 0, 17 | "paramItem": { 18 | "parseStrategy": 3, 19 | "fieldName": "name" 20 | } 21 | } 22 | ] 23 | -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel/README.md: -------------------------------------------------------------------------------- 1 | ## 5.4 Alibaba Sentinel 2 | 3 | Sentinel 与 RestTemplate,OpenFeign,Spring Cloud Circuit Breaker 的整合。 4 | 5 | 还包括了 Sentinel 自身的熔断、限流使用。 -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel/src/main/resources/application-flowcontrol.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.sentinel.datasource.ds.file.file=classpath: flowcontrol.json 2 | spring.cloud.sentinel.datasource.ds.file.data-type=json 3 | spring.cloud.sentinel.datasource.ds.file.rule-type=flow 4 | 5 | spring.cloud.sentinel.filter.url-patterns=/** -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel/src/main/resources/application-openfeign.properties: -------------------------------------------------------------------------------- 1 | feign.sentinel.enabled=true 2 | 3 | spring.cloud.sentinel.datasource.ds.file.file=classpath: degraderule-openfeign.json 4 | spring.cloud.sentinel.datasource.ds.file.data-type=json 5 | spring.cloud.sentinel.datasource.ds.file.rule-type=degrade 6 | -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel/src/main/resources/application-resttemplate.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.sentinel.datasource.ds.file.file=classpath: degraderule-resttemplate.json 2 | spring.cloud.sentinel.datasource.ds.file.data-type=json 3 | spring.cloud.sentinel.datasource.ds.file.rule-type=degrade 4 | -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=alibaba-sentinel 2 | server.port=8080 3 | 4 | spring.cloud.sentinel.transport.dashboard=localhost:9090 -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel/src/main/resources/degraderule-openfeign.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "resource": "GET:http://inventory-provider/save", 4 | "count": 5, 5 | "grade": 2, 6 | "timeWindow": 10 7 | }, 8 | { 9 | "resource": "GET:https://httpbin.org/delay/3/", 10 | "count": 20, 11 | "grade": 0, 12 | "timeWindow": 30 13 | } 14 | ] -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel/src/main/resources/degraderule-resttemplate.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "resource": "GET:https://httpbin.org/status/500", 4 | "count": 5, 5 | "grade": 2, 6 | "timeWindow": 10 7 | }, 8 | { 9 | "resource": "GET:https://httpbin.org/status/404", 10 | "count": 5, 11 | "grade": 2, 12 | "timeWindow": 10 13 | }, 14 | { 15 | "resource": "GET:https://httpbin.org/delay/3", 16 | "count": 20, 17 | "grade": 0, 18 | "timeWindow": 30 19 | } 20 | ] -------------------------------------------------------------------------------- /chapter05/alibaba-sentinel/src/main/resources/flowcontrol.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "resource": "/hello", 4 | "controlBehavior": 0, 5 | "count": 1, 6 | "grade": 1, 7 | "limitApp": "default", 8 | "strategy": 0 9 | }, 10 | { 11 | "resource": "/springcloud", 12 | "controlBehavior": 0, 13 | "count": 0, 14 | "grade": 1, 15 | "limitApp": "default", 16 | "strategy": 0 17 | }, 18 | { 19 | "resource": "/hello/{name}", 20 | "controlBehavior": 0, 21 | "count": 0, 22 | "grade": 1, 23 | "limitApp": "default", 24 | "strategy": 0 25 | } 26 | ] 27 | -------------------------------------------------------------------------------- /chapter05/delivery-service/README.md: -------------------------------------------------------------------------------- 1 | ## 5.7 使用 Sentinel 保护应用防止服务雪崩 2 | 3 | 配送服务对应的例子。 -------------------------------------------------------------------------------- /chapter05/delivery-service/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=delivery-service 2 | server.port=8081 3 | 4 | spring.cloud.sentinel.transport.dashboard=localhost:9090 5 | spring.cloud.nacos.discovery.server-addr=localhost:8848 6 | 7 | ribbon.ReadTimeout=10000 8 | ribbon.eager-load.enabled=true 9 | ribbon.eager-load.clients=sms-service 10 | 11 | 12 | feign.sentinel.enabled=true 13 | 14 | spring.cloud.sentinel.datasource.ds.file.file=classpath: degraderule-openfeign.json 15 | spring.cloud.sentinel.datasource.ds.file.data-type=json 16 | spring.cloud.sentinel.datasource.ds.file.rule-type=degrade -------------------------------------------------------------------------------- /chapter05/delivery-service/src/main/resources/degraderule-openfeign.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "resource": "GET:http://sms-service/send", 4 | "count": 20, 5 | "grade": 0, 6 | "timeWindow": 30 7 | } 8 | ] -------------------------------------------------------------------------------- /chapter05/my-circuit-breaker/README.md: -------------------------------------------------------------------------------- 1 | ## 5.1 手动实现一个断路器 2 | 3 | 自定义断路器的实现例子。 -------------------------------------------------------------------------------- /chapter05/my-circuit-breaker/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.1.RELEASE 9 | 10 | 11 | deep.in.spring.cloud 12 | my-circuit-breaker 13 | 0.0.1-SNAPSHOT 14 | my-circuit-breaker 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-test 29 | test 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-maven-plugin 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 43 | 8 44 | 8 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /chapter05/my-circuit-breaker/src/main/java/deep/in/spring/cloud/Config.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | /** 20 | * @author Jim 21 | */ 22 | public class Config { 23 | 24 | // Closed 状态进入 Open 状态的错误个数阀值 25 | private int failureCount = 5; 26 | 27 | // failureCount 统计时间窗口 28 | private long failureTimeInterval = 2 * 1000; 29 | 30 | // Open 状态进入 Half-Open 状态的超时时间 31 | private int halfOpenTimeout = 5 * 1000; 32 | 33 | // Half-Open 状态进入 Open 状态的成功个数阀值 34 | private int halfOpenSuccessCount = 2; 35 | 36 | public int getFailureCount() { 37 | return failureCount; 38 | } 39 | 40 | public void setFailureCount(int failureCount) { 41 | this.failureCount = failureCount; 42 | } 43 | 44 | public long getFailureTimeInterval() { 45 | return failureTimeInterval; 46 | } 47 | 48 | public void setFailureTimeInterval(long failureTimeInterval) { 49 | this.failureTimeInterval = failureTimeInterval; 50 | } 51 | 52 | public int getHalfOpenTimeout() { 53 | return halfOpenTimeout; 54 | } 55 | 56 | public void setHalfOpenTimeout(int halfOpenTimeout) { 57 | this.halfOpenTimeout = halfOpenTimeout; 58 | } 59 | 60 | public int getHalfOpenSuccessCount() { 61 | return halfOpenSuccessCount; 62 | } 63 | 64 | public void setHalfOpenSuccessCount(int halfOpenSuccessCount) { 65 | this.halfOpenSuccessCount = halfOpenSuccessCount; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /chapter05/my-circuit-breaker/src/main/java/deep/in/spring/cloud/DegradeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | /** 20 | * @author Jim 21 | */ 22 | public class DegradeException extends Exception { 23 | 24 | public DegradeException() { 25 | } 26 | 27 | public DegradeException(String message) { 28 | super(message); 29 | } 30 | 31 | public DegradeException(String message, Throwable cause) { 32 | super(message, cause); 33 | } 34 | 35 | public DegradeException(Throwable cause) { 36 | super(cause); 37 | } 38 | 39 | public DegradeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 40 | super(message, cause, enableSuppression, writableStackTrace); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /chapter05/my-circuit-breaker/src/main/java/deep/in/spring/cloud/State.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | /** 4 | * @author Jim 5 | */ 6 | public enum State { 7 | 8 | CLOSED, 9 | HALF_OPEN, 10 | OPEN 11 | 12 | } 13 | -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-dashboard/README.md: -------------------------------------------------------------------------------- 1 | ## 5.5.3 Hystrix Dashboard 2 | 3 | Hystrix Dashboard 应用。 -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-dashboard/src/main/java/deep/in/spring/cloud/HystrixDashboard.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 22 | 23 | /** 24 | * @author Jim 25 | */ 26 | @SpringBootApplication 27 | @EnableHystrixDashboard 28 | public class HystrixDashboard { 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(HystrixDashboard.class, args); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-dashboard/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8888 2 | spring.application.name=hystrix-dashboard 3 | -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-spring-cloud-gateway/README.md: -------------------------------------------------------------------------------- 1 | ## 5.5.3 Hystrix 限流 2 | 3 | Hystrix 对 Spring Cloud Gateway 网关限流的例子。 -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-spring-cloud-gateway/scripts/provider1.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | while true 3 | do 4 | echo `curl -s -XGET http://localhost:8080/dubbo/status/500` 5 | done -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-spring-cloud-gateway/scripts/provider2.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | while true 3 | do 4 | echo `curl -s -XGET http://localhost:8080/springcloud/status/500` 5 | done -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-spring-cloud-gateway/scripts/provider3.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | while true 3 | do 4 | echo `curl -s -XGET http://localhost:8080/s-c-alibaba/status/500` 5 | done -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-spring-cloud-gateway/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | application: 5 | name: hystrix-spring-cloud-gateway 6 | cloud: 7 | gateway: 8 | enabled: true 9 | discovery: 10 | locator: 11 | lower-case-service-id: true 12 | loadbalancer: 13 | ribbon: 14 | enabled: false 15 | 16 | management: 17 | endpoints: 18 | web: 19 | exposure: 20 | include: "*" 21 | -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-zuul/README.md: -------------------------------------------------------------------------------- 1 | ## 5.5.3 Hystrix 限流 2 | 3 | Hystrix 对 Netflix Zuul 网关限流的例子。 -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-zuul/scripts/provider1.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | while true 3 | do 4 | echo `curl -s -XGET http://localhost:8080/dubbo/123` 5 | done -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-zuul/scripts/provider2.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | while true 3 | do 4 | echo `curl -s -XGET http://localhost:8080/springcloud/123` 5 | done -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-zuul/scripts/provider3.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | while true 3 | do 4 | echo `curl -s -XGET http://localhost:8080/s-c-alibaba/123` 5 | done -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-zuul/src/main/java/deep/in/spring/cloud/HystrixZuulApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 22 | 23 | /** 24 | * @author Jim 25 | */ 26 | @SpringBootApplication 27 | @EnableZuulProxy 28 | public class HystrixZuulApplication { 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(HystrixZuulApplication.class, args); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter05/netflix-hystrix-zuul/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8080 2 | spring.application.name=hystrix-zuul 3 | 4 | zuul.routes.my-provider1.path=/dubbo/** 5 | zuul.routes.my-provider1.service-id=my-provider1 6 | 7 | zuul.routes.my-provider2.path=/springcloud/** 8 | zuul.routes.my-provider2.service-id=my-provider2 9 | 10 | zuul.routes.my-provider3.path=/s-c-alibaba/** 11 | zuul.routes.my-provider3.url=my-provider3 12 | 13 | 14 | hystrix.command.my-provider1.execution.isolation.semaphore.maxConcurrentRequests=0 15 | hystrix.command.my-provider1.execution.isolation.strategy=SEMAPHORE 16 | hystrix.command.my-provider1.execution.timeout.enabled=false 17 | 18 | hystrix.command.my-provider2.execution.isolation.thread.timeoutInMilliseconds=5000 19 | hystrix.command.my-provider2.metrics.rollingStats.timeInMilliseconds=10000 20 | hystrix.command.my-provider2.circuitBreaker.requestVolumeThreshold=15 21 | hystrix.command.my-provider2.circuitBreaker.errorThresholdPercentage=50 22 | hystrix.command.my-provider2.circuitBreaker.sleepWindowInMilliseconds=5000 23 | 24 | hystrix.command.my-provider3.execution.isolation.thread.timeoutInMilliseconds=5000 25 | hystrix.command.my-provider3.metrics.rollingStats.timeInMilliseconds=10000 26 | hystrix.command.my-provider3.circuitBreaker.requestVolumeThreshold=30 27 | hystrix.command.my-provider3.circuitBreaker.errorThresholdPercentage=50 28 | hystrix.command.my-provider3.circuitBreaker.sleepWindowInMilliseconds=5000 29 | 30 | management.endpoints.web.exposure.include=* 31 | -------------------------------------------------------------------------------- /chapter05/netflix-hystrix/README.md: -------------------------------------------------------------------------------- 1 | ## 5.5 Netflix Hystrix 2 | 3 | Hystrix 与 OpenFeign,Spring Cloud Circuit Breaker 的整合。 4 | 5 | 还包括了 Hystrix 自身的熔断、限流使用。 -------------------------------------------------------------------------------- /chapter05/netflix-hystrix/src/main/java/deep/in/spring/cloud/command/HelloWorldCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud.command; 18 | 19 | import com.netflix.hystrix.HystrixCommand; 20 | import com.netflix.hystrix.HystrixCommandGroupKey; 21 | import org.springframework.web.client.RestTemplate; 22 | 23 | /** 24 | * @author Jim 25 | */ 26 | public class HelloWorldCommand extends HystrixCommand { 27 | 28 | private final RestTemplate restTemplate = new RestTemplate(); 29 | private final String code; 30 | 31 | public HelloWorldCommand(String code) { 32 | super(HystrixCommandGroupKey.Factory.asKey("HelloWorldExample")); 33 | this.code = code; 34 | } 35 | 36 | @Override 37 | protected String run() { 38 | String url = "http://httpbin.org/status/" + code; 39 | System.out.println("start to curl: " + url); 40 | restTemplate.getForObject(url, String.class); 41 | return "Request success"; 42 | } 43 | 44 | @Override 45 | protected String getFallback() { 46 | return "Request failed"; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /chapter05/netflix-hystrix/src/main/java/deep/in/spring/cloud/command/TimeoutRestCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud.command; 18 | 19 | import com.netflix.hystrix.HystrixCommand; 20 | import com.netflix.hystrix.HystrixCommandGroupKey; 21 | import com.netflix.hystrix.HystrixCommandProperties; 22 | import org.springframework.web.client.RestTemplate; 23 | 24 | /** 25 | * @author Jim 26 | */ 27 | public class TimeoutRestCommand extends HystrixCommand { 28 | 29 | private final RestTemplate restTemplate = new RestTemplate(); 30 | 31 | private final int seconds; 32 | 33 | public TimeoutRestCommand(int seconds) { 34 | super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TimeoutRestExample")) 35 | .andCommandPropertiesDefaults( 36 | HystrixCommandProperties.Setter() 37 | .withExecutionTimeoutInMilliseconds(1500) 38 | //.withExecutionTimeoutInMilliseconds(100) 39 | ) 40 | ); 41 | this.seconds = seconds; 42 | } 43 | 44 | @Override 45 | protected String run() { 46 | String url = "http://httpbin.org/delay/" + seconds; 47 | System.out.println("start to curl: " + url); 48 | restTemplate.getForObject(url, String.class); 49 | return "Request success"; 50 | } 51 | 52 | @Override 53 | protected String getFallback() { 54 | return "Request failed"; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /chapter05/netflix-hystrix/src/main/resources/application-flowcontrol.properties: -------------------------------------------------------------------------------- 1 | hystrix.command.Hello.execution.isolation.semaphore.maxConcurrentRequests=0 2 | hystrix.command.Hello.execution.isolation.strategy=SEMAPHORE 3 | hystrix.command.Hello.execution.timeout.enabled=false -------------------------------------------------------------------------------- /chapter05/netflix-hystrix/src/main/resources/application-openfeign.properties: -------------------------------------------------------------------------------- 1 | feign.hystrix.enabled=true 2 | 3 | hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000 4 | hystrix.command.default.metrics.rollingStats.timeInMilliseconds=1000 5 | hystrix.command.default.circuitBreaker.requestVolumeThreshold=3 6 | hystrix.command.default.circuitBreaker.errorThresholdPercentage=100 7 | hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000 -------------------------------------------------------------------------------- /chapter05/netflix-hystrix/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.root=INFO 2 | server.port=8080 3 | spring.application.name=netflix-hystrix 4 | 5 | management.endpoints.web.exposure.include=* -------------------------------------------------------------------------------- /chapter05/order-service/README.md: -------------------------------------------------------------------------------- 1 | ## 5.7 使用 Sentinel 保护应用防止服务雪崩 2 | 3 | 订单服务对应的例子。 -------------------------------------------------------------------------------- /chapter05/order-service/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=order-service 2 | server.port=8082 3 | 4 | spring.cloud.sentinel.transport.dashboard=localhost:9090 5 | spring.cloud.nacos.discovery.server-addr=localhost:8848 6 | 7 | ribbon.ReadTimeout=10000 -------------------------------------------------------------------------------- /chapter05/resilience4j-spring-cloud/README.md: -------------------------------------------------------------------------------- 1 | ## 5.6.2 Spring Cloud Resilience4j 2 | 3 | Resilience4j 与 Spring Cloud Circuit Breaker 的整合。 -------------------------------------------------------------------------------- /chapter05/resilience4j/README.md: -------------------------------------------------------------------------------- 1 | ## 5.6.1 Resilience4j 体验 2 | 3 | Resilience4j 与 OpenFeign 的整合。 4 | 5 | 还包括了 Resilience4j 自身的熔断、限流使用。 -------------------------------------------------------------------------------- /chapter05/resilience4j/src/main/resources/application-openfeign2r4j.properties: -------------------------------------------------------------------------------- 1 | resilience4j.circuitbreaker.backends.inventory.minimum-number-of-calls=3 2 | resilience4j.circuitbreaker.backends.inventory.failure-rate-threshold=100 -------------------------------------------------------------------------------- /chapter05/resilience4j/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.root=INFO 2 | server.port=8080 3 | spring.application.name=r4j 4 | 5 | management.endpoints.web.exposure.include=* -------------------------------------------------------------------------------- /chapter05/sms-service/README.md: -------------------------------------------------------------------------------- 1 | ## 5.7 使用 Sentinel 保护应用防止服务雪崩 2 | 3 | 第三方短信服务对应的例子。 -------------------------------------------------------------------------------- /chapter05/sms-service/src/main/java/deep/in/spring/cloud/SMSApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.concurrent.atomic.AtomicInteger; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.web.bind.annotation.GetMapping; 24 | import org.springframework.web.bind.annotation.RestController; 25 | 26 | /** 27 | * @author Jim 28 | */ 29 | @SpringBootApplication 30 | public class SMSApplication { 31 | 32 | public static void main(String[] args) { 33 | SpringApplication.run(SMSApplication.class, args); 34 | } 35 | 36 | @RestController 37 | class SMSController { 38 | 39 | private AtomicInteger count = new AtomicInteger(); 40 | 41 | @GetMapping("/send") 42 | public String send(String orderId, int delaySecs) { 43 | int num = count.addAndGet(1); 44 | if(num >= 1000) { 45 | if(delaySecs > 0) { 46 | try { 47 | Thread.sleep(1000 * delaySecs); 48 | } catch (InterruptedException e) { 49 | return "Interrupted: " + e.getMessage(); 50 | } 51 | } 52 | } 53 | System.out.println(orderId + " send successfully"); 54 | return "success"; 55 | } 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /chapter05/sms-service/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=sms-service 2 | server.port=8080 3 | 4 | spring.cloud.sentinel.transport.dashboard=localhost:9090 5 | spring.cloud.nacos.discovery.server-addr=localhost:8848 -------------------------------------------------------------------------------- /chapter06/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-consumer-error/README.md: -------------------------------------------------------------------------------- 1 | ## 6.4.4 Spring Cloud Stream 高级特性 2 | 3 | 消息消费失败处理的例子。 -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-consumer-error/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.output.destination=test-output 2 | spring.cloud.stream.bindings.output.content-type=text/plain 3 | 4 | spring.cloud.stream.bindings.input.destination=test-input 5 | spring.cloud.stream.bindings.input.content-type=text/plain 6 | spring.cloud.stream.bindings.input.group=test-input-group 7 | spring.cloud.stream.bindings.input.consumer.concurrency=20 8 | 9 | 10 | spring.application.name=scs-consumer-error -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-consumer-retry/README.md: -------------------------------------------------------------------------------- 1 | ## 6.4.4 Spring Cloud Stream 高级特性 2 | 3 | 消息消费失败重试的例子。 -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-consumer-retry/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.input.destination=test-input 2 | spring.cloud.stream.bindings.input.content-type=application/json 3 | spring.cloud.stream.bindings.input.group=test-input-binder 4 | spring.cloud.stream.rocketmq.bindings.input.consumer.orderly=true 5 | spring.cloud.stream.rocketmq.bindings.input.consumer.suspendCurrentQueueTimeMillis=2000 6 | spring.cloud.stream.bindings.input.consumer.maxAttempts=3 7 | 8 | spring.cloud.stream.bindings.output.destination=test-output 9 | spring.cloud.stream.bindings.output.content-type=text/plain 10 | 11 | spring.application.name=scs-retry -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-custom-bindingtargetfactory/README.md: -------------------------------------------------------------------------------- 1 | ## 6.4.4 Spring Cloud Stream 高级特性 2 | 3 | 自定义 MessageChannel 类型的例子。 -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-custom-bindingtargetfactory/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.input.destination=test-input 2 | spring.cloud.stream.bindings.input.group=test-input-binder 3 | 4 | spring.application.name=scs-custom-bindingtargetfactory -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-metrics/README.md: -------------------------------------------------------------------------------- 1 | ## 6.4.4 Spring Cloud Stream 高级特性 2 | 3 | Metrics 指标的例子。 -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-metrics/src/main/java/deep/in/spring/cloud/SCSMetricsApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.beans.factory.annotation.Autowired; 20 | import org.springframework.boot.CommandLineRunner; 21 | import org.springframework.boot.WebApplicationType; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.boot.builder.SpringApplicationBuilder; 24 | import org.springframework.cloud.stream.annotation.EnableBinding; 25 | import org.springframework.cloud.stream.messaging.Source; 26 | import org.springframework.context.annotation.Bean; 27 | import org.springframework.integration.support.MessageBuilder; 28 | 29 | /** 30 | * @author Jim 31 | */ 32 | @SpringBootApplication 33 | @EnableBinding({Source.class}) 34 | public class SCSMetricsApplication { 35 | 36 | public static void main(String[] args) { 37 | new SpringApplicationBuilder().sources(SCSMetricsApplication.class) 38 | .web(WebApplicationType.SERVLET).run(args); 39 | } 40 | 41 | @Autowired 42 | private Source source; 43 | 44 | @Bean 45 | public CommandLineRunner runner() { 46 | return (args) -> { 47 | source.output().send(MessageBuilder.withPayload("msg1") 48 | .build()); 49 | source.output().send(MessageBuilder.withPayload("msg2") 50 | .build()); 51 | source.output().send(MessageBuilder.withPayload("msg3") 52 | .build()); 53 | }; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-metrics/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.output.destination=test-output 2 | 3 | spring.cloud.stream.metrics.key=scs-metrics 4 | spring.cloud.stream.bindings.applicationMetrics.destination=metrics-topic 5 | 6 | 7 | spring.application.name=scs-metrics 8 | 9 | server.port=8080 10 | 11 | management.endpoints.web.exposure.include=* -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-polling-consumer/README.md: -------------------------------------------------------------------------------- 1 | ## 6.4.4 Spring Cloud Stream 高级特性 2 | 3 | Polling Consumer 例子。 -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-polling-consumer/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.input.destination=test-input 2 | spring.cloud.stream.bindings.input.group=test-input-binder-polling 3 | 4 | spring.application.name=scs-polling-consumer -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-producer-error/README.md: -------------------------------------------------------------------------------- 1 | ## 6.4.4 Spring Cloud Stream 高级特性 2 | 3 | 消息发送失败处理的例子。 -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-producer-error/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.output.destination=test-output 2 | spring.cloud.stream.bindings.output.content-type=text/plain 3 | spring.cloud.stream.bindings.output.producer.errorChannelEnabled=true 4 | spring.cloud.stream.rocketmq.bindings.output.producer.sync=true 5 | 6 | spring.application.name=scs-producer-error 7 | -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-producer-partition/README.md: -------------------------------------------------------------------------------- 1 | ## 6.4.4 Spring Cloud Stream 高级特性 2 | 3 | Partition 统一的例子。 -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-producer-partition/src/main/java/deep/in/spring/cloud/MyPartitionKeyExtractor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy; 20 | import org.springframework.messaging.Message; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | public class MyPartitionKeyExtractor implements PartitionKeyExtractorStrategy { 26 | @Override 27 | public Object extractKey(Message message) { 28 | return message.getHeaders().get("partitionKey"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-producer-partition/src/main/java/deep/in/spring/cloud/MyPartitionSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import deep.in.spring.cloud.SCSProducerPartitionApplication.User; 20 | import org.springframework.cloud.stream.binder.PartitionSelectorStrategy; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | public class MyPartitionSelector implements PartitionSelectorStrategy { 26 | @Override 27 | public int selectPartition(Object key, int partitionCount) { 28 | if(key instanceof User) { 29 | return ((User) key).getId(); 30 | } 31 | return 0; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream-producer-partition/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.output.destination=test-output 2 | #spring.cloud.stream.bindings.output.producer.partition-key-extractor-name=myKeyExtractor 3 | spring.cloud.stream.bindings.output.producer.partition-key-expression=headers['partitionKey'] 4 | #spring.cloud.stream.bindings.output.producer.partition-selector-name=mySelector 5 | spring.cloud.stream.bindings.output.producer.partition-selector-expression=id 6 | 7 | spring.application.name=scs-producer-partition -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream/README.md: -------------------------------------------------------------------------------- 1 | ## 6.4.1 Spring Cloud Stream 发送和接收消息 2 | 3 | Spring Cloud Steam 项目入门例子。 -------------------------------------------------------------------------------- /chapter06/spring-cloud-stream/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.input.destination=test-input 2 | spring.cloud.stream.bindings.input.group=test-input-binder 3 | spring.cloud.stream.bindings.input.binder=kafka 4 | 5 | spring.cloud.stream.bindings.output.destination=test-output 6 | spring.cloud.stream.bindings.output.binder=rocketmq -------------------------------------------------------------------------------- /chapter06/spring-integration/README.md: -------------------------------------------------------------------------------- 1 | ## 6.3.2 Spring Integration 核心组件使用 2 | 3 | Spring Integration 项目核心功能的例子。 -------------------------------------------------------------------------------- /chapter06/spring-messaging/README.md: -------------------------------------------------------------------------------- 1 | ## 6.2.1/6.2.2 Spring 与消息 2 | 3 | spring-messaging 相关的例子。 -------------------------------------------------------------------------------- /chapter06/spring-messaging/src/main/java/deep/in/spring/cloud/MyPollableChannel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.concurrent.ArrayBlockingQueue; 20 | import java.util.concurrent.BlockingQueue; 21 | import java.util.concurrent.TimeUnit; 22 | 23 | import org.springframework.messaging.Message; 24 | import org.springframework.messaging.PollableChannel; 25 | 26 | /** 27 | * @author Jim 28 | */ 29 | public class MyPollableChannel implements PollableChannel { 30 | 31 | private BlockingQueue queue = new ArrayBlockingQueue<>(1000); 32 | 33 | @Override 34 | public Message receive() { 35 | return queue.poll(); 36 | } 37 | 38 | @Override 39 | public Message receive(long timeout) { 40 | try { 41 | return queue.poll(timeout, TimeUnit.MILLISECONDS); 42 | } catch (InterruptedException e) { 43 | e.printStackTrace(); 44 | } 45 | return null; 46 | } 47 | 48 | @Override 49 | public boolean send(Message message, long timeout) { 50 | return queue.add(message); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /chapter06/spring-messaging/src/main/java/deep/in/spring/cloud/MySubscribableChannel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.Iterator; 20 | import java.util.Random; 21 | 22 | import org.springframework.messaging.Message; 23 | import org.springframework.messaging.MessageHandler; 24 | import org.springframework.messaging.support.AbstractSubscribableChannel; 25 | import org.springframework.util.CollectionUtils; 26 | 27 | /** 28 | * @author Jim 29 | */ 30 | public class MySubscribableChannel extends AbstractSubscribableChannel { 31 | 32 | private Random random = new Random(); 33 | 34 | @Override 35 | protected boolean sendInternal(Message message, long timeout) { 36 | if (message == null || CollectionUtils.isEmpty(getSubscribers())) { 37 | return false; 38 | } 39 | Iterator iter = getSubscribers().iterator(); 40 | int index = 0, targetIndex = random.nextInt(getSubscribers().size()); 41 | while(iter.hasNext()) { 42 | MessageHandler handler = iter.next(); 43 | if(index == targetIndex) { 44 | handler.handleMessage(message); 45 | return true; 46 | } 47 | index ++; 48 | } 49 | return false; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /chapter06/spring-messaging/src/main/java/deep/in/spring/cloud/PollableChannelApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.Collections; 20 | 21 | import org.springframework.messaging.MessageHeaders; 22 | import org.springframework.messaging.support.MessageBuilder; 23 | 24 | /** 25 | * @author Jim 26 | */ 27 | public class PollableChannelApp { 28 | 29 | public static void main(String[] args) { 30 | MyPollableChannel channel = new MyPollableChannel(); 31 | channel.send(MessageBuilder.withPayload("custom payload1").setHeader("k1", "v1").build()); 32 | channel.send(MessageBuilder.withPayload("custom payload2").setHeader("k2", "v2").build()); 33 | channel.send(MessageBuilder 34 | .createMessage("custom payload3", new MessageHeaders(Collections.singletonMap("ignore", true)))); 35 | 36 | System.out.println(channel.receive()); 37 | System.out.println(channel.receive()); 38 | System.out.println(channel.receive()); 39 | System.out.println(channel.receive()); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /chapter06/spring-messaging/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8080 2 | spring.application.name=websocket-demo -------------------------------------------------------------------------------- /chapter07/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /chapter07/bus-common/README.md: -------------------------------------------------------------------------------- 1 | ## 7.3 使用 Spring Cloud Bus 完成多节点配置动态刷新 2 | 3 | 一些公共的类或接口。 -------------------------------------------------------------------------------- /chapter07/bus-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.1.RELEASE 9 | 10 | 11 | deep.in.spring.cloud 12 | bus-common 13 | 0.0.1-SNAPSHOT 14 | bus-common 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Hoxton.RELEASE 20 | 21 | 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-bus 27 | true 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.springframework.cloud 36 | spring-cloud-dependencies 37 | ${spring-cloud.version} 38 | pom 39 | import 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.apache.maven.plugins 48 | maven-compiler-plugin 49 | 50 | 8 51 | 8 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /chapter07/bus-common/src/main/java/deep/in/spring/cloud/CustomEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.cloud.bus.event.RemoteApplicationEvent; 20 | 21 | /** 22 | * @author Jim 23 | */ 24 | public class CustomEvent extends RemoteApplicationEvent { 25 | 26 | private User user; 27 | 28 | public CustomEvent() { 29 | } 30 | 31 | public CustomEvent(Object source, User user, String originService, String destinationService) { 32 | super(source, originService, destinationService); 33 | this.user = user; 34 | } 35 | 36 | public User getUser() { 37 | return user; 38 | } 39 | 40 | public void setUser(User user) { 41 | this.user = user; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /chapter07/bus-common/src/main/java/deep/in/spring/cloud/User.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | /** 20 | * @author Jim 21 | */ 22 | public class User { 23 | 24 | private long id; 25 | private String name; 26 | 27 | public User() { 28 | } 29 | 30 | public User(long id, String name) { 31 | this.id = id; 32 | this.name = name; 33 | } 34 | 35 | public long getId() { 36 | return id; 37 | } 38 | 39 | public void setId(long id) { 40 | this.id = id; 41 | } 42 | 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | public void setName(String name) { 48 | this.name = name; 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return "User{" + 54 | "id=" + id + 55 | ", name='" + name + '\'' + 56 | '}'; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /chapter07/spring-cloud-bus-node1/README.md: -------------------------------------------------------------------------------- 1 | ## 7.3 使用 Spring Cloud Bus 完成多节点配置动态刷新 2 | 3 | 节点 1。需要配合节点 2 和节点 3。 -------------------------------------------------------------------------------- /chapter07/spring-cloud-bus-node1/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=scb-node1 2 | spring.cloud.bus.id=scb-node1 3 | server.port=8080 4 | 5 | management.endpoints.web.exposure.include=* 6 | spring.cloud.bus.trace.enabled=true -------------------------------------------------------------------------------- /chapter07/spring-cloud-bus-node2/README.md: -------------------------------------------------------------------------------- 1 | ## 7.3 使用 Spring Cloud Bus 完成多节点配置动态刷新 2 | 3 | 节点 2。需要配合节点 1 和节点 3。 -------------------------------------------------------------------------------- /chapter07/spring-cloud-bus-node2/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=scb-node2 2 | spring.cloud.bus.id=scb-node2 3 | server.port=8081 4 | 5 | management.endpoints.web.exposure.include=* 6 | spring.cloud.bus.trace.enabled=true 7 | -------------------------------------------------------------------------------- /chapter07/spring-cloud-bus-node3/README.md: -------------------------------------------------------------------------------- 1 | ## 7.3 使用 Spring Cloud Bus 完成多节点配置动态刷新 2 | 3 | 节点 3。需要配合节点 1 和节点 2。 -------------------------------------------------------------------------------- /chapter07/spring-cloud-bus-node3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=scb-node3 2 | spring.cloud.bus.id=scb-node3 3 | server.port=8082 4 | 5 | management.endpoints.web.exposure.include=* 6 | spring.cloud.bus.trace.enabled=true -------------------------------------------------------------------------------- /chapter08/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /chapter08/batch-processing/spring-cloud-task-simple/README.md: -------------------------------------------------------------------------------- 1 | ## 8.3 批处理案例: 统计 Github 仓库的各项指标数据 2 | 3 | 使用 Spring Cloud Task 完成批处理。 -------------------------------------------------------------------------------- /chapter08/batch-processing/spring-cloud-task-simple/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.h2.console.enabled=true -------------------------------------------------------------------------------- /chapter08/batch-processing/spring-cloud-task-simple/src/test/java/deep/in/spring/cloud/SpringCloudTaskSimpleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package deep.in.spring.cloud; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringCloudTaskSimpleApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /chapter08/batch-processing/task-01/README.md: -------------------------------------------------------------------------------- 1 | ## 8.4 Spring Cloud Data Flow 批处理任务组合 2 | 3 | 8-4 章节里的 Task01 任务。 -------------------------------------------------------------------------------- /chapter08/batch-processing/task-01/src/main/java/deep/in/spring/cloud/Task01Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | import org.springframework.beans.factory.annotation.Value; 22 | import org.springframework.boot.CommandLineRunner; 23 | import org.springframework.boot.SpringApplication; 24 | import org.springframework.boot.autoconfigure.SpringBootApplication; 25 | import org.springframework.cloud.task.configuration.EnableTask; 26 | import org.springframework.context.annotation.Bean; 27 | 28 | /** 29 | * @author Jim 30 | */ 31 | @SpringBootApplication 32 | @EnableTask 33 | public class Task01Application { 34 | 35 | private final Logger logger = LoggerFactory.getLogger(Task01Application.class); 36 | 37 | public static void main(String[] args) { 38 | SpringApplication.run(Task01Application.class, args); 39 | } 40 | 41 | @Value("${sleep.time:5000}") 42 | private Long sleepTime; 43 | 44 | @Bean 45 | public CommandLineRunner commandLineRunner() { 46 | return args -> { 47 | logger.info("task01 will sleep: " + sleepTime); 48 | logger.info("task01 complete"); 49 | }; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /chapter08/batch-processing/task-02/README.md: -------------------------------------------------------------------------------- 1 | ## 8.4 Spring Cloud Data Flow 批处理任务组合 2 | 3 | 8-4 章节里的 Task02 任务。 -------------------------------------------------------------------------------- /chapter08/batch-processing/task-02/src/main/java/deep/in/spring/cloud/Task02Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | import org.springframework.beans.factory.annotation.Value; 22 | import org.springframework.boot.CommandLineRunner; 23 | import org.springframework.boot.SpringApplication; 24 | import org.springframework.boot.autoconfigure.SpringBootApplication; 25 | import org.springframework.cloud.task.configuration.EnableTask; 26 | import org.springframework.context.annotation.Bean; 27 | 28 | /** 29 | * @author Jim 30 | */ 31 | @SpringBootApplication 32 | @EnableTask 33 | public class Task02Application { 34 | 35 | private final Logger logger = LoggerFactory.getLogger(Task02Application.class); 36 | 37 | public static void main(String[] args) { 38 | SpringApplication.run(Task02Application.class, args); 39 | } 40 | 41 | @Value("${sleep.time:5000}") 42 | private Long sleepTime; 43 | 44 | @Bean 45 | public CommandLineRunner commandLineRunner() { 46 | return args -> { 47 | logger.info("task02 will sleep: " + sleepTime); 48 | logger.info("task02 complete"); 49 | }; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /chapter08/batch-processing/task-03/README.md: -------------------------------------------------------------------------------- 1 | ## 8.4 Spring Cloud Data Flow 批处理任务组合 2 | 3 | 8-4 章节里的 Task03 任务。 -------------------------------------------------------------------------------- /chapter08/batch-processing/task-03/src/main/java/deep/in/spring/cloud/Task03Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | import org.springframework.beans.factory.annotation.Value; 22 | import org.springframework.boot.CommandLineRunner; 23 | import org.springframework.boot.SpringApplication; 24 | import org.springframework.boot.autoconfigure.SpringBootApplication; 25 | import org.springframework.cloud.task.configuration.EnableTask; 26 | import org.springframework.context.annotation.Bean; 27 | 28 | /** 29 | * @author Jim 30 | */ 31 | @SpringBootApplication 32 | @EnableTask 33 | public class Task03Application { 34 | 35 | private final Logger logger = LoggerFactory.getLogger(Task03Application.class); 36 | 37 | public static void main(String[] args) { 38 | SpringApplication.run(Task03Application.class, args); 39 | } 40 | 41 | @Value("${sleep.time:5000}") 42 | private Long sleepTime; 43 | 44 | @Bean 45 | public CommandLineRunner commandLineRunner() { 46 | return args -> { 47 | logger.info("task03 will sleep: " + sleepTime); 48 | logger.info("task03 complete"); 49 | }; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /chapter08/batch-processing/task-04/README.md: -------------------------------------------------------------------------------- 1 | ## 8.4 Spring Cloud Data Flow 批处理任务组合 2 | 3 | 8-4 章节里的 Task04 任务。 -------------------------------------------------------------------------------- /chapter08/batch-processing/task-04/src/main/java/deep/in/spring/cloud/Task04Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | import org.springframework.beans.factory.annotation.Value; 22 | import org.springframework.boot.CommandLineRunner; 23 | import org.springframework.boot.SpringApplication; 24 | import org.springframework.boot.autoconfigure.SpringBootApplication; 25 | import org.springframework.cloud.task.configuration.EnableTask; 26 | import org.springframework.context.annotation.Bean; 27 | 28 | /** 29 | * @author Jim 30 | */ 31 | @SpringBootApplication 32 | @EnableTask 33 | public class Task04Application { 34 | 35 | private final Logger logger = LoggerFactory.getLogger(Task04Application.class); 36 | 37 | public static void main(String[] args) { 38 | SpringApplication.run(Task04Application.class, args); 39 | } 40 | 41 | @Value("${sleep.time:5000}") 42 | private Long sleepTime; 43 | 44 | @Bean 45 | public CommandLineRunner commandLineRunner() { 46 | return args -> { 47 | logger.info("task04 will sleep: " + sleepTime); 48 | logger.info("task04 complete"); 49 | }; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /chapter08/batch-processing/task-05/README.md: -------------------------------------------------------------------------------- 1 | ## 8.4 Spring Cloud Data Flow 批处理任务组合 2 | 3 | 8-4 章节里的 Task05 任务。 -------------------------------------------------------------------------------- /chapter08/batch-processing/task-05/src/main/java/deep/in/spring/cloud/Task05Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | import org.springframework.beans.factory.annotation.Value; 22 | import org.springframework.boot.CommandLineRunner; 23 | import org.springframework.boot.SpringApplication; 24 | import org.springframework.boot.autoconfigure.SpringBootApplication; 25 | import org.springframework.cloud.task.configuration.EnableTask; 26 | import org.springframework.context.annotation.Bean; 27 | 28 | /** 29 | * @author Jim 30 | */ 31 | @SpringBootApplication 32 | @EnableTask 33 | public class Task05Application { 34 | 35 | private final Logger logger = LoggerFactory.getLogger(Task05Application.class); 36 | 37 | public static void main(String[] args) { 38 | SpringApplication.run(Task05Application.class, args); 39 | } 40 | 41 | @Value("${sleep.time:5000}") 42 | private Long sleepTime; 43 | 44 | @Bean 45 | public CommandLineRunner commandLineRunner() { 46 | return args -> { 47 | logger.info("task05 will sleep: " + sleepTime); 48 | logger.info("task05 complete"); 49 | }; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /chapter08/batch-processing/task-06/README.md: -------------------------------------------------------------------------------- 1 | ## 8.4 Spring Cloud Data Flow 批处理任务组合 2 | 3 | 8-4 章节里的 Task06 任务。 -------------------------------------------------------------------------------- /chapter08/batch-processing/task-06/src/main/java/deep/in/spring/cloud/Task06Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | import org.springframework.beans.factory.annotation.Value; 22 | import org.springframework.boot.CommandLineRunner; 23 | import org.springframework.boot.SpringApplication; 24 | import org.springframework.boot.autoconfigure.SpringBootApplication; 25 | import org.springframework.cloud.task.configuration.EnableTask; 26 | import org.springframework.context.annotation.Bean; 27 | 28 | /** 29 | * @author Jim 30 | */ 31 | @SpringBootApplication 32 | @EnableTask 33 | public class Task06Application { 34 | 35 | private final Logger logger = LoggerFactory.getLogger(Task06Application.class); 36 | 37 | public static void main(String[] args) { 38 | SpringApplication.run(Task06Application.class, args); 39 | } 40 | 41 | @Value("${sleep.time:5000}") 42 | private Long sleepTime; 43 | 44 | @Bean 45 | public CommandLineRunner commandLineRunner() { 46 | return args -> { 47 | logger.info("task06 will sleep: " + sleepTime); 48 | logger.info("task06 complete"); 49 | }; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /chapter08/batch-processing/task-error/README.md: -------------------------------------------------------------------------------- 1 | ## 8.4 Spring Cloud Data Flow 批处理任务组合 2 | 3 | 8-4 章节里的 Task-Error 任务。 -------------------------------------------------------------------------------- /chapter08/scdf-common/README.md: -------------------------------------------------------------------------------- 1 | ## 8 Spring Cloud Data Flow 2 | 3 | Spring Cloud Data Flow 章节一些公共的类或接口。 -------------------------------------------------------------------------------- /chapter08/scdf-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.1.RELEASE 9 | 10 | 11 | deep.in.spring.cloud 12 | scdf-common 13 | 0.0.1-SNAPSHOT 14 | scdf-common 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Hoxton.RELEASE 20 | 21 | 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-bus 27 | true 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.springframework.cloud 36 | spring-cloud-dependencies 37 | ${spring-cloud.version} 38 | pom 39 | import 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.apache.maven.plugins 48 | maven-compiler-plugin 49 | 50 | 8 51 | 8 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /chapter08/spring-batch/README.md: -------------------------------------------------------------------------------- 1 | ## 8.9.2 案例: 使用 Spring Batch 完成便利店每日账单统计 2 | 3 | 使用 Spring Batch 完成便利店每日账单统计的例子。 -------------------------------------------------------------------------------- /chapter08/spring-batch/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.h2.console.enabled=true -------------------------------------------------------------------------------- /chapter08/spring-batch/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS BILLS 2 | ( 3 | name varchar(50), 4 | amount decimal(10,2) 5 | ); -------------------------------------------------------------------------------- /chapter08/spring-batch/src/main/resources/turnover.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "apple", 4 | "count": 10, 5 | "price": 2 6 | }, 7 | { 8 | "name": "banana", 9 | "count": 20, 10 | "price": 2.5 11 | }, 12 | { 13 | "name": "nintendo switch", 14 | "count": 2, 15 | "price": 2000 16 | }, 17 | { 18 | "name": "book: deep in spring cloud", 19 | "count": 100, 20 | "price": 45 21 | }, 22 | { 23 | "name": "iphone", 24 | "count": 10, 25 | "price": 5000 26 | } 27 | ] -------------------------------------------------------------------------------- /chapter08/spring-batch/turnover.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "apple", 4 | "count": 10, 5 | "price": 2 6 | }, 7 | { 8 | "name": "banana", 9 | "count": 20, 10 | "price": 2.5 11 | }, 12 | { 13 | "name": "nintendo switch", 14 | "count": 2, 15 | "price": 2000 16 | }, 17 | { 18 | "name": "nintendo switch", 19 | "count": 2, 20 | "price": 2000 21 | }, 22 | { 23 | "name": "book: deep in spring clou", 24 | "count": 100, 25 | "price": 45 26 | }, 27 | { 28 | "name": "iphone", 29 | "count": 10, 30 | "price": 5000 31 | } 32 | ] -------------------------------------------------------------------------------- /chapter08/spring-cloud-deployer-local/README.md: -------------------------------------------------------------------------------- 1 | ## 8.7.3 LocalAppDeployer 2 | 3 | 使用基于本地 Local 的 Spring Cloud Deploy 例子。 -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v1/README.md: -------------------------------------------------------------------------------- 1 | ## 8.6 Spring Cloud Skipper 2 | 3 | helloskipper 1.0.0 版本对应的应用。 -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v1/helloskipper-1.0.0/package.yml: -------------------------------------------------------------------------------- 1 | apiVersion: deep.in.spring.cloud/v1 2 | kind: SkipperPackageMetadata 3 | name: helloskipper 4 | version: 1.0.0 5 | description: Hello Skipper v1 -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v1/helloskipper-1.0.0/templates/helloskipper.yml: -------------------------------------------------------------------------------- 1 | apiVersion: deep.in.spring.cloud/v1 2 | kind: SpringCloudDeployerApplication 3 | metadata: 4 | name: helloskipper 5 | type: demo 6 | spec: 7 | resource: maven://deep.in.spring.cloud:hello-skipper 8 | version: {{version}} 9 | applicationProperties: 10 | {{#spec.applicationProperties.entrySet}} 11 | {{key}}: {{value}} 12 | {{/spec.applicationProperties.entrySet}} 13 | deploymentProperties: 14 | {{#spec.deploymentProperties.entrySet}} 15 | {{key}}: {{value}} 16 | {{/spec.deploymentProperties.entrySet}} 17 | -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v1/helloskipper-1.0.0/values.yml: -------------------------------------------------------------------------------- 1 | version: 1.0.0 2 | -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v1/src/main/java/deep/in/spring/cloud/HelloSkipperApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | import org.springframework.web.bind.annotation.GetMapping; 22 | import org.springframework.web.bind.annotation.RestController; 23 | 24 | /** 25 | * @author Jim 26 | */ 27 | @SpringBootApplication 28 | public class HelloSkipperApplication { 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(HelloSkipperApplication.class, args); 32 | } 33 | 34 | @RestController 35 | class HelloController { 36 | @GetMapping("/hello") 37 | String hello() { 38 | return "hello skipper: v1"; 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v2/README.md: -------------------------------------------------------------------------------- 1 | ## 8.6 Spring Cloud Skipper 2 | 3 | helloskipper 1.0.1 版本对应的应用。 -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v2/helloskipper-1.0.1/package.yml: -------------------------------------------------------------------------------- 1 | apiVersion: deep.in.spring.cloud/v1 2 | kind: SkipperPackageMetadata 3 | name: helloskipper 4 | version: 1.0.1 5 | description: Hello Skipper v1.0.1 -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v2/helloskipper-1.0.1/templates/helloskipper.yml: -------------------------------------------------------------------------------- 1 | apiVersion: deep.in.spring.cloud/v1 2 | kind: SpringCloudDeployerApplication 3 | metadata: 4 | name: helloskipper 5 | type: demo 6 | spec: 7 | resource: maven://deep.in.spring.cloud:hello-skipper 8 | version: {{version}} 9 | applicationProperties: 10 | {{#spec.applicationProperties.entrySet}} 11 | {{key}}: {{value}} 12 | {{/spec.applicationProperties.entrySet}} 13 | deploymentProperties: 14 | {{#spec.deploymentProperties.entrySet}} 15 | {{key}}: {{value}} 16 | {{/spec.deploymentProperties.entrySet}} 17 | -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v2/helloskipper-1.0.1/values.yml: -------------------------------------------------------------------------------- 1 | version: 1.0.1 2 | -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v2/src/main/java/deep/in/spring/cloud/HelloSkipperApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | import org.springframework.web.bind.annotation.GetMapping; 22 | import org.springframework.web.bind.annotation.RestController; 23 | 24 | /** 25 | * @author Jim 26 | */ 27 | @SpringBootApplication 28 | public class HelloSkipperApplication { 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(HelloSkipperApplication.class, args); 32 | } 33 | 34 | @RestController 35 | class HelloController { 36 | @GetMapping("/hello") 37 | String hello() { 38 | return "hello skipper: v1.0.1"; 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v3/README.md: -------------------------------------------------------------------------------- 1 | ## 8.6 Spring Cloud Skipper 2 | 3 | helloskipper 1.0.2 版本对应的应用。 -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v3/helloskipper-1.0.2/package.yml: -------------------------------------------------------------------------------- 1 | apiVersion: deep.in.spring.cloud/v1 2 | kind: SkipperPackageMetadata 3 | name: helloskipper 4 | version: 1.0.2 5 | description: Hello Skipper v1.0.2 -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v3/helloskipper-1.0.2/templates/helloskipper.yml: -------------------------------------------------------------------------------- 1 | apiVersion: deep.in.spring.cloud/v1 2 | kind: SpringCloudDeployerApplication 3 | metadata: 4 | name: helloskipper 5 | type: demo 6 | spec: 7 | resource: maven://deep.in.spring.cloud:hello-skipper 8 | version: {{version}} 9 | applicationProperties: 10 | {{#spec.applicationProperties.entrySet}} 11 | {{key}}: {{value}} 12 | {{/spec.applicationProperties.entrySet}} 13 | deploymentProperties: 14 | {{#spec.deploymentProperties.entrySet}} 15 | {{key}}: {{value}} 16 | {{/spec.deploymentProperties.entrySet}} 17 | -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v3/helloskipper-1.0.2/values.yml: -------------------------------------------------------------------------------- 1 | version: 1.0.2 2 | -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v3/helloskipper-upgrade.yml: -------------------------------------------------------------------------------- 1 | spec: 2 | applicationProperties: 3 | server.port: 9090 4 | deploymentProperties: 5 | spring.cloud.deployer.memory: 2048m -------------------------------------------------------------------------------- /chapter08/spring-cloud-skipper/hello-skipper-v3/src/main/java/deep/in/spring/cloud/HelloSkipperApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | import org.springframework.web.bind.annotation.GetMapping; 22 | import org.springframework.web.bind.annotation.RestController; 23 | 24 | /** 25 | * @author Jim 26 | */ 27 | @SpringBootApplication 28 | public class HelloSkipperApplication { 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(HelloSkipperApplication.class, args); 32 | } 33 | 34 | @RestController 35 | class HelloController { 36 | @GetMapping("/hello") 37 | String hello() { 38 | return "hello skipper: v1.0.2"; 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /chapter08/spring-cloud-task/README.md: -------------------------------------------------------------------------------- 1 | ## 8.8.1 体验 Spring Cloud Task 2 | 3 | Spring Cloud Task 入门的例子。 -------------------------------------------------------------------------------- /chapter08/spring-cloud-task/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.h2.console.enabled=true 2 | 3 | logging.level.org.springframework.cloud.task=DEBUG 4 | 5 | spring.cloud.task.single-instance-enabled=true 6 | spring.cloud.task.external-execution-id=888 7 | spring.cloud.task.parent-execution-id=999 -------------------------------------------------------------------------------- /chapter08/stream-processing/creditcard-fraud-detection-sink/README.md: -------------------------------------------------------------------------------- 1 | ## 8.2 流处理案例:信用卡反欺诈系统 2 | 3 | Sink 应用。 -------------------------------------------------------------------------------- /chapter08/stream-processing/creditcard-source/README.md: -------------------------------------------------------------------------------- 1 | ## 8.2 流处理案例:信用卡反欺诈系统 2 | 3 | Source 应用。 -------------------------------------------------------------------------------- /chapter09/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /chapter09/nacos-provider/README.md: -------------------------------------------------------------------------------- 1 | ## 9 网关 2 | 3 | 第 9 章网关需要调用的微服务提供者应用。 -------------------------------------------------------------------------------- /chapter09/nacos-provider/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.1.RELEASE 9 | 10 | 11 | deep.in.spring.cloud 12 | nacos-provider 13 | 0.0.1-SNAPSHOT 14 | nacos-provider 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.2.0.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | com.alibaba.cloud 29 | spring-cloud-starter-alibaba-nacos-discovery 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-actuator 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | 43 | 44 | 45 | 46 | com.alibaba.cloud 47 | spring-cloud-alibaba-dependencies 48 | ${spring-cloud-alibaba.version} 49 | pom 50 | import 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.springframework.boot 59 | spring-boot-maven-plugin 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /chapter09/nacos-provider/src/main/java/deep/in/spring/cloud/NacosProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import javax.servlet.http.HttpServletRequest; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.web.bind.annotation.GetMapping; 24 | import org.springframework.web.bind.annotation.RestController; 25 | 26 | @SpringBootApplication 27 | public class NacosProvider { 28 | 29 | public static void main(String[] args) { 30 | SpringApplication.run(NacosProvider.class, args); 31 | } 32 | 33 | @RestController 34 | class EchoController { 35 | 36 | @GetMapping("/echo") 37 | public String echo(HttpServletRequest request) { 38 | return "echo: " + request.getParameter("name"); 39 | } 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter09/nacos-provider/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-provider 2 | server.port=9090 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 -------------------------------------------------------------------------------- /chapter09/spring-cloud-gateway-serviceregistry/README.md: -------------------------------------------------------------------------------- 1 | ## 9.7 Spring Cloud Gateway 整合注册中心 2 | 3 | Spring Cloud Gateway 读取 Nacos 注册中心上的服务的例子。 -------------------------------------------------------------------------------- /chapter09/spring-cloud-gateway-serviceregistry/src/main/java/deep/in/spring/cloud/SpringCloudGatewayServiceRegistryApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | @SpringBootApplication 26 | public class SpringCloudGatewayServiceRegistryApplication { 27 | 28 | public static void main(String[] args) { 29 | SpringApplication.run(SpringCloudGatewayServiceRegistryApplication.class, args); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /chapter09/spring-cloud-gateway-serviceregistry/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-cloud-gateway-proxy 4 | cloud: 5 | nacos: 6 | discovery: 7 | server-addr: localhost:8848 8 | gateway: 9 | discovery: 10 | locator: 11 | enabled: true 12 | 13 | server: 14 | port: 8080 15 | management: 16 | endpoints: 17 | web: 18 | exposure: 19 | include: "*" 20 | -------------------------------------------------------------------------------- /chapter09/spring-cloud-gateway/README.md: -------------------------------------------------------------------------------- 1 | ## 9.3/9.4/9.5/9.6 2 | 3 | Spring Cloud Gateway 相关知识点包括 Route、Predicate、Filter 的例子。 -------------------------------------------------------------------------------- /chapter09/spring-cloud-gateway/src/main/java/deep/in/spring/cloud/SpringCloudGatewayApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | @SpringBootApplication 26 | public class SpringCloudGatewayApplication { 27 | 28 | public static void main(String[] args) { 29 | SpringApplication.run(SpringCloudGatewayApplication.class, args); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /chapter09/spring-cloud-gateway/url.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | curl -v -XGET 'http://localhost:8080/httpbin/status/400' 3 | 4 | sleep 1 5 | 6 | curl -v -XGET 'http://localhost:8080/nacos/echo?name=jim' -------------------------------------------------------------------------------- /chapter09/zuul-gateway/README.md: -------------------------------------------------------------------------------- 1 | ## 9.2 Netflix Zuul 2 | 3 | Netflix Zuul 相关知识点的例子。 -------------------------------------------------------------------------------- /chapter09/zuul-gateway/src/main/java/deep/in/spring/cloud/ZuulApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 22 | 23 | /** 24 | * @author Jim 25 | */ 26 | @SpringBootApplication 27 | @EnableZuulProxy 28 | public class ZuulApplication { 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(ZuulApplication.class, args); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter09/zuul-gateway/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=zuul-proxy 2 | server.port=8080 3 | spring.cloud.nacos.discovery.server-addr=localhost:8848 4 | 5 | 6 | zuul.routes.service1.path=/nacos/** 7 | zuul.routes.service1.service-id=nacos-provider 8 | 9 | zuul.routes.service2.path=/http/** 10 | zuul.routes.service2.url=http://httpbin.org/ -------------------------------------------------------------------------------- /chapter09/zuul-gateway/url.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | curl -v -XGET 'http://localhost:8080/http/status/400' 3 | 4 | sleep 1 5 | 6 | curl -v -XGET 'http://localhost:8080/nacos/echo?name=jim' -------------------------------------------------------------------------------- /chapter10/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /chapter10/java-function/README.md: -------------------------------------------------------------------------------- 1 | ## 10.2 Java Function 2 | 3 | Java 内置 Function 相关的例子。 -------------------------------------------------------------------------------- /chapter10/java-function/src/main/java/deep/in/spring/cloud/ConsumerApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.stream.Stream; 20 | 21 | import org.springframework.http.ResponseEntity; 22 | import org.springframework.web.client.RestTemplate; 23 | 24 | /** 25 | * @author Jim 26 | */ 27 | public class ConsumerApplication { 28 | 29 | public static void main(String[] args) { 30 | RestTemplate restTemplate = new RestTemplate(); 31 | Stream.of("200", "201", "202").forEach(code -> { 32 | ResponseEntity responseEntity = 33 | restTemplate.getForEntity("http://httpbin.org/status/" + code, String.class); 34 | System.out.println(responseEntity.getStatusCode()); 35 | }); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /chapter10/java-function/src/main/java/deep/in/spring/cloud/FunctionApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.function.Function; 20 | import java.util.stream.Collectors; 21 | import java.util.stream.Stream; 22 | 23 | /** 24 | * @author Jim 25 | */ 26 | public class FunctionApplication { 27 | 28 | public static void main(String[] args) { 29 | System.out.println(Stream.of("a", "b", "c").map(String::toUpperCase).collect(Collectors.toList())); 30 | 31 | System.out.println(func(s -> s.toUpperCase(), "a")); 32 | 33 | Function func = s -> s.toUpperCase(); 34 | 35 | System.out.println(func(func, "b")); 36 | System.out.println(func(func, "c")); 37 | } 38 | 39 | static String func(Function func, String origin) { 40 | return func.apply(origin); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter10/java-function/src/main/java/deep/in/spring/cloud/JavaFunctionApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.function.Consumer; 20 | import java.util.function.Function; 21 | 22 | import org.springframework.boot.SpringApplication; 23 | import org.springframework.boot.autoconfigure.SpringBootApplication; 24 | import org.springframework.context.ApplicationContext; 25 | import org.springframework.context.annotation.Bean; 26 | 27 | /** 28 | * @author Jim 29 | */ 30 | @SpringBootApplication 31 | public class JavaFunctionApplication { 32 | 33 | public static void main(String[] args) { 34 | ApplicationContext applicationContext = 35 | SpringApplication.run(JavaFunctionApplication.class, args); 36 | System.out.println(applicationContext.getBean("upperCase", Function.class)); 37 | System.out.println(applicationContext.getBean("upperCase", Function.class).apply("a")); 38 | } 39 | 40 | @Bean 41 | public Function upperCase() { 42 | return s -> s.toUpperCase(); 43 | } 44 | 45 | @Bean 46 | public Function lowerCase() { 47 | return s -> s.toLowerCase(); 48 | } 49 | 50 | @Bean 51 | public Consumer print() { 52 | return s -> System.out.println(s); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /chapter10/java-function/src/main/java/deep/in/spring/cloud/SupplierApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.Random; 20 | import java.util.function.Supplier; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | public class SupplierApplication { 26 | 27 | public static void main(String[] args) { 28 | 29 | Random random = new Random(); 30 | 31 | Supplier supplier100 = () -> random.nextInt(100); 32 | Supplier supplier1000 = () -> random.nextInt(1000); 33 | 34 | System.out.println(supplier100.get()); 35 | System.out.println(supplier1000.get()); 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-aws/README.md: -------------------------------------------------------------------------------- 1 | ## 10.3 Spring Cloud Function 2 | 3 | Spring Cloud Function 与 AWS Lambda 集成的例子。 -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-aws/src/main/java/deep/in/spring/cloud/UserHandlers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import deep.in.spring.cloud.SpringCloudFunctionAWSApplication.User; 20 | import org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler; 21 | 22 | /** 23 | * @author Jim 24 | */ 25 | public class UserHandlers extends SpringBootRequestHandler { 26 | } 27 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-gcp/README.md: -------------------------------------------------------------------------------- 1 | ## 10.5 案例: 使用 GCP Cloud Functions 体验 Spring Cloud Function 2 | 3 | Spring Cloud Function 与 GCP Cloud Functions 集成的例子。 -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-gcp/src/main/java/deep/in/spring/cloud/SpringCloudFunctionGCPApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.function.Function; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.context.annotation.Bean; 24 | 25 | /** 26 | * @author Jim 27 | */ 28 | @SpringBootApplication 29 | public class SpringCloudFunctionGCPApplication { 30 | 31 | public static void main(String[] args) { 32 | SpringApplication.run(SpringCloudFunctionGCPApplication.class, args); 33 | } 34 | 35 | @Bean 36 | public Function user() { 37 | return s -> s.toString(); 38 | } 39 | 40 | public static class User { 41 | private String name; 42 | 43 | public User() { 44 | } 45 | 46 | public User(String name) { 47 | this.name = name; 48 | } 49 | 50 | public String getName() { 51 | return name; 52 | } 53 | 54 | public void setName(String name) { 55 | this.name = name; 56 | } 57 | 58 | @Override 59 | public String toString() { 60 | return "User{" + 61 | "name='" + name + '\'' + 62 | '}'; 63 | } 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-gcp/src/main/resources/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Main-Class: deep.in.spring.cloud.SpringCloudFunctionGCPApplication 2 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-stream-multifuncs/README.md: -------------------------------------------------------------------------------- 1 | ## 10.4.2 Spring Cloud Function 与 Spring Cloud Stream 2 | 3 | Spring Cloud Function 和 Spring Cloud Stream 集成完成根据参数决定处理函数的例子。 -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-stream-multifuncs/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.functionRouter-in-0.destination=test-input 2 | spring.cloud.stream.bindings.functionRouter-in-0.group=test-input-function 3 | 4 | spring.cloud.function.definition=functionRouter;supplier;consume1;consume2;consume3 -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-stream-withbindingannotation/README.md: -------------------------------------------------------------------------------- 1 | ## 10.4.2 Spring Cloud Function 与 Spring Cloud Stream 2 | 3 | Spring Cloud Function 和 Spring Cloud Stream 集成的例子,这个使用了 @EnableBinding 注解。 -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-stream-withbindingannotation/src/main/java/deep/in/spring/cloud/SpringCloudFunctionStreamWithBindingAnnotationApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.function.Function; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.cloud.stream.annotation.EnableBinding; 24 | import org.springframework.cloud.stream.messaging.Processor; 25 | import org.springframework.context.annotation.Bean; 26 | 27 | /** 28 | * @author Jim 29 | */ 30 | @SpringBootApplication 31 | @EnableBinding(Processor.class) 32 | public class SpringCloudFunctionStreamWithBindingAnnotationApplication { 33 | 34 | public static void main(String[] args) { 35 | SpringApplication.run(SpringCloudFunctionStreamWithBindingAnnotationApplication.class, args); 36 | } 37 | 38 | @Bean 39 | public Function uppercase() { 40 | return x -> x.toUpperCase(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-stream-withbindingannotation/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.cloud.stream.bindings.input.destination=test-input 2 | spring.cloud.stream.bindings.input.group=test-input-function 3 | 4 | spring.cloud.stream.bindings.output.destination=upper-test-input 5 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-stream/README.md: -------------------------------------------------------------------------------- 1 | ## 10.4.2 Spring Cloud Function 与 Spring Cloud Stream 2 | 3 | Spring Cloud Function 和 Spring Cloud Stream 集成的例子。 -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-stream/src/main/java/deep/in/spring/cloud/SpringCloudFunctionStreamApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.function.Function; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.context.annotation.Bean; 24 | 25 | /** 26 | * @author Jim 27 | */ 28 | @SpringBootApplication 29 | public class SpringCloudFunctionStreamApplication { 30 | 31 | public static void main(String[] args) { 32 | SpringApplication.run(SpringCloudFunctionStreamApplication.class, args); 33 | } 34 | 35 | @Bean 36 | public Function uppercase() { 37 | return x -> x.toUpperCase(); 38 | } 39 | 40 | @Bean 41 | public Function prefix() { 42 | return x -> "prefix-" + x; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-stream/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #spring.cloud.stream.bindings.uppercase-in-0.destination=test-input 2 | #spring.cloud.stream.bindings.uppercase-in-0.group=test-input-function 3 | # 4 | #spring.cloud.stream.bindings.uppercase-out-0.destination=upper-test-input 5 | 6 | 7 | spring.cloud.function.definition=uppercase|prefix 8 | 9 | spring.cloud.stream.bindings.uppercase|prefix-in-0.destination=test-input 10 | spring.cloud.stream.bindings.uppercase|prefix-in-0.group=test-input-function 11 | spring.cloud.stream.bindings.uppercase|prefix-out-0.destination=upper-test-input -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-task/README.md: -------------------------------------------------------------------------------- 1 | ## 10.4.3 Spring Cloud Function 与 Spring Cloud Task 2 | 3 | Spring Cloud Function 和 Spring Cloud Task 集成的例子。 -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-task/src/main/java/deep/in/spring/cloud/SpringCloudFunctionTaskApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.Arrays; 20 | import java.util.List; 21 | import java.util.function.Consumer; 22 | import java.util.function.Function; 23 | import java.util.function.Supplier; 24 | import java.util.stream.Collectors; 25 | 26 | import org.springframework.boot.SpringApplication; 27 | import org.springframework.boot.autoconfigure.SpringBootApplication; 28 | import org.springframework.context.annotation.Bean; 29 | 30 | /** 31 | * @author Jim 32 | */ 33 | @SpringBootApplication 34 | public class SpringCloudFunctionTaskApplication { 35 | 36 | public static void main(String[] args) { 37 | SpringApplication.run(SpringCloudFunctionTaskApplication.class, args); 38 | } 39 | 40 | @Bean 41 | public Supplier> supplier() { 42 | return () -> Arrays.asList("200", "201", "202"); 43 | } 44 | 45 | @Bean 46 | public Function, List> function() { 47 | return (list) -> 48 | list.stream().map( item -> "prefix-" + item).collect(Collectors.toList()); 49 | } 50 | 51 | @Bean 52 | public Consumer> consumer() { 53 | return (list) -> { 54 | list.stream().forEach(System.out::println); 55 | }; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-task/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.h2.console.enabled=true 2 | 3 | spring.cloud.function.task.function=function 4 | spring.cloud.function.task.supplier=supplier 5 | spring.cloud.function.task.consumer=consumer -------------------------------------------------------------------------------- /chapter10/spring-cloud-function-web/README.md: -------------------------------------------------------------------------------- 1 | ## 10.4.3 Spring Cloud Function 与 Spring Web/WebFlux 2 | 3 | Spring Cloud Function 和 Spring Web 集成的例子。 -------------------------------------------------------------------------------- /chapter10/spring-cloud-function/README.md: -------------------------------------------------------------------------------- 1 | ## 10.3 Spring Cloud Function 2 | 3 | Spring Cloud Function 相关的例子。 -------------------------------------------------------------------------------- /chapter10/spring-cloud-function/src/main/java/functions/CustomFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package functions; 18 | 19 | import java.util.function.Function; 20 | 21 | /** 22 | * @author Jim 23 | */ 24 | public class CustomFunction implements Function { 25 | 26 | @Override 27 | public String apply(String s) { 28 | return "prefix:" + s; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-function/src/main/java/functions/MyConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package functions; 18 | 19 | import java.util.function.Consumer; 20 | 21 | /** 22 | * @author Jim 23 | */ 24 | public class MyConsumer implements Consumer { 25 | 26 | @Override 27 | public void accept(String s) { 28 | System.out.println("myconsumer:" + s); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-function/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #spring.functional.enabled=true 2 | 3 | spring.cloud.function.definition=upperCase -------------------------------------------------------------------------------- /chapter10/spring-cloud-functional-application/README.md: -------------------------------------------------------------------------------- 1 | 使用 FunctionalSpringApplication 运行一个 FaaS 程序。 -------------------------------------------------------------------------------- /chapter10/spring-cloud-functional-application/src/main/java/deep/in/spring/cloud/SpringCloudFunctionalApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package deep.in.spring.cloud; 18 | 19 | import java.util.function.Function; 20 | 21 | import org.springframework.boot.SpringBootConfiguration; 22 | import org.springframework.cloud.function.context.FunctionRegistration; 23 | import org.springframework.cloud.function.context.FunctionType; 24 | import org.springframework.cloud.function.context.FunctionalSpringApplication; 25 | import org.springframework.context.ApplicationContextInitializer; 26 | import org.springframework.context.support.GenericApplicationContext; 27 | 28 | /** 29 | * @author Jim 30 | */ 31 | @SpringBootConfiguration 32 | public class SpringCloudFunctionalApplication implements ApplicationContextInitializer { 33 | 34 | public static void main(String[] args) { 35 | FunctionalSpringApplication.run(SpringCloudFunctionalApplication.class, args); 36 | } 37 | 38 | public Function upperCase() { 39 | return value -> value.toUpperCase(); 40 | } 41 | 42 | @Override 43 | public void initialize(GenericApplicationContext context) { 44 | context.registerBean("demo", FunctionRegistration.class, 45 | () -> new FunctionRegistration<>(upperCase()) 46 | .type(FunctionType.from(String.class).to(String.class))); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /chapter10/spring-cloud-functional-application/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.functional.enabled=true --------------------------------------------------------------------------------