├── Finchley ├── .gitignore ├── docs │ ├── Dubbo 简介.md │ ├── Gateway 的Filter和GlobalFilter.md │ ├── Gateway 端点监控(actuator).md │ ├── Gateway 路由断言工厂.md │ ├── Gateway 过滤器工厂.md │ ├── Nacos作为配置中心.md │ ├── Nacos快速安装.md │ ├── Nacos服务发现.md │ ├── Nacos的基本概述.md │ ├── Sentinel @SentinelResource 注解使用.md │ ├── Sentinel API 配置规则.md │ ├── Sentinel 快速入门.md │ ├── Sentinel 流量控制.md │ ├── ZooKeeper 单机、伪集群、集群.md │ ├── spring boot RbbitMQ.md │ ├── spring boot admin.md │ ├── spring cloud feign 基本使用.md │ ├── spring cloud ribbon .md │ ├── spring cloud sleuth 整合 zipkin .md │ └── spring cloud stream RbbitMQ .md ├── gateway-cloud-client-actuator-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── gateway │ │ │ │ └── cloud │ │ │ │ └── client │ │ │ │ └── actuator │ │ │ │ └── example │ │ │ │ └── GatewayCloudClientActuatorExampleApplication.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── gateway │ │ └── cloud │ │ └── client │ │ └── actuator │ │ └── example │ │ └── GatewayCloudClientActuatorExampleApplicationTests.java ├── gateway-cloud-client-filter-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── gateway │ │ │ │ └── cloud │ │ │ │ └── client │ │ │ │ └── filter │ │ │ │ └── example │ │ │ │ └── GatewayCloudClientFilterExampleApplication.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── gateway │ │ └── cloud │ │ └── client │ │ └── filter │ │ └── example │ │ └── GatewayCloudClientFilterExampleApplicationTests.java ├── gateway-cloud-client-global-filter-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── gateway │ │ │ │ └── cloud │ │ │ │ └── client │ │ │ │ └── global │ │ │ │ └── filter │ │ │ │ └── example │ │ │ │ ├── GatewayCloudClientGlobalFilterExampleApplication.java │ │ │ │ ├── GlobalOrderFilter.java │ │ │ │ ├── GlobalTokenFilter.java │ │ │ │ └── RequestTimeFilter.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── gateway │ │ └── cloud │ │ └── client │ │ └── global │ │ └── filter │ │ └── example │ │ └── GatewayCloudClientGlobalFilterExampleApplicationTests.java ├── gateway-cloud-client-route-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── gateway │ │ │ │ └── cloud │ │ │ │ └── client │ │ │ │ └── route │ │ │ │ └── example │ │ │ │ └── GatewayCloudClientRouteExampleApplication.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── gateway │ │ └── cloud │ │ └── client │ │ └── route │ │ └── example │ │ └── GatewayCloudClientRouteExampleApplicationTests.java ├── gateway-cloud-server-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── getway │ │ │ │ └── cloud │ │ │ │ └── server │ │ │ │ └── example │ │ │ │ ├── GatewayCloudServerExampleApplication.java │ │ │ │ └── RouteTestController.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── getway │ │ └── cloud │ │ └── server │ │ └── example │ │ └── GetwayCloudServerExampleApplicationTests.java ├── nacos-cloud-config-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── nacos │ │ │ │ └── config │ │ │ │ └── example │ │ │ │ ├── CommonController.java │ │ │ │ ├── ConfigController.java │ │ │ │ └── NacosConfigApplication.java │ │ └── resources │ │ │ └── bootstrap.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── nacos │ │ └── config │ │ └── example │ │ └── NacosConfigApplicationTests.java ├── nacos-cloud-discovery-consumer-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── nacos │ │ │ │ └── cloud │ │ │ │ └── discovery │ │ │ │ └── consumer │ │ │ │ └── example │ │ │ │ ├── NacosConsumerApplication.java │ │ │ │ ├── RestTemplateController.java │ │ │ │ └── config │ │ │ │ └── RestTemplateConfig.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── nacos │ │ └── cloud │ │ └── discovery │ │ └── consumer │ │ └── example │ │ └── NacosConsumerApplicationTests.java ├── nacos-cloud-discovery-consumer-feign-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── nacos │ │ │ │ └── cloud │ │ │ │ └── discovery │ │ │ │ └── consumer │ │ │ │ └── feign │ │ │ │ └── example │ │ │ │ ├── Feign.java │ │ │ │ ├── FeignClientController.java │ │ │ │ └── NaocsConsumerFeignApplication.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── nacos │ │ └── cloud │ │ └── discovery │ │ └── consumer │ │ └── feign │ │ └── example │ │ └── CouldConsumerFeignApplicationTests.java ├── nacos-cloud-discovery-provider-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── nacos │ │ │ │ └── cloud │ │ │ │ └── provider │ │ │ │ └── example │ │ │ │ └── NacosProviderApplication.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── nacos │ │ └── cloud │ │ └── provider │ │ └── example │ │ └── NacosCouldDiscoveryServerExampleApplicationTests.java ├── pom.xml ├── sentinel-cloud-annotation-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── sentinel │ │ │ │ └── cloud │ │ │ │ └── annotation │ │ │ │ └── example │ │ │ │ ├── SentinelCloudAnnotationApplication.java │ │ │ │ ├── TestController.java │ │ │ │ └── exception │ │ │ │ ├── BlockHandlerClassException.java │ │ │ │ └── FallbackClassException.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── sentinel │ │ └── cloud │ │ └── annotation │ │ └── example │ │ └── SentinelCloudAnnotationApplication.java ├── sentinel-cloud-api-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── sentinel │ │ │ │ └── cloud │ │ │ │ └── code │ │ │ │ └── example │ │ │ │ ├── SentinelCloudApiApplication.java │ │ │ │ └── TestController.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── sentinel │ │ └── cloud │ │ └── code │ │ └── example │ │ └── SentinelCloudCodeApplicationTests.java ├── sentinel-cloud-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── sentinel │ │ │ │ └── cloud │ │ │ │ └── view │ │ │ │ └── example │ │ │ │ ├── SentinelCloudApplication.java │ │ │ │ └── TestController.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── sentinel │ │ └── cloud │ │ └── view │ │ └── example │ │ └── SentinelCloudApplicationTests.java ├── sentinel-cloud-view-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── sentinel │ │ │ │ └── cloud │ │ │ │ └── view │ │ │ │ └── example │ │ │ │ ├── FilterContextConfig.java │ │ │ │ ├── SentinelViewCloudApplication.java │ │ │ │ ├── controller │ │ │ │ └── FlowController.java │ │ │ │ └── service │ │ │ │ └── FlowService.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── sentinel │ │ └── cloud │ │ └── view │ │ └── example │ │ └── SentinelViewCloudApplication.java ├── spring-boot-admin-client-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── admin │ │ │ │ └── client │ │ │ │ └── example │ │ │ │ └── SpringBootAdminClientExampleApplication.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── admin │ │ └── client │ │ └── example │ │ └── SpringBootAdminClientExampleApplicationTests.java ├── spring-boot-admin-server-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── admin │ │ │ │ └── server │ │ │ │ └── example │ │ │ │ ├── SecuritySecureConfig.java │ │ │ │ └── SpringBootAdminServerExampleApplication.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── admin │ │ └── server │ │ └── example │ │ └── SpringBootAdminServerExampleApplicationTests.java ├── spring-boot-dubbo-zookeeper-example │ ├── consumer-example │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── spring │ │ │ │ └── boot │ │ │ │ └── dubbo │ │ │ │ └── zookeeper │ │ │ │ ├── ConsumerHelloServiceImpl.java │ │ │ │ └── DubboConsumerExampleApplication.java │ │ │ └── resources │ │ │ └── application.properties │ ├── pom.xml │ ├── provider-example │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── spring │ │ │ │ └── boot │ │ │ │ └── dubbo │ │ │ │ └── zookeeper │ │ │ │ ├── DubboProviderExampleApplication.java │ │ │ │ └── HelloServiceImpl.java │ │ │ └── resources │ │ │ └── application.properties │ └── provider-interface-example │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── spring │ │ └── boot │ │ └── dubbo │ │ └── zookeeper │ │ └── HelloService.java ├── spring-boot-rabbitmq-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── rabbitmq │ │ │ │ └── example │ │ │ │ ├── Book.java │ │ │ │ ├── RabbitConfig.java │ │ │ │ ├── SpringBootRabbitmqExampleApplication.java │ │ │ │ ├── direct │ │ │ │ ├── DirectConfig.java │ │ │ │ └── DirectReceiver.java │ │ │ │ └── topic │ │ │ │ ├── TopicReceiver.java │ │ │ │ └── TopicSender.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── rabbitmq │ │ └── example │ │ └── SpringBootRabbitmqExampleApplicationTests.java ├── spring-cloud-feign-basic-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── feign │ │ │ │ └── basic │ │ │ │ └── example │ │ │ │ ├── SpringCloudFeignBasicExampleApplication.java │ │ │ │ ├── config │ │ │ │ ├── FeignServerConfig.java │ │ │ │ └── OkHttpConfig.java │ │ │ │ ├── controller │ │ │ │ └── BasicController.java │ │ │ │ └── feign │ │ │ │ ├── FeignServerClient.java │ │ │ │ └── FeignUrlClient.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── spring │ │ └── cloud │ │ └── feign │ │ └── basic │ │ └── example │ │ └── SpringCloudFeignBasicExampleApplicationTests.java ├── spring-cloud-feign-server-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── feign │ │ │ │ └── server │ │ │ │ └── example │ │ │ │ ├── FeignServerController.java │ │ │ │ └── SpringCloudFeignServerExampleApplication.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── spring │ │ └── cloud │ │ └── feign │ │ └── server │ │ └── example │ │ └── SpringCloudFeignServerExampleApplicationTests.java ├── spring-cloud-ribbon-resttemplate-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ ├── ribbon │ │ │ │ └── RibbonConfig.java │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── ribbon │ │ │ │ └── resttemplate │ │ │ │ └── example │ │ │ │ ├── SpringCloudRibbonResttemplateExampleApplication.java │ │ │ │ ├── TestController.java │ │ │ │ └── config │ │ │ │ └── RestTemplateConfig.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── spring │ │ └── cloud │ │ └── ribbon │ │ └── resttemplate │ │ └── example │ │ └── SpringCloudRibbonResttemplateExampleApplicationTests.java ├── spring-cloud-ribbon-server-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── ribbon │ │ │ │ └── server │ │ │ │ └── example │ │ │ │ ├── SpringCloudRibbonServerExampleApplication.java │ │ │ │ └── UserController.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── spring │ │ └── cloud │ │ └── ribbon │ │ └── server │ │ └── example │ │ └── SpringCloudRibbonServerExampleApplicationTests.java ├── spring-cloud-sleuth-client-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── sleuth │ │ │ │ └── client │ │ │ │ └── example │ │ │ │ ├── SleuthClientController.java │ │ │ │ ├── SpringCloudSleuthClientExampleApplication.java │ │ │ │ └── feign │ │ │ │ └── SleuthServerFeign.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── spring │ │ └── cloud │ │ └── sleuth │ │ └── client │ │ └── example │ │ └── SpringCloudSleuthClientExampleApplicationTests.java ├── spring-cloud-sleuth-server-example │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mtcarpenter │ │ │ │ └── spring │ │ │ │ └── cloud │ │ │ │ └── sleuth │ │ │ │ └── server │ │ │ │ └── example │ │ │ │ ├── SleuthServerController.java │ │ │ │ └── SpringCloudSleuthServerExampleApplication.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── mtcarpenter │ │ └── spring │ │ └── cloud │ │ └── sleuth │ │ └── server │ │ └── example │ │ └── SpringCloudSleuthServerExampleApplicationTests.java └── spring-cloud-stream-rabbitmq-example │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── mtcarpenter │ │ │ └── stream │ │ │ └── rabbitmq │ │ │ └── example │ │ │ ├── Book.java │ │ │ ├── SpringCloudStreamRabbitmqExampleApplication.java │ │ │ ├── StreamBinding.java │ │ │ └── StreamReceiver.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── mtcarpenter │ └── stream │ └── rabbitmq │ └── example │ └── SpringCloudStreamRabbitmqExampleApplicationTests.java └── README.md /Finchley/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 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 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /Finchley/docs/Gateway 端点监控(actuator).md: -------------------------------------------------------------------------------- 1 | ## Gateway 端点监控(actuator) 2 | 3 | ## 概述 4 | 5 | Spring Cloud Gateway源码中提供了 GatewayControllerEndpoint 类来修改路由配置。 6 | 7 | ## 快速入门 8 | 9 | ### 创建应用 10 | 11 | 创建一个命名为: `gateway-cloud-client-actuator-example` 的 Spring cloud 应用。 12 | 13 | ```xml 14 | 15 | 1.8 16 | 2.1.2.RELEASE 17 | Greenwich.RELEASE 18 | 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter 24 | 25 | 26 | 27 | org.springframework.cloud 28 | spring-cloud-starter-gateway 29 | 30 | 31 | 32 | com.alibaba.cloud 33 | spring-cloud-starter-alibaba-nacos-discovery 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-actuator 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.cloud 52 | spring-cloud-dependencies 53 | ${spring.cloud.version} 54 | pom 55 | import 56 | 57 | 58 | com.alibaba.cloud 59 | spring-cloud-alibaba-dependencies 60 | ${spring.cloud.alibaba.version} 61 | pom 62 | import 63 | 64 | 65 | 66 | 67 | ``` 68 | 69 | ### 配置 70 | 71 | 在 `application.yml`中配置 72 | 73 | ````properties 74 | server: 75 | port: 8094 76 | spring: 77 | application: 78 | name: gateway-actuator 79 | cloud: 80 | nacos: 81 | discovery: 82 | server-addr: 127.0.0.1:8848 83 | gateway: 84 | routes: 85 | - id: path_route 86 | uri: https://example.org 87 | predicates: 88 | - Path=/route 89 | logging: 90 | level: 91 | org.springframework.cloud.gateway: trace 92 | org.springframework.http.server.reactive: debug 93 | org.springframework.web.reactive: debug 94 | reactor.ipc.netty: debug 95 | management: 96 | endpoints: 97 | web: 98 | exposure: 99 | # 可以使用 '*' 代表全部 100 | include: gateway 101 | ```` 102 | 103 | - `management.endpoints.web.exposure.include=gateway`: 表示将 gateway 端点暴露 104 | 105 | ### 启动测试 106 | 107 | 请求 `http://localhost:8094/actuator` 108 | 109 | ![](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/e6a9802f-6fc2-901e-c557-3535c41477b0.png) 110 | 111 | 112 | 113 | ## 监控端点列表 114 | 115 | > 例如:`routes` 的全路径是 `/actuator/gateway/globalfilters` ,以此类推。 116 | 117 | | ID | HTTP Method | Description | 118 | | :-------------- | :----------------- | :---------------------------------------------- | 119 | | `globalfilters` | GET | 展示所有的全局过滤器 | 120 | | `routefilters` | GET | 展示所有的过滤器工厂(GatewayFilter factories) | 121 | | `refresh` | POST【无消息体】 | 清空路由缓存 | 122 | | `routes` | GET | 展示路由列表 | 123 | | `routes/{id}` | GET | 展示指定id的路由的信息 | 124 | | `routes/{id}` | POST【消息体如下】 | 新增一个路由 | 125 | | `routes/{id}` | DELETE【无消息体】 | 删除一个路由 | 126 | 127 | ## 全局过滤器列表 128 | 129 | 请求地址: http://localhost:8094/actuator/gateway/globalfilters 130 | 131 | ![](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/a3062340-7a58-10bc-4b92-3a5431c5d87f.png) 132 | 133 | > 如果有自定义也会显示。 134 | 135 | ## 过滤器工厂列表 136 | 137 | 请求地址 : http://localhost:8094/actuator/gateway/routefilters 138 | 139 | ![](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/a15143c2-a16a-425a-3eef-a11231a7d742.png) 140 | 141 | ## 路由列表 142 | 143 | 请求地址 : http://localhost:8094/actuator/gateway/routefilters 144 | 145 | ![](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/b0666c2f-5989-0df8-3d6e-639f510b9ee3.png) 146 | 147 | 148 | 149 | 150 | 151 | ## 动态新增路由 152 | 153 | 请求地址 : http://localhost:8094/actuator/gateway/routes/new_route 154 | 155 | ```json 156 | { 157 | "predicates": [{ 158 | "name": "Path", 159 | "args": { 160 | "_genkey_0": "/new" 161 | } 162 | }], 163 | "uri": "https://example.org", 164 | "filters": [], 165 | "order": 0 166 | } 167 | ``` 168 | 169 | **postman 测试** 170 | 171 | ![](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/ad909c27-b89a-6ce1-01c0-589aa22095ee.png) 172 | 173 | **动态路由新增结果如下** 174 | 175 | ![](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/d2e70f8d-9d01-2f5a-6478-339c78297fce.png) 176 | 177 | > 如果路由没有出来,可以通过端点刷新(`refresh`)。 178 | 179 | ## 文章参考 180 | 181 | - *https://cloud.spring.io/spring-cloud-gateway/multi/multi__actuator_api.html* 182 | 183 | ## 代码示例 184 | 185 | 本文示例代码访问下面查看仓库: 186 | 187 | - *Github:* [https://github.com/mtcarpenter/spring-cloud-learning](https://github.com/mtcarpenter/spring-cloud-learning) 188 | - *Gitee:* [https://gitee.com/mtcarpenter/spring-cloud-learning](https://gitee.com/mtcarpenter/spring-cloud-learning) 189 | 190 | 其中,本文示例代码名称: 191 | 192 | - `gateway-cloud-client-actuator-example`:Gateway 端点监控(actuator) -------------------------------------------------------------------------------- /Finchley/docs/Gateway 过滤器工厂.md: -------------------------------------------------------------------------------- 1 | ## Gateway 过滤器工厂 2 | 3 | ## 概述 4 | 5 | GatewayFilter Factory是Spring Cloud Gateway中提供的过滤器工厂。Spring CloudGateway的路由过滤器允许以某种方式修改传入的HTTP请求或输出的HTTP响应,只作用于特定的路由。Spring Cloud Gateway中内置了很多过滤器工厂,直接采用配置的方式使用即可,同时也支持自定义GatewayFilter Factory来实现更复杂的业务需求。 6 | 7 | ## 快速入门 8 | 9 | ### 创建应用 10 | 11 | 创建一个命名为: `gateway-cloud-client-filter-example` 的 Spring cloud 应用。 12 | 13 | ```xml 14 | 15 | 1.8 16 | 2.1.2.RELEASE 17 | Greenwich.RELEASE 18 | 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter 24 | 25 | 26 | 27 | org.springframework.cloud 28 | spring-cloud-starter-gateway 29 | 30 | 31 | 32 | com.alibaba.cloud 33 | spring-cloud-starter-alibaba-nacos-discovery 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.cloud 47 | spring-cloud-dependencies 48 | ${spring.cloud.version} 49 | pom 50 | import 51 | 52 | 53 | com.alibaba.cloud 54 | spring-cloud-alibaba-dependencies 55 | ${spring.cloud.alibaba.version} 56 | pom 57 | import 58 | 59 | 60 | 61 | 62 | ``` 63 | 64 | ### AddRequestHeader过滤器工厂 65 | 66 | 通过名称我们可以快速明白这个过滤器工厂的作用是添加请求头。 67 | 68 | 在 `application.yml`中配置 69 | 70 | ````properties 71 | server: 72 | port: 8092 73 | spring: 74 | application: 75 | name: gateway-route 76 | cloud: 77 | nacos: 78 | discovery: 79 | server-addr: 127.0.0.1:8848 80 | gateway: 81 | routes: 82 | - id: add_request_header_route 83 | uri: https://example.org 84 | predicates: 85 | - After=2020-05-01T14:45:39.145+08:00[Asia/Shanghai] 86 | filters: 87 | - AddRequestHeader=X-Request-Foo, Bar 88 | logging: 89 | level: 90 | org.springframework.cloud.gateway: trace 91 | org.springframework.http.server.reactive: debug 92 | org.springframework.web.reactive: debug 93 | reactor.ipc.netty: debug 94 | 95 | ```` 96 | 97 | - `-AddRequestHeader=X-Request-Foo, Bar` : 为请求添加名为 `X-Request-Foo` ,值为 `Bar` 的请求头。 98 | - `logging`: 设置 `gateway ` 请求打印日志级别 99 | 100 | **断点测试** 101 | 102 | 断点打在 `org.springframework.cloud.gateway.filter.NettyRoutingFilter#filter` ,debug 启动: 103 | 104 | ![](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/bd017d95-2964-0118-607c-8ed329bf7de2.png) 105 | 106 | ![](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/e432677d-8ea1-47e5-e7e5-39ca65cc22d9.png) 107 | 108 | ### AddRequestParameter 过滤器工厂 109 | 110 | 新增请求参数 111 | 112 | 在 `application.yml`中配置 113 | 114 | ```yaml 115 | spring: 116 | cloud: 117 | gateway: 118 | routes: 119 | - id: add_request_header_route 120 | uri: https://example.org 121 | predicates: 122 | - After=2020-05-01T14:45:39.145+08:00[Asia/Shanghai] 123 | filters: 124 | # 请求参数 token:abc 125 | - AddRequestParameter=token,abc 126 | ``` 127 | 128 | ### Retry 过滤器工厂 129 | 130 | Retry 过滤器工厂是一种定义的重试的过滤器工厂。在 Retry 过滤器工厂有以下 5 个参数。 131 | 132 | - retries:默认为3,重试次数,非负数 133 | - series:用来指定哪些段的状态码需要重试,默认`SERVER_ERROR`,即5xx。 134 | - statuses:用于指定哪些状态需要重试,默认为空,它跟`series`至少得指定一个。一般不怎么配置这个。 135 | - methods:于指定那些方法的请求需要重试,默认为`GET` 136 | - exceptions:用于指定哪些异常需要重试,默认为`java.io.IOException`、`java.util.concurrent.TimeoutException` 137 | 138 | ```yaml 139 | spring: 140 | cloud: 141 | gateway: 142 | routes: 143 | - id: add_request_header_route 144 | uri: https://example.org 145 | predicates: 146 | - After=2020-05-01T14:45:39.145+08:00[Asia/Shanghai] 147 | filters: 148 | # 过滤器工厂名称 149 | - name: Retry 150 | args: 151 | # 默认 3 152 | retries: 3 153 | series: SERVER_ERROR 154 | statuses: INTERNAL_SERVER_ERROR,BAD_GATEWAY 155 | methods: GET,POST,DELETE,PUT 156 | exceptions: 157 | - java.io.IOException 158 | - java.util.concurrent.TimeoutException 159 | ``` 160 | 161 | ## 文章参考 162 | 163 | - *https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway* 164 | 165 | ## 代码示例 166 | 167 | 本文示例代码访问下面查看仓库: 168 | 169 | - *Github:* [https://github.com/mtcarpenter/spring-cloud-learning](https://github.com/mtcarpenter/spring-cloud-learning) 170 | - *Gitee:* [https://gitee.com/mtcarpenter/spring-cloud-learning](https://gitee.com/mtcarpenter/spring-cloud-learning) 171 | 172 | 其中,本文示例代码名称: 173 | 174 | - `gateway-cloud-client-filter-example`:过滤器断言工厂 -------------------------------------------------------------------------------- /Finchley/docs/Nacos作为配置中心.md: -------------------------------------------------------------------------------- 1 | # Nacos 作为配置中心 2 | 3 | 4 | 5 | ## 入门 6 | 7 | ### 1、创建应用 8 | 9 | 创建一个命名为: `nacos-cloud-config-example` 的 Spring Boot 应用, 10 | 11 | ### 2、添加依赖 12 | 13 | ```xml 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.1.13.RELEASE 18 | 19 | 20 | com.mtcarpenter 21 | nacos-could-config-example 22 | 1.0.0 23 | nacos-config 24 | 25 | 26 | 1.8 27 | 2.1.0.RELEASE 28 | 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | 41 | 42 | com.alibaba.cloud 43 | spring-cloud-starter-alibaba-nacos-config 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | 53 | 54 | 55 | 56 | com.alibaba.cloud 57 | spring-cloud-alibaba-dependencies 58 | ${alibaba.version} 59 | pom 60 | import 61 | 62 | 63 | 64 | 65 | ``` 66 | 67 | - `dependencyManagement`: 主要用于主版本管理,后续所有子项目使用的依赖项为同一版本,无需在指定依赖。 68 | 69 | **注意**:版本 [2.1.x.RELEASE](https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery) 对应的是 Spring Boot 2.1.x 版本。版本 [2.0.x.RELEASE](https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery) 对应的是 Spring Boot 2.0.x 版本,版本 [1.5.x.RELEASE](https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery) 对应的是 Spring Boot 1.5.x 版本。 70 | 71 | 更多版本对应关系参考:[版本说明 Wiki](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/版本说明) 72 | 73 | ### 3、增加配置 74 | 75 | 在 `bootstrap.properties` 中配置 Nacos server 的地址: 76 | 77 | ````properties 78 | spring.application.name=nacos-config 79 | server.port=8090 80 | spring.cloud.nacos.config.server-addr=192.168.0.145:8848 81 | ```` 82 | 83 | - `port`:项目端口 84 | - `spring.application.name`:服务名称 85 | - `spring.cloud.nacos.discovery.server-addr`: nacos 服务器的地址 86 | 87 | > 说明:之所以需要配置 `spring.application.name` ,是因为它是构成 Nacos 配置管理 `dataId`字段的一部分。默认采用 `spring.application.name.properties ` 88 | 89 | ### 4、加注解 90 | 91 | 在启动类上加入注解,这里暂无注解。 92 | 93 | ### 5、控制类 94 | 95 | ```java 96 | @RestController 97 | @RequestMapping("/config") 98 | @RefreshScope 99 | public class ConfigController { 100 | 101 | @Value("${mtcarpenter.title:none}") 102 | private String mtcarpenter; 103 | 104 | 105 | @RequestMapping("/get") 106 | public String get() { 107 | return mtcarpenter; 108 | } 109 | 110 | 111 | } 112 | ``` 113 | 114 | - ` @Value`:获取`properties`中的属性 115 | - `@RefreshScope`:动态刷新服务器上的配置。 116 | 117 | ### 6 、nacos config 配置管理 118 | 119 | 访问 `nacos server`地址: http://127.0.0.1:8848/nacos/,进入配置管理。 120 | 121 | **创建配置** 122 | 123 | ![75483604-0b28-4c3a-ae12-d488555f5c45.png](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/75483604-0b28-4c3a-ae12-d488555f5c45.png) 124 | 125 | 其中: 126 | 127 | - `Data ID`:填入`nacos-config.properties`,与`spring.application.name`名字保持一致。 128 | 129 | - `Group`: 暂不做修改默认值`DEFAULT_GROUP` 130 | 131 | - `配置格式`:选择`Properties` 132 | 133 | - `配置内容`:应用要加载的配置内容,这里仅作为示例,做简单配置 134 | 135 | ```properties 136 | mtcarpenter.title=nacos-config 137 | ``` 138 | 139 | ### 7、启动项目 140 | 141 | 启动完成之后,访问 http://localhsot:8090/config/get ,返回如下: 142 | 143 | ```sh 144 | nacos-config 145 | ``` 146 | 147 | 在前面控制台加入了`@RefreshScope`,可以动态刷新配置。 148 | 149 | - `配置内容`:应用要加载的配置内容,这里仅作为示例,做简单配置 150 | 151 | ```properties 152 | mtcarpenter.title=nacos-config-title 153 | ``` 154 | 155 | 输出如下: 156 | 157 | ```sh 158 | nacos-config-title 159 | ``` 160 | 161 | ## 进阶 162 | 163 | 在 Nacos Spring Cloud 中,`dataId` 的完整格式如下: 164 | 165 | ```plain 166 | ${prefix}-${spring.profile.active}.${file-extension} 167 | ``` 168 | 169 | - `prefix` 默认为 `spring.application.name` 的值,也可以通过配置项 `spring.cloud.nacos.config.prefix`来配置。 170 | - `spring.profile.active` 即为当前环境对应的 profile,详情可以参考 [Spring Boot文档](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)。 **注意:当 `spring.profile.active` 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}.${file-extension}`** 171 | - `file-exetension` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.file-extension` 来配置。目前只支持 `properties` 和 `yaml` 类型。 172 | 173 | ### 1、自定义配置 174 | 175 | 在实际中项目中,我们的需求 `data-id`有格式要求,和`spring.application.name`并不一致,数据格式为`yaml`, 配置如下: 176 | 177 | ```properties 178 | spring.application.name=nacos-config 179 | server.port=8090 180 | spring.cloud.nacos.config.server-addr=192.168.0.145:8848 181 | 182 | # 自定义 name ,file-extension :默认 properties 可以修改为 yaml 183 | spring.cloud.nacos.config.prefix=nacos-config 184 | spring.cloud.nacos.config.file-extension=yaml 185 | ``` 186 | 187 | 对应`data-id`:`nacos-config.yaml`,`nacos server`服务端新增如下: 188 | 189 | ![b2f1e46f-a323-86a2-e18f-119f6a32a268.png](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/b2f1e46f-a323-86a2-e18f-119f6a32a268.png) 190 | 191 | 192 | 193 | ### 2、多环境 194 | 195 | 实际开发中一般环境有`test`、`dev`、`prod` 等。 196 | 197 | ```properties 198 | spring.application.name=nacos-config 199 | server.port=8090 200 | spring.cloud.nacos.config.server-addr=192.168.0.145:8848 201 | 202 | # 自定义 name ,file-extension :默认 properties 可以修改为 yaml 203 | spring.cloud.nacos.config.prefix=nacos-config 204 | spring.cloud.nacos.config.file-extension=yaml 205 | # 环境 dev test prod 206 | spring.profiles.active=dev 207 | ``` 208 | 启动查找`data-id`环境如下: 209 | ```properties 210 | 211 | # ${prefix}-${spring.profile.active}.${file-extension} 212 | nacos-config-dev.yaml 213 | ``` 214 | 215 | ### 3、服务端创建多个命名空间 216 | 217 | - **创建命名 :dev** 218 | 219 | ![7c019e22-e8f0-3648-7f40-b828a28de837.png](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/7c019e22-e8f0-3648-7f40-b828a28de837.png) 220 | 221 | - 进入配置列表:切换 `dev` ,创建配置 222 | 223 | ![19088c4c-978a-d2f0-0770-bb04cfcd98e6.png](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/19088c4c-978a-d2f0-0770-bb04cfcd98e6.png) 224 | 225 | - 修改在启动类增加 226 | 227 | ```properties 228 | spring.application.name=nacos-config 229 | server.port=8090 230 | spring.cloud.nacos.config.server-addr=192.168.0.145:8848 231 | 232 | spring.cloud.nacos.config.namespace=67f66b31-d97a-403f-988e-d56aebb4ccd8 233 | ``` 234 | 235 | - `namespace`: 创建命名空间`dev`自动生成的。 236 | 237 | ### 4、加载多个配置 238 | 239 | ```properties 240 | spring.cloud.nacos.config.ext-config[0].data-id=common.properties 241 | spring.cloud.nacos.config.ext-config[0].group=DEFAULT_GROUP 242 | spring.cloud.nacos.config.ext-config[0].refresh=true 243 | spring.cloud.nacos.config.ext-config[1].data-id=database.properties 244 | ``` 245 | 246 | - `ext-config`:数组,目前有 `dataId`、`group`、`refresh`。 247 | 248 | ### 5、加载多个共享配置 249 | 250 | 在实际开发中,数据库,日志配置文件大部分相同在实际开发中,就可以放在一个公共的文件进行引用,如下: 251 | 252 | ```properties 253 | spring.cloud.nacos.config.shared-dataids=common.properties,base-common.properties 254 | spring.cloud.nacos.config.refreshable-dataids=common.properties,base-common.properties 255 | 256 | ``` 257 | 258 | ## 代码示例 259 | 260 | 本文示例代码访问下面查看仓库: 261 | 262 | - *Github:* https://github.com/mtcarpenter/spring-cloud-learning 263 | 264 | 其中,本文示例代码名称: 265 | 266 | - `nacos-cloud-config-example`:nacos 配置中心 -------------------------------------------------------------------------------- /Finchley/docs/Nacos的基本概述.md: -------------------------------------------------------------------------------- 1 | # Nacos 的基本概述 2 | 3 | ## Nacos 概述 4 | 5 | Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。 6 | 7 | Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。 8 | 9 | ## Consul、Eureka、nacos对比 10 | 11 | ### 配置中心 12 | 13 | - eureka 不支持 14 | - consul 支持 但用起来偏麻烦,不太符合 springBoot 框架的命名风格,支持动态刷新 15 | - nacos 支持 用起来简单,符合springBoot的命名风格,支持动态刷新 16 | 17 | ### 注册中心 18 | 19 | - eureka 20 | 21 | - 依赖:不依赖ZooKeeper 22 | - 应用内/外:直接集成到应用中,依赖于应用自身完成服务的注册与发现, 23 | - ACP原则:遵循AP(可用性+分离容忍)原则,有较强的可用性,服务注册快,但牺牲了一定的一致性。 24 | - 版本迭代:目前已经不进行升级 25 | - 集成支持:只支持SpringCloud集成 26 | - 访问协议:HTTP 27 | - 雪崩保护:支持雪崩保护 28 | - 界面:英文界面,不符合国人习惯 29 | - 上手:容易 30 | 31 | - consul 32 | 33 | - 依赖:不依赖其他组件 34 | - 应用内/外:属于外部应用,侵入性小 35 | - ACP原则:遵循CP原则(一致性+分离容忍) 服务注册稍慢,由于其一致性导致了在Leader挂掉时重新选举期间真个consul不可用。 36 | - 版本迭代:目前仍然进行版本迭代 37 | - 集成支持:支持SpringCloud K8S集成 38 | - 访问协议:HTTP/DNS 39 | - 雪崩保护:不支持雪崩保护 40 | - 界面:英文界面,不符合国人习惯 41 | - 上手:复杂一点 42 | 43 | - nacos 44 | 45 | - 依赖:不依赖其他组件 46 | - 应用内/外:属于外部应用,侵入性小 47 | - ACP原则:通知遵循CP原则(一致性+分离容忍) 和AP原则(可用性+分离容忍) 48 | - 版本迭代:目前仍然进行版本迭代 49 | - 集成支持:支持Dubbo 、SpringCloud、K8S集成 50 | - 访问协议:HTTP/动态DNS/UDP 51 | - 雪崩保护:支持雪崩保护 52 | - 界面:中文界面,符合国人习惯 53 | - 上手:极易,中文文档,案例,社区活跃 54 | - Nacos 1.2.0 支持权限控制 55 | 56 | 57 | 58 | ## 什么是 Nacos? 59 | 60 | 服务(Service)是 Nacos 世界的一等公民。Nacos 支持几乎所有主流类型的“服务”的发现、配置和管理: 61 | 62 | Nacos 的关键特性包括: 63 | 64 | - **服务发现和服务健康监测** 65 | 66 | Nacos 支持基于 DNS 和基于 RPC 的服务发现。服务提供者使用 [原生SDK](https://nacos.io/zh-cn/docs/sdk.html)、[OpenAPI](https://nacos.io/zh-cn/docs/open-API.html)、或一个[独立的Agent TODO](https://nacos.io/zh-cn/docs/other-language.html)注册 Service 后,服务消费者可以使用[DNS TODO](https://nacos.io/zh-cn/docs/xx) 或[HTTP&API](https://nacos.io/zh-cn/docs/open-API.html)查找和发现服务。 67 | 68 | Nacos 提供对服务的实时的健康检查,阻止向不健康的主机或服务实例发送请求。Nacos 支持传输层 (PING 或 TCP)和应用层 (如 HTTP、MySQL、用户自定义)的健康检查。 对于复杂的云环境和网络拓扑环境中(如 VPC、边缘网络等)服务的健康检查,Nacos 提供了 agent 上报模式和服务端主动检测2种健康检查模式。Nacos 还提供了统一的健康检查仪表盘,帮助您根据健康状态管理服务的可用性及流量。 69 | 70 | - **动态配置服务** 71 | 72 | 动态配置服务可以让您以中心化、外部化和动态化的方式管理所有环境的应用配置和服务配置。 73 | 74 | 动态配置消除了配置变更时重新部署应用和服务的需要,让配置管理变得更加高效和敏捷。 75 | 76 | 配置中心化管理让实现无状态服务变得更简单,让服务按需弹性扩展变得更容易。 77 | 78 | Nacos 提供了一个简洁易用的UI ([控制台样例 Demo](http://console.nacos.io/nacos/index.html)) 帮助您管理所有的服务和应用的配置。Nacos 还提供包括配置版本跟踪、金丝雀发布、一键回滚配置以及客户端配置更新状态跟踪在内的一系列开箱即用的配置管理特性,帮助您更安全地在生产环境中管理配置变更和降低配置变更带来的风险。 79 | 80 | - **动态 DNS 服务** 81 | 82 | 动态 DNS 服务支持权重路由,让您更容易地实现中间层负载均衡、更灵活的路由策略、流量控制以及数据中心内网的简单DNS解析服务。动态DNS服务还能让您更容易地实现以 DNS 协议为基础的服务发现,以帮助您消除耦合到厂商私有服务发现 API 上的风险。 83 | 84 | Nacos 提供了一些简单的 [DNS APIs TODO](https://nacos.io/zh-cn/docs/xx) 帮助您管理服务的关联域名和可用的 IP:PORT 列表. 85 | 86 | - **服务及其元数据管理** 87 | 88 | Nacos 能让您从微服务平台建设的视角管理数据中心的所有服务及元数据,包括管理服务的描述、生命周期、服务的静态依赖分析、服务的健康状态、服务的流量管理、路由及安全策略、服务的 SLA 以及最首要的 metrics 统计数据。 89 | 90 | - [更多的特性列表 ...](https://nacos.io/zh-cn/docs/roadmap.html) 91 | 92 | ## Nacos 地图 93 | 94 | ![nacosMap.jpg](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/nacosMap.jpg) 95 | 96 | - 特性大图:要从功能特性,非功能特性,全面介绍我们要解的问题域的特性诉求 97 | - 架构大图:通过清晰架构,让您快速进入 Nacos 世界 98 | - 业务大图:利用当前特性可以支持的业务场景,及其最佳实践 99 | - 生态大图:系统梳理 Nacos 和主流技术生态的关系 100 | - 优势大图:展示 Nacos 核心竞争力 101 | - 战略大图:要从战略到战术层面讲 Nacos 的宏观优势 102 | 103 | ## Nacos 生态图 104 | 105 | ![1533045871534-e64b8031-008c-4dfc-b6e8-12a597a003fb.png](http://mtcarpenter.oss-cn-beijing.aliyuncs.com/2020/1533045871534-e64b8031-008c-4dfc-b6e8-12a597a003fb.png) 106 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-actuator-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | gateway-cloud-client-actuator-example 13 | 0.0.1-SNAPSHOT 14 | gateway-cloud-client-actuator-example 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.2.RELEASE 20 | Greenwich.RELEASE 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter 27 | 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-gateway 32 | 33 | 34 | 35 | com.alibaba.cloud 36 | spring-cloud-starter-alibaba-nacos-discovery 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-actuator 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-test 47 | test 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.cloud 55 | spring-cloud-dependencies 56 | ${spring.cloud.version} 57 | pom 58 | import 59 | 60 | 61 | com.alibaba.cloud 62 | spring-cloud-alibaba-dependencies 63 | ${spring.cloud.alibaba.version} 64 | pom 65 | import 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | org.springframework.boot 74 | spring-boot-maven-plugin 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-actuator-example/src/main/java/com/mtcarpenter/gateway/cloud/client/actuator/example/GatewayCloudClientActuatorExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.gateway.cloud.client.actuator.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class GatewayCloudClientActuatorExampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(GatewayCloudClientActuatorExampleApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-actuator-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8094 3 | spring: 4 | application: 5 | name: gateway-actuator 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: 127.0.0.1:8848 10 | gateway: 11 | routes: 12 | - id: path_route 13 | uri: https://example.org 14 | predicates: 15 | - Path=/route 16 | logging: 17 | level: 18 | org.springframework.cloud.gateway: trace 19 | org.springframework.http.server.reactive: debug 20 | org.springframework.web.reactive: debug 21 | reactor.ipc.netty: debug 22 | management: 23 | endpoints: 24 | web: 25 | exposure: 26 | # 可以使用 '*' 代表全部 27 | include: gateway -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-actuator-example/src/test/java/com/mtcarpenter/gateway/cloud/client/actuator/example/GatewayCloudClientActuatorExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.gateway.cloud.client.actuator.example; 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 GatewayCloudClientActuatorExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-filter-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | gateway-cloud-client-filter-example 13 | 0.0.1-SNAPSHOT 14 | gateway-cloud-client-filter-example 15 | 16 | 17 | 1.8 18 | 2.1.2.RELEASE 19 | Greenwich.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-starter-gateway 31 | 32 | 33 | 34 | com.alibaba.cloud 35 | spring-cloud-starter-alibaba-nacos-discovery 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-dependencies 50 | ${spring.cloud.version} 51 | pom 52 | import 53 | 54 | 55 | com.alibaba.cloud 56 | spring-cloud-alibaba-dependencies 57 | ${spring.cloud.alibaba.version} 58 | pom 59 | import 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | org.springframework.boot 68 | spring-boot-maven-plugin 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-filter-example/src/main/java/com/mtcarpenter/gateway/cloud/client/filter/example/GatewayCloudClientFilterExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.gateway.cloud.client.filter.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class GatewayCloudClientFilterExampleApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(GatewayCloudClientFilterExampleApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-filter-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8092 3 | spring: 4 | application: 5 | name: gateway-filter 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: 127.0.0.1:8848 10 | gateway: 11 | routes: 12 | - id: add_request_header_route 13 | uri: https://example.org 14 | predicates: 15 | - After=2020-05-01T14:45:39.145+08:00[Asia/Shanghai] 16 | filters: 17 | # 请求添加名为 X-Request-Foo ,值为 Bar 的请求头 18 | # - AddRequestHeader=X-Request-Foo, Bar 19 | # 请求参数 token:abc 20 | - AddRequestParameter=token,abc 21 | # 响应头请求参数 22 | - AddResponseHeader=token,abc123 23 | # 过滤器工厂名称 24 | - name: Retry 25 | args: 26 | # 默认 3 27 | retries: 3 28 | series: SERVER_ERROR 29 | statuses: INTERNAL_SERVER_ERROR,BAD_GATEWAY 30 | methods: GET,POST,DELETE,PUT 31 | exceptions: 32 | - java.io.IOException 33 | - java.util.concurrent.TimeoutException 34 | 35 | logging: 36 | level: 37 | org.springframework.cloud.gateway: trace 38 | org.springframework.http.server.reactive: debug 39 | org.springframework.web.reactive: debug 40 | reactor.ipc.netty: debug 41 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-filter-example/src/test/java/com/mtcarpenter/gateway/cloud/client/filter/example/GatewayCloudClientFilterExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.gateway.cloud.client.filter.example; 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 GatewayCloudClientFilterExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-global-filter-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | gateway-cloud-client-global-filter-example 13 | 0.0.1-SNAPSHOT 14 | gateway-cloud-client-global-filter-example 15 | 16 | 17 | 1.8 18 | 2.1.2.RELEASE 19 | Greenwich.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-starter-gateway 31 | 32 | 33 | 34 | com.alibaba.cloud 35 | spring-cloud-starter-alibaba-nacos-discovery 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-actuator 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.cloud 53 | spring-cloud-dependencies 54 | ${spring.cloud.version} 55 | pom 56 | import 57 | 58 | 59 | com.alibaba.cloud 60 | spring-cloud-alibaba-dependencies 61 | ${spring.cloud.alibaba.version} 62 | pom 63 | import 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | org.springframework.boot 72 | spring-boot-maven-plugin 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-global-filter-example/src/main/java/com/mtcarpenter/gateway/cloud/client/global/filter/example/GatewayCloudClientGlobalFilterExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.gateway.cloud.client.global.filter.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.gateway.route.RouteLocator; 6 | import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; 7 | import org.springframework.context.annotation.Bean; 8 | 9 | @SpringBootApplication 10 | public class GatewayCloudClientGlobalFilterExampleApplication { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(GatewayCloudClientGlobalFilterExampleApplication.class, args); 14 | } 15 | 16 | @Bean 17 | public RouteLocator routeLocator(RouteLocatorBuilder builder) { 18 | return builder.routes().route(r -> 19 | r.path("/route") 20 | //转发路由 21 | .uri("https://example.org") 22 | //注册自定义过滤器 23 | .filters(new RequestTimeFilter()) 24 | //给定id 25 | .id("filter_route")) 26 | .build(); 27 | } 28 | 29 | 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-global-filter-example/src/main/java/com/mtcarpenter/gateway/cloud/client/global/filter/example/GlobalOrderFilter.java: -------------------------------------------------------------------------------- 1 | 2 | package com.mtcarpenter.gateway.cloud.client.global.filter.example; 3 | 4 | import org.apache.commons.logging.Log; 5 | import org.apache.commons.logging.LogFactory; 6 | import org.springframework.cloud.gateway.filter.GlobalFilter; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.core.annotation.Order; 9 | import org.springframework.stereotype.Component; 10 | import reactor.core.publisher.Mono; 11 | 12 | /** 13 | * @author mtcarpenter 14 | * @github https://github.com/mtcarpenter/spring-cloud-learning 15 | * @desc 微信公众号:山间木匠 16 | */ 17 | @Component 18 | public class GlobalOrderFilter { 19 | private static final Log log = LogFactory.getLog(GlobalOrderFilter.class); 20 | 21 | @Bean 22 | @Order(-1) 23 | public GlobalFilter a() { 24 | return (exchange, chain) -> { 25 | log.info("first pre filter"); 26 | return chain.filter(exchange).then(Mono.fromRunnable(() -> { 27 | log.info("third post filter"); 28 | })); 29 | }; 30 | } 31 | 32 | @Bean 33 | @Order(0) 34 | public GlobalFilter b() { 35 | return (exchange, chain) -> { 36 | log.info("second pre filter"); 37 | return chain.filter(exchange).then(Mono.fromRunnable(() -> { 38 | log.info("second post filter"); 39 | })); 40 | }; 41 | } 42 | 43 | @Bean 44 | @Order(1) 45 | public GlobalFilter c() { 46 | return (exchange, chain) -> { 47 | log.info("third pre filter"); 48 | return chain.filter(exchange).then(Mono.fromRunnable(() -> { 49 | log.info("first post filter"); 50 | })); 51 | }; 52 | } 53 | } -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-global-filter-example/src/main/java/com/mtcarpenter/gateway/cloud/client/global/filter/example/GlobalTokenFilter.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.gateway.cloud.client.global.filter.example; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; 6 | import org.springframework.cloud.gateway.filter.GlobalFilter; 7 | import org.springframework.core.Ordered; 8 | import org.springframework.http.HttpStatus; 9 | import org.springframework.stereotype.Component; 10 | import org.springframework.web.server.ServerWebExchange; 11 | import reactor.core.publisher.Mono; 12 | 13 | /** 14 | * @author mtcarpenter 15 | * @github https://github.com/mtcarpenter/spring-cloud-learning 16 | * @desc 微信公众号:山间木匠 17 | */ 18 | @Component 19 | public class GlobalTokenFilter implements GlobalFilter, Ordered { 20 | 21 | private static final Log log = LogFactory.getLog(GlobalTokenFilter.class); 22 | 23 | @Override 24 | public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { 25 | String token = exchange.getRequest().getQueryParams().getFirst("token"); 26 | if (token == null || token.isEmpty()) { 27 | log.info("token is empty..."); 28 | exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED); 29 | return exchange.getResponse().setComplete(); 30 | } 31 | return chain.filter(exchange); 32 | } 33 | 34 | @Override 35 | public int getOrder() { 36 | return Ordered.HIGHEST_PRECEDENCE; 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-global-filter-example/src/main/java/com/mtcarpenter/gateway/cloud/client/global/filter/example/RequestTimeFilter.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.gateway.cloud.client.global.filter.example; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.cloud.gateway.filter.GatewayFilter; 6 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; 7 | import org.springframework.core.Ordered; 8 | import org.springframework.stereotype.Component; 9 | import org.springframework.web.server.ServerWebExchange; 10 | import reactor.core.publisher.Mono; 11 | 12 | /** 13 | * @author mtcarpenter 14 | * @github https://github.com/mtcarpenter/spring-cloud-learning 15 | * @desc 微信公众号:山间木匠 16 | */ 17 | @Component 18 | public class RequestTimeFilter implements GatewayFilter, Ordered { 19 | private static final Log log = LogFactory.getLog(GatewayFilter.class); 20 | private static final String START_TIME = "startTime"; 21 | 22 | @Override 23 | public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { 24 | exchange.getAttributes().put(START_TIME, System.currentTimeMillis()); 25 | return chain.filter(exchange).then( 26 | Mono.fromRunnable(() -> { 27 | Long startTime = exchange.getAttribute(START_TIME); 28 | if (startTime != null) { 29 | Long endTime = System.currentTimeMillis() - startTime; 30 | log.info(exchange.getRequest().getURI().getRawPath() + ": " + endTime + "ms"); 31 | } 32 | }) 33 | ); 34 | } 35 | 36 | @Override 37 | public int getOrder() { 38 | return Ordered.LOWEST_PRECEDENCE; 39 | } 40 | } -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-global-filter-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8093 3 | spring: 4 | application: 5 | name: gateway-global-filter 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: 127.0.0.1:8848 10 | # 日志打印级别 11 | logging: 12 | level: 13 | org.springframework.cloud.gateway: trace 14 | org.springframework.http.server.reactive: debug 15 | org.springframework.web.reactive: debug 16 | reactor.ipc.netty: debug -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-global-filter-example/src/test/java/com/mtcarpenter/gateway/cloud/client/global/filter/example/GatewayCloudClientGlobalFilterExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.gateway.cloud.client.global.filter.example; 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 GatewayCloudClientGlobalFilterExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-route-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | gateway-cloud-client-route-example 13 | 0.0.1-SNAPSHOT 14 | gateway-cloud-client-route-example 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.2.RELEASE 20 | Greenwich.RELEASE 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter 27 | 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-gateway 32 | 33 | 34 | 35 | com.alibaba.cloud 36 | spring-cloud-starter-alibaba-nacos-discovery 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-dependencies 50 | ${spring.cloud.version} 51 | pom 52 | import 53 | 54 | 55 | com.alibaba.cloud 56 | spring-cloud-alibaba-dependencies 57 | ${spring.cloud.alibaba.version} 58 | pom 59 | import 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | org.springframework.boot 68 | spring-boot-maven-plugin 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-route-example/src/main/java/com/mtcarpenter/gateway/cloud/client/route/example/GatewayCloudClientRouteExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.gateway.cloud.client.route.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.gateway.route.RouteLocator; 7 | import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; 8 | import org.springframework.context.annotation.Bean; 9 | 10 | import java.time.LocalDateTime; 11 | import java.time.ZoneId; 12 | import java.time.ZonedDateTime; 13 | 14 | /** 15 | * @author mtcarpenter 16 | * @github https://github.com/mtcarpenter/spring-cloud-learning 17 | * @desc 微信公众号:山间木匠 18 | */ 19 | @SpringBootApplication 20 | @EnableDiscoveryClient 21 | public class GatewayCloudClientRouteExampleApplication { 22 | 23 | public static void main(String[] args) { 24 | SpringApplication.run(GatewayCloudClientRouteExampleApplication.class, args); 25 | } 26 | 27 | /* 28 | @Bean 29 | public RouteLocator routeLocator(RouteLocatorBuilder routeLocatorBuilder){ 30 | // 1 、简单路由 31 | return routeLocatorBuilder.routes() 32 | .route(r-> r.path("/route") 33 | .uri("http://localhost:8090") 34 | .id("path_route")) 35 | .build(); 36 | } 37 | */ 38 | 39 | /* 40 | @Bean 41 | public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { 42 | ZonedDateTime datetime = LocalDateTime.now().plusDays(1).atZone(ZoneId.systemDefault()); 43 | return builder.routes() 44 | .route("path_route_before", r -> r.before(datetime) 45 | .uri("http://blog.lixc.top")) 46 | .build(); 47 | } 48 | 49 | */ 50 | 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-route-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8091 3 | spring: 4 | application: 5 | name: gateway-route 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: 127.0.0.1:8848 10 | gateway: 11 | routes: 12 | # 通过IP 地址转发 13 | - id: path_route 14 | uri: http://localhost:8090 15 | predicates: 16 | - Path=/route 17 | # 通过服务地址 18 | # - id: path_route_lb 19 | # uri: lb://gateway-server 20 | # predicates: 21 | # - Path=/route/** 22 | # Before 路由断言 23 | # - id: path_route_before 24 | # uri: http://blog.lixc.top/ 25 | # predicates: 26 | # - Before=2020-05-10T14:45:39.145+08:00[Asia/Shanghai] 27 | # After 路由断言 28 | # - id: path_route_after 29 | # uri: http://blog.lixc.top/ 30 | # predicates: 31 | # - After=2020-05-01T14:45:39.145+08:00[Asia/Shanghai] 32 | # path_route_between 路由断言 33 | # - id: path_route_between 34 | # uri: http://blog.lixc.top/ 35 | # predicates: 36 | # - Between=2020-05-01T14:45:39.145+08:00[Asia/Shanghai],2020-05-10T14:45:39.145+08:00[Asia/Shanghai] 37 | # cookie_route 38 | # - id: cookie_route 39 | # uri: lb://gateway-server 40 | # predicates: 41 | # # 当且仅当带有名为somecookie,并且值符合正则ch.p的Cookie时,匹配成功转发微服务 42 | # - Cookie=somecookie, ch.p 43 | # header_route 44 | # - id: header_route 45 | # uri: https://example.org 46 | # predicates: 47 | # # 当且仅当带有名为X-Request-Id,并且值符合正则\d+的Header时,匹配成功转发微服务 48 | # - Header=X-Request-Id, \d+ 49 | # host_route 50 | # - id: host_route 51 | # uri: https://blog.lixc.top 52 | # predicates: 53 | # # 当且仅当名为Host的Header符合**.somehost.org或**.anotherhost.org时,匹配成功转发微服务 54 | # - Host=**.somehost.org,**.anotherhost.org 55 | # method_route 56 | # - id: method_route 57 | # uri: https://blog.lixc.top 58 | # # 当且仅当HTTP请求方法是GET POST 时,才会转发用户微服务 59 | # predicates: 60 | # - Method=GET,POST -------------------------------------------------------------------------------- /Finchley/gateway-cloud-client-route-example/src/test/java/com/mtcarpenter/gateway/cloud/client/route/example/GatewayCloudClientRouteExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.gateway.cloud.client.route.example; 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 | import java.time.LocalDateTime; 9 | import java.time.ZoneId; 10 | import java.time.ZonedDateTime; 11 | 12 | @RunWith(SpringRunner.class) 13 | @SpringBootTest 14 | public class GatewayCloudClientRouteExampleApplicationTests { 15 | 16 | @Test 17 | public void contextLoads() { 18 | ZonedDateTime zonedDateTime = LocalDateTime.now().plusDays(1).atZone(ZoneId.systemDefault()); 19 | System.out.println(zonedDateTime); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-server-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | gateway-cloud-server-example 13 | 0.0.1-SNAPSHOT 14 | gateway-cloud-server-example 15 | 16 | 17 | 1.8 18 | 2.1.2.RELEASE 19 | Greenwich.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | com.alibaba.cloud 33 | spring-cloud-starter-alibaba-nacos-discovery 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.cloud 48 | spring-cloud-dependencies 49 | ${spring.cloud.version} 50 | pom 51 | import 52 | 53 | 54 | com.alibaba.cloud 55 | spring-cloud-alibaba-dependencies 56 | ${spring.cloud.alibaba.version} 57 | pom 58 | import 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | org.springframework.boot 68 | spring-boot-maven-plugin 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-server-example/src/main/java/com/mtcarpenter/getway/cloud/server/example/GatewayCloudServerExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.getway.cloud.server.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @author mtcarpenter 9 | * @github https://github.com/mtcarpenter/spring-cloud-learning 10 | * @desc 微信公众号:山间木匠 11 | */ 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class GatewayCloudServerExampleApplication { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(GatewayCloudServerExampleApplication.class, args); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-server-example/src/main/java/com/mtcarpenter/getway/cloud/server/example/RouteTestController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.getway.cloud.server.example; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.PathVariable; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import javax.servlet.http.Cookie; 9 | import javax.servlet.http.HttpServletRequest; 10 | 11 | /** 12 | * @author mtcarpenter 13 | * @github https://github.com/mtcarpenter/spring-cloud-learning 14 | * @desc 微信公众号:山间木匠 15 | */ 16 | @RestController 17 | @RequestMapping("/route") 18 | public class RouteTestController { 19 | 20 | @GetMapping(value = "") 21 | public String hello() { 22 | return "8090:hello"; 23 | } 24 | 25 | @GetMapping(value = "/sayHello/{name}") 26 | public String sayHello(@PathVariable String name) { 27 | return "8090:sayHello:" + name; 28 | } 29 | 30 | @GetMapping(value = "/cookie") 31 | public String cookie(HttpServletRequest request) { 32 | Cookie[] cookies = request.getCookies(); 33 | return "8090:cookie:" + cookies; 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Finchley/gateway-cloud-server-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8090 3 | spring: 4 | application: 5 | name: gateway-server 6 | cloud: 7 | # nacos 服务端地址 8 | nacos: 9 | discovery: 10 | server-addr: 127.0.0.1:8848 -------------------------------------------------------------------------------- /Finchley/gateway-cloud-server-example/src/test/java/com/mtcarpenter/getway/cloud/server/example/GetwayCloudServerExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.getway.cloud.server.example; 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 GetwayCloudServerExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-config-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | nacos-cloud-config-example 13 | 1.0.0 14 | nacos-config 15 | 16 | 17 | 1.8 18 | 2.1.0.RELEASE 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | 33 | com.alibaba.cloud 34 | spring-cloud-starter-alibaba-nacos-config 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | com.alibaba.cloud 48 | spring-cloud-alibaba-dependencies 49 | ${alibaba.version} 50 | pom 51 | import 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.springframework.boot 60 | spring-boot-maven-plugin 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-config-example/src/main/java/com/mtcarpenter/nacos/config/example/CommonController.java: -------------------------------------------------------------------------------- 1 | 2 | package com.mtcarpenter.nacos.config.example; 3 | 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.cloud.context.config.annotation.RefreshScope; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | @RequestMapping("/common") 11 | @RefreshScope 12 | public class CommonController { 13 | 14 | @Value("${mtcarpenter.comon:}") 15 | private String common; 16 | 17 | @RequestMapping("/common") 18 | public String common() { 19 | return common; 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Finchley/nacos-cloud-config-example/src/main/java/com/mtcarpenter/nacos/config/example/ConfigController.java: -------------------------------------------------------------------------------- 1 | 2 | package com.mtcarpenter.nacos.config.example; 3 | 4 | 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.cloud.context.config.annotation.RefreshScope; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | @RequestMapping("/config") 12 | @RefreshScope 13 | public class ConfigController { 14 | 15 | @Value("${mtcarpenter.title:none}") 16 | private String mtcarpenter; 17 | 18 | 19 | @RequestMapping("/get") 20 | public String get() { 21 | return mtcarpenter; 22 | } 23 | 24 | 25 | } -------------------------------------------------------------------------------- /Finchley/nacos-cloud-config-example/src/main/java/com/mtcarpenter/nacos/config/example/NacosConfigApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.nacos.config.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class NacosConfigApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(NacosConfigApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-config-example/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=nacos-config 2 | server.port=8090 3 | spring.cloud.nacos.config.server-addr=192.168.0.145:8848 4 | 5 | # 进阶(1) 6 | # 自定义 name ,file-extension :默认 properties 可以修改为 yaml 7 | #spring.cloud.nacos.config.prefix=nacos-config 8 | #spring.cloud.nacos.config.file-extension=yaml 9 | 10 | # 进阶(2) 11 | # 环境 12 | #spring.profiles.active=dev 13 | 14 | # 命名空间 (进阶 3) 15 | # spring.cloud.nacos.config.namespace=67f66b31-d97a-403f-988e-d56aebb4ccd8 16 | 17 | # 命名空间 (进阶 4) 18 | # 加载多个配置 19 | #spring.cloud.nacos.config.ext-config[0].data-id=common.properties 20 | #spring.cloud.nacos.config.ext-config[0].group=DEFAULT_GROUP 21 | #spring.cloud.nacos.config.ext-config[0].refresh=true 22 | #spring.cloud.nacos.config.ext-config[1].data-id=database.properties 23 | 24 | # 命名空间 (进阶 5) 25 | # 引用多个共享 配置 26 | #spring.cloud.nacos.config.shared-dataids=common.properties,base-common.properties 27 | #spring.cloud.nacos.config.refreshable-dataids=common.properties,base-common.properties 28 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-config-example/src/test/java/com/mtcarpenter/nacos/config/example/NacosConfigApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.nacos.config.example; 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 NacosConfigApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | nacos-cloud-discovery-consumer-example 13 | 0.0.1-SNAPSHOT 14 | nacos-consumer 15 | 16 | 17 | 18 | 1.8 19 | 2.1.0.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | com.alibaba.cloud 35 | spring-cloud-starter-alibaba-nacos-discovery 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | com.alibaba.cloud 49 | spring-cloud-alibaba-dependencies 50 | ${alibaba.version} 51 | pom 52 | import 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-maven-plugin 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-example/src/main/java/com/mtcarpenter/nacos/cloud/discovery/consumer/example/NacosConsumerApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.nacos.cloud.discovery.consumer.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class NacosConsumerApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(NacosConsumerApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-example/src/main/java/com/mtcarpenter/nacos/cloud/discovery/consumer/example/RestTemplateController.java: -------------------------------------------------------------------------------- 1 | 2 | package com.mtcarpenter.nacos.cloud.discovery.consumer.example; 3 | 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RestController; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | @RestController 11 | public class RestTemplateController { 12 | 13 | @Autowired 14 | private RestTemplate restTemplate; 15 | 16 | 17 | @GetMapping(value = "/echo/{name}") 18 | public String echo(@PathVariable String name) { 19 | // 原始方式通过 ip:port 进行访问 20 | // restTemplate.getForObject("http://localhost:8081/msg"+name,String.class); 21 | 22 | // 通过 spring.application.name 名称发现 23 | return restTemplate.getForObject("http://nacos-provider/echo/" + name, String.class); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-example/src/main/java/com/mtcarpenter/nacos/cloud/discovery/consumer/example/config/RestTemplateConfig.java: -------------------------------------------------------------------------------- 1 | 2 | package com.mtcarpenter.nacos.cloud.discovery.consumer.example.config; 3 | 4 | 5 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | @Configuration 11 | public class RestTemplateConfig { 12 | 13 | 14 | @Bean 15 | @LoadBalanced 16 | public RestTemplate restTemplate() { 17 | return new RestTemplate(); 18 | } 19 | } -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8085 2 | spring.application.name=nacos-consumer 3 | spring.cloud.nacos.discovery.server-addr=192.168.0.145:8848 4 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-example/src/test/java/com/mtcarpenter/nacos/cloud/discovery/consumer/example/NacosConsumerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.nacos.cloud.discovery.consumer.example; 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 NacosConsumerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-feign-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | nacos-cloud-discovery-consumer-feign-example 13 | 1.0.0 14 | could-consumer-feign 15 | 16 | 17 | 1.8 18 | 2.1.0.RELEASE 19 | Greenwich.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | 35 | com.alibaba.cloud 36 | spring-cloud-starter-alibaba-nacos-discovery 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-starter-openfeign 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.cloud 57 | spring-cloud-dependencies 58 | ${spring.cloud.version} 59 | pom 60 | import 61 | 62 | 63 | com.alibaba.cloud 64 | spring-cloud-alibaba-dependencies 65 | ${alibaba.version} 66 | pom 67 | import 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | org.springframework.boot 77 | spring-boot-maven-plugin 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-feign-example/src/main/java/com/mtcarpenter/nacos/cloud/discovery/consumer/feign/example/Feign.java: -------------------------------------------------------------------------------- 1 | 2 | package com.mtcarpenter.nacos.cloud.discovery.consumer.feign.example; 3 | 4 | import org.springframework.cloud.openfeign.FeignClient; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | 8 | 9 | @FeignClient(name = "nacos-provider") 10 | public interface Feign { 11 | 12 | /** 13 | * 请求服务提供方的 接口 14 | * @param name 15 | * @return 16 | */ 17 | @GetMapping("/echo/{name}") 18 | String echo(@PathVariable String name); 19 | } -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-feign-example/src/main/java/com/mtcarpenter/nacos/cloud/discovery/consumer/feign/example/FeignClientController.java: -------------------------------------------------------------------------------- 1 | 2 | package com.mtcarpenter.nacos.cloud.discovery.consumer.feign.example; 3 | 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | public class FeignClientController { 11 | 12 | @Autowired 13 | private Feign feign; 14 | 15 | @GetMapping(value = "/echo/{name}") 16 | public String echo(@PathVariable String name) { 17 | return feign.echo(name); 18 | } 19 | } -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-feign-example/src/main/java/com/mtcarpenter/nacos/cloud/discovery/consumer/feign/example/NaocsConsumerFeignApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.nacos.cloud.discovery.consumer.feign.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.openfeign.EnableFeignClients; 7 | 8 | @SpringBootApplication 9 | @EnableDiscoveryClient 10 | @EnableFeignClients 11 | public class NaocsConsumerFeignApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(NaocsConsumerFeignApplication.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-feign-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8087 2 | spring.application.name=nacos-consumer-feign 3 | spring.cloud.nacos.discovery.server-addr=192.168.0.145:8848 -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-consumer-feign-example/src/test/java/com/mtcarpenter/nacos/cloud/discovery/consumer/feign/example/CouldConsumerFeignApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.nacos.cloud.discovery.consumer.feign.example; 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 CouldConsumerFeignApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-provider-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | nacos-cloud-discovery-provider-example 13 | 0.0.1-SNAPSHOT 14 | nacos-could-discovery-server-example 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.0.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | 33 | com.alibaba.cloud 34 | spring-cloud-starter-alibaba-nacos-discovery 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | com.alibaba.cloud 48 | spring-cloud-alibaba-dependencies 49 | ${alibaba.version} 50 | pom 51 | import 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-maven-plugin 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-provider-example/src/main/java/com/mtcarpenter/nacos/cloud/provider/example/NacosProviderApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.nacos.cloud.provider.example; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | @SpringBootApplication 10 | @EnableDiscoveryClient 11 | public class NacosProviderApplication { 12 | 13 | @Value("${server.port}") 14 | private String port; 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(NacosProviderApplication.class, args); 18 | } 19 | 20 | 21 | @RestController 22 | class EchoController { 23 | @GetMapping(value = "/echo/{name}") 24 | public String echo(@PathVariable String name) { 25 | return port+": Hello Nacos Discovery " + name; 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-provider-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 2 | spring.application.name=nacos-provider 3 | spring.cloud.nacos.discovery.server-addr=192.168.0.145:8848 4 | -------------------------------------------------------------------------------- /Finchley/nacos-cloud-discovery-provider-example/src/test/java/com/mtcarpenter/nacos/cloud/provider/example/NacosCouldDiscoveryServerExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.nacos.cloud.provider.example; 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 NacosCouldDiscoveryServerExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.mtcarpenter 6 | spring-cloud-finchley 7 | pom 8 | 1.0.0 9 | 10 | 11 | 12 | nacos-cloud-discovery-provider-example 13 | nacos-cloud-discovery-consumer-example 14 | nacos-cloud-discovery-consumer-feign-example 15 | 16 | nacos-cloud-config-example 17 | 18 | sentinel-cloud-example 19 | sentinel-cloud-view-example 20 | sentinel-cloud-annotation-example 21 | sentinel-cloud-api-example 22 | 23 | gateway-cloud-server-example 24 | gateway-cloud-client-route-example 25 | gateway-cloud-client-filter-example 26 | gateway-cloud-client-global-filter-example 27 | gateway-cloud-client-actuator-example 28 | 29 | 30 | spring-boot-admin-server-example 31 | spring-boot-admin-client-example 32 | 33 | spring-cloud-ribbon-server-example 34 | spring-cloud-ribbon-resttemplate-example 35 | 36 | spring-cloud-feign-server-example 37 | spring-cloud-feign-basic-example 38 | 39 | spring-cloud-sleuth-server-example 40 | spring-cloud-sleuth-client-example 41 | 42 | spring-boot-rabbitmq-example 43 | spring-cloud-stream-rabbitmq-example 44 | 45 | 46 | spring-boot-dubbo-zookeeper-example 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-annotation-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | sentinel-cloud-annotation-example 13 | 0.0.1-SNAPSHOT 14 | sentinel-annotation-cloud 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.2.RELEASE 20 | Greenwich.RELEASE 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-actuator 32 | 33 | 34 | com.alibaba.cloud 35 | spring-cloud-starter-alibaba-sentinel 36 | 37 | 38 | org.projectlombok 39 | lombok 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.cloud 54 | spring-cloud-dependencies 55 | ${spring.cloud.version} 56 | pom 57 | import 58 | 59 | 60 | com.alibaba.cloud 61 | spring-cloud-alibaba-dependencies 62 | ${alibaba.version} 63 | pom 64 | import 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | org.springframework.boot 74 | spring-boot-maven-plugin 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-annotation-example/src/main/java/com/mtcarpenter/sentinel/cloud/annotation/example/SentinelCloudAnnotationApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.annotation.example; 2 | 3 | import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.annotation.Bean; 7 | 8 | /** 9 | * @author mtcarpenter 10 | * @github https://github.com/mtcarpenter/spring-cloud-learning 11 | * @desc 微信公众号:山间木匠 12 | */ 13 | @SpringBootApplication 14 | public class SentinelCloudAnnotationApplication { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(SentinelCloudAnnotationApplication.class, args); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-annotation-example/src/main/java/com/mtcarpenter/sentinel/cloud/annotation/example/TestController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.annotation.example; 2 | 3 | import com.alibaba.csp.sentinel.annotation.SentinelResource; 4 | import com.alibaba.csp.sentinel.slots.block.BlockException; 5 | import com.mtcarpenter.sentinel.cloud.annotation.example.exception.BlockHandlerClassException; 6 | import com.mtcarpenter.sentinel.cloud.annotation.example.exception.FallbackClassException; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.springframework.util.StringUtils; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | /** 12 | * @author mtcarpenter 13 | * @github https://github.com/mtcarpenter/spring-cloud-learning 14 | * @desc 微信公众号:山间木匠 15 | */ 16 | @RestController 17 | @RequestMapping("/test") 18 | @Slf4j 19 | public class TestController { 20 | 21 | @GetMapping(value = "/hello") 22 | @SentinelResource(value = "hello", blockHandler = "block") 23 | public String hello() { 24 | return "mtcarpenter:hello"; 25 | } 26 | 27 | /** 28 | * 限流降级 29 | * 30 | * @param ex 31 | * @return 32 | */ 33 | public String block(BlockException ex) { 34 | log.warn("服务被限流或者降级了 block", ex.getMessage()); 35 | return "服务被限流或者降级了 block"; 36 | } 37 | 38 | 39 | @GetMapping(value = "/degrade") 40 | @SentinelResource(value = "degrade", blockHandler = "degradeBlock", fallback = "degradeFallback") 41 | public String apiHello(@RequestParam(required = false) String name) { 42 | if (StringUtils.isEmpty(name)) { 43 | throw new IllegalArgumentException("参数为空"); 44 | } 45 | return "mtcarpenter:" + name; 46 | } 47 | 48 | public String degradeBlock(String name, BlockException ex) { 49 | log.warn("服务被限流或者降级了 block", name, ex.getMessage()); 50 | return "服务被限流或者降级了 block"; 51 | } 52 | 53 | /** 54 | * 限流降级 异常 55 | * 56 | * @return 57 | */ 58 | public String degradeFallback(String name) { 59 | log.warn("服务被限流或者降级了 异常 fallback"); 60 | return "服务被限流或者降级了 异常 fallback"; 61 | } 62 | 63 | 64 | @GetMapping(value = "/hot") 65 | @SentinelResource( 66 | value = "hot", 67 | blockHandler = "block", 68 | blockHandlerClass = BlockHandlerClassException.class, 69 | fallback = "fallback", 70 | fallbackClass = FallbackClassException.class) 71 | public String hot(@RequestParam("productId") String productId, @RequestParam("key") String key) { 72 | return productId + "---" + key; 73 | } 74 | 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-annotation-example/src/main/java/com/mtcarpenter/sentinel/cloud/annotation/example/exception/BlockHandlerClassException.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.annotation.example.exception; 2 | 3 | import com.alibaba.csp.sentinel.slots.block.BlockException; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.web.bind.annotation.RequestParam; 6 | 7 | /** 8 | * @author mtcarpenter 9 | * @github https://github.com/mtcarpenter/spring-cloud-learning 10 | * @desc 微信公众号:山间木匠 11 | */ 12 | @Slf4j 13 | public class BlockHandlerClassException { 14 | /** 15 | * 降级和限流 16 | * @param productId 17 | * @param key 18 | * @param ex 19 | * @return 20 | */ 21 | public static String block(String productId, String key, BlockException ex){ 22 | log.warn("productId={}:热点数据被限流或者降级了 err={}",productId,ex.getMessage()); 23 | return productId+":热点数据被限流或者降级了"; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-annotation-example/src/main/java/com/mtcarpenter/sentinel/cloud/annotation/example/exception/FallbackClassException.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.annotation.example.exception; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | 5 | /** 6 | * @author mtcarpenter 7 | * @github https://github.com/mtcarpenter/spring-cloud-learning 8 | * @desc 微信公众号:山间木匠 9 | */ 10 | @Slf4j 11 | public class FallbackClassException { 12 | 13 | /** 14 | * 限流降级 异常 15 | * @return 16 | */ 17 | public static String fallback(String productId, String key,Throwable ex){ 18 | log.warn("热点数据被限流或者降级了",ex); 19 | return productId+":热点数据被限流或者降级了"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-annotation-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # 服务名称 2 | spring.application.name=sentinel-annotation-example 3 | # 服务端口 4 | server.port=8081 5 | # sentinel dashboard 6 | spring.cloud.sentinel.transport.dashboard=localhost:8080 7 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-annotation-example/src/test/java/com/mtcarpenter/sentinel/cloud/annotation/example/SentinelCloudAnnotationApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.annotation.example; 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 SentinelCloudAnnotationApplication { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-api-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | sentinel-cloud-api-example 13 | 0.0.1-SNAPSHOT 14 | sentinel-api-cloud 15 | Demo project for Spring Boot 16 | 17 | 18 | 19 | 1.8 20 | 2.1.2.RELEASE 21 | Greenwich.RELEASE 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | com.alibaba.cloud 36 | spring-cloud-starter-alibaba-sentinel 37 | 38 | 39 | 40 | org.projectlombok 41 | lombok 42 | true 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-test 47 | test 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.cloud 56 | spring-cloud-dependencies 57 | ${spring.cloud.version} 58 | pom 59 | import 60 | 61 | 62 | com.alibaba.cloud 63 | spring-cloud-alibaba-dependencies 64 | ${alibaba.version} 65 | pom 66 | import 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | org.springframework.boot 76 | spring-boot-maven-plugin 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-api-example/src/main/java/com/mtcarpenter/sentinel/cloud/code/example/SentinelCloudApiApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.code.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SentinelCloudApiApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SentinelCloudApiApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-api-example/src/main/java/com/mtcarpenter/sentinel/cloud/code/example/TestController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.code.example; 2 | 3 | import com.alibaba.csp.sentinel.Entry; 4 | import com.alibaba.csp.sentinel.SphU; 5 | import com.alibaba.csp.sentinel.Tracer; 6 | import com.alibaba.csp.sentinel.slots.block.BlockException; 7 | import com.alibaba.csp.sentinel.slots.block.RuleConstant; 8 | import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; 9 | import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; 10 | import io.micrometer.core.instrument.util.StringUtils; 11 | import lombok.extern.slf4j.Slf4j; 12 | import org.springframework.web.bind.annotation.GetMapping; 13 | import org.springframework.web.bind.annotation.RequestMapping; 14 | import org.springframework.web.bind.annotation.RequestParam; 15 | import org.springframework.web.bind.annotation.RestController; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | /** 21 | * @author mtcarpenter 22 | * @github https://github.com/mtcarpenter/spring-cloud-learning 23 | * @desc 微信公众号:山间木匠 24 | */ 25 | @RestController 26 | @RequestMapping("/test") 27 | @Slf4j 28 | public class TestController { 29 | 30 | 31 | 32 | @GetMapping(value = "/initFlowRules") 33 | public String init() { 34 | // 流控规则 初始化 35 | initFlowRules(); 36 | return "mtcarpenter:init"; 37 | } 38 | 39 | /** 40 | * 流控规则初始化 41 | */ 42 | private static void initFlowRules() { 43 | List rules = new ArrayList<>(); 44 | FlowRule rule = new FlowRule(); 45 | rule.setResource("/test/hello"); 46 | rule.setGrade(RuleConstant.FLOW_GRADE_QPS); 47 | // Set limit QPS to 1. 48 | rule.setCount(1); 49 | rules.add(rule); 50 | FlowRuleManager.loadRules(rules); 51 | } 52 | 53 | @GetMapping(value = "/hello") 54 | public String hello(@RequestParam(value = "name",required = false) String name) { 55 | return "mtcarpenter:"+name; 56 | } 57 | 58 | 59 | 60 | @GetMapping(value = "/sayHello") 61 | public String sayHello(@RequestParam(value = "name", required = false) String name) { 62 | Entry entry = null; 63 | // 务必保证 finally 会被执行 64 | try { 65 | // 资源名可使用任意有业务语义的字符串,注意数目不能太多(超过 1K),超出几千请作为参数传入而不要直接作为资源名 66 | // EntryType 代表流量类型(inbound/outbound),其中系统规则只对 IN 类型的埋点生效 67 | entry = SphU.entry("sayHello"); 68 | // 被保护的业务逻辑 69 | if (StringUtils.isBlank(name)) { 70 | throw new IllegalArgumentException("不能为空"); 71 | } 72 | return "mtcarpenter:" + name; 73 | // 被保护的业务逻辑 74 | // do something... 75 | } catch (BlockException ex) { 76 | // 资源访问阻止,被限流或被降级 77 | // 进行相应的处理操作 78 | log.warn("限流,或者降级了", ex); 79 | return "限流,或者降级了"; 80 | } 81 | catch (IllegalArgumentException e2) { 82 | // 统计IllegalArgumentException【发生的次数、发生占比...】 83 | Tracer.trace(e2); 84 | return "参数非法!"; 85 | } 86 | catch (Exception ex) { 87 | // 若需要配置降级规则,需要通过这种方式记录业务异常 88 | Tracer.traceEntry(ex, entry); 89 | return "mtcarpenter:"+ex.getMessage() ; 90 | } finally { 91 | // 务必保证 exit,务必保证每个 entry 与 exit 配对 92 | if (entry != null) { 93 | entry.exit(); 94 | } 95 | } 96 | 97 | } 98 | 99 | @GetMapping(value = "/sayHelloRules") 100 | public String sayHelloRules() { 101 | List rules = new ArrayList<>(); 102 | FlowRule rule = new FlowRule(); 103 | rule.setResource("sayHello"); 104 | rule.setGrade(RuleConstant.FLOW_GRADE_QPS); 105 | // Set limit QPS to 1. 106 | rule.setCount(1); 107 | rules.add(rule); 108 | FlowRuleManager.loadRules(rules); 109 | return "mtcarpenter:sayHelloRules"; 110 | } 111 | 112 | 113 | 114 | 115 | } -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-api-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # 服务名称 2 | spring.application.name=sentinel-api-example 3 | # 服务端口 4 | server.port=8081 5 | # sentinel dashboard 6 | spring.cloud.sentinel.transport.dashboard=localhost:8080 7 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-api-example/src/test/java/com/mtcarpenter/sentinel/cloud/code/example/SentinelCloudCodeApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.code.example; 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 SentinelCloudCodeApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | sentinel-cloud-example 13 | 0.0.1-SNAPSHOT 14 | sentinel-cloud 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.2.RELEASE 20 | Greenwich.RELEASE 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-actuator 32 | 33 | 34 | com.alibaba.cloud 35 | spring-cloud-starter-alibaba-sentinel 36 | 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.cloud 55 | spring-cloud-dependencies 56 | ${spring.cloud.version} 57 | pom 58 | import 59 | 60 | 61 | com.alibaba.cloud 62 | spring-cloud-alibaba-dependencies 63 | ${alibaba.version} 64 | pom 65 | import 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | org.springframework.boot 75 | spring-boot-maven-plugin 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-example/src/main/java/com/mtcarpenter/sentinel/cloud/view/example/SentinelCloudApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.view.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SentinelCloudApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SentinelCloudApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-example/src/main/java/com/mtcarpenter/sentinel/cloud/view/example/TestController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.view.example; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | /** 8 | * @author 山间木匠 9 | * 10 | */ 11 | @RestController 12 | @RequestMapping("/test") 13 | public class TestController { 14 | 15 | @GetMapping("/echo") 16 | public String echo(){ 17 | return "mtcarpenter"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # 服务名称 2 | spring.application.name=sentinel-example 3 | # 服务端口 4 | server.port=8081 5 | # sentinel dashboard 6 | spring.cloud.sentinel.transport.dashboard=localhost:8080 7 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-example/src/test/java/com/mtcarpenter/sentinel/cloud/view/example/SentinelCloudApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.view.example; 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 SentinelCloudApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-view-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | sentinel-cloud-view-example 13 | 0.0.1-SNAPSHOT 14 | sentinel-view-cloud 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.2.RELEASE 20 | Greenwich.RELEASE 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-actuator 32 | 33 | 34 | com.alibaba.cloud 35 | spring-cloud-starter-alibaba-sentinel 36 | 37 | 38 | com.alibaba.csp 39 | sentinel-web-servlet 40 | 1.7.0 41 | 42 | 43 | 44 | org.projectlombok 45 | lombok 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-test 51 | test 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.springframework.cloud 60 | spring-cloud-dependencies 61 | ${spring.cloud.version} 62 | pom 63 | import 64 | 65 | 66 | com.alibaba.cloud 67 | spring-cloud-alibaba-dependencies 68 | ${alibaba.version} 69 | pom 70 | import 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | org.springframework.boot 80 | spring-boot-maven-plugin 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-view-example/src/main/java/com/mtcarpenter/sentinel/cloud/view/example/FilterContextConfig.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.view.example; 2 | import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter; 3 | import org.springframework.boot.web.servlet.FilterRegistrationBean; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | 8 | /** 9 | * @author 山间木匠 10 | * @github https://github.com/mtcarpenter/spring-cloud-learning 11 | */ 12 | @Configuration 13 | public class FilterContextConfig { 14 | /** 15 | * @NOTE 在spring-cloud-alibaba v2.1.1.RELEASE及前,sentinel1.7.0及后,关闭URL PATH聚合需要通过该方式,spring-cloud-alibaba v2.1.1.RELEASE后,可以通过配置关闭:spring.cloud.sentinel.web-context-unify=false 16 | * 手动注入Sentinel的过滤器,关闭Sentinel注入CommonFilter实例,修改配置文件中的 spring.cloud.sentinel.filter.enabled=false 17 | * 入口资源聚合问题:https://github.com/alibaba/Sentinel/issues/1024 或 https://github.com/alibaba/Sentinel/issues/1213 18 | * 入口资源聚合问题解决:https://github.com/alibaba/Sentinel/pull/1111 19 | */ 20 | @Bean 21 | public FilterRegistrationBean sentinelFilterRegistration() { 22 | FilterRegistrationBean registration = new FilterRegistrationBean(); 23 | registration.setFilter(new CommonFilter()); 24 | registration.addUrlPatterns("/*"); 25 | // 入口资源关闭聚合 26 | registration.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false"); 27 | registration.setName("sentinelFilter"); 28 | registration.setOrder(1); 29 | return registration; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-view-example/src/main/java/com/mtcarpenter/sentinel/cloud/view/example/SentinelViewCloudApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.view.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SentinelViewCloudApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SentinelViewCloudApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-view-example/src/main/java/com/mtcarpenter/sentinel/cloud/view/example/controller/FlowController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.view.example.controller; 2 | 3 | import com.mtcarpenter.sentinel.cloud.view.example.service.FlowService; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @author 山间木匠 11 | * 12 | */ 13 | @RestController 14 | @RequestMapping("/flow") 15 | public class FlowController { 16 | 17 | @Autowired 18 | private FlowService flowService; 19 | 20 | @GetMapping("/test") 21 | public String test(){ 22 | return "mtcarpenter:test"; 23 | } 24 | 25 | @RequestMapping("/test-a") 26 | public String testA(){ 27 | return "mtcarpenter:test-a"; 28 | } 29 | 30 | @GetMapping("/test-b") 31 | public String testB(){ 32 | this.flowService.common(); 33 | return "mtcarpenter:test-b"; 34 | } 35 | 36 | @GetMapping("/test-c") 37 | public String testC(){ 38 | this.flowService.common(); 39 | return "mtcarpenter:test-c"; 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-view-example/src/main/java/com/mtcarpenter/sentinel/cloud/view/example/service/FlowService.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.view.example.service; 2 | 3 | 4 | import com.alibaba.csp.sentinel.annotation.SentinelResource; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.stereotype.Service; 7 | 8 | 9 | @Slf4j 10 | @Service 11 | public class FlowService { 12 | 13 | @SentinelResource("common") 14 | public String common() { 15 | log.info("common...."); 16 | return "common"; 17 | } 18 | } -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-view-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # 服务名称 2 | spring.application.name=sentinel-view-example 3 | # 服务端口 4 | server.port=8081 5 | # sentinel dashboard 6 | spring.cloud.sentinel.transport.dashboard=localhost:8080 7 | spring.cloud.sentinel.filter.enabled=false 8 | #spring.cloud.sentinel.web-context-unify=false 9 | -------------------------------------------------------------------------------- /Finchley/sentinel-cloud-view-example/src/test/java/com/mtcarpenter/sentinel/cloud/view/example/SentinelViewCloudApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.sentinel.cloud.view.example; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | import org.springframework.web.client.RestTemplate; 10 | 11 | import java.util.concurrent.TimeUnit; 12 | 13 | @RunWith(SpringRunner.class) 14 | @SpringBootTest 15 | @Slf4j 16 | public class SentinelViewCloudApplication { 17 | 18 | @Test 19 | public void contextLoads() throws InterruptedException { 20 | RestTemplate restTemplate = new RestTemplate(); 21 | for (int i = 50000; i > 0; i--) { 22 | String forObject = restTemplate.getForObject("http://localhost:8081/test/echo/13", String.class); 23 | log.info("result = {}", forObject); 24 | TimeUnit.MILLISECONDS.sleep(600); 25 | } 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Finchley/spring-boot-admin-client-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | spring-boot-admin-client-example 13 | 0.0.1-SNAPSHOT 14 | spring-boot-admin-client-example 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.2.RELEASE 20 | Greenwich.RELEASE 21 | 2.1.6 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-actuator 37 | 38 | 39 | 40 | com.alibaba.cloud 41 | spring-cloud-starter-alibaba-nacos-discovery 42 | 43 | 44 | 45 | de.codecentric 46 | spring-boot-admin-starter-client 47 | ${spring.boot.admin.version} 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-starter-test 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.springframework.cloud 60 | spring-cloud-dependencies 61 | ${spring.cloud.version} 62 | pom 63 | import 64 | 65 | 66 | com.alibaba.cloud 67 | spring-cloud-alibaba-dependencies 68 | ${spring.cloud.alibaba.version} 69 | pom 70 | import 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | org.springframework.boot 79 | spring-boot-maven-plugin 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /Finchley/spring-boot-admin-client-example/src/main/java/com/mtcarpenter/admin/client/example/SpringBootAdminClientExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.admin.client.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootAdminClientExampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootAdminClientExampleApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/spring-boot-admin-client-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8096 3 | spring: 4 | application: 5 | name: admin-client 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: 127.0.0.1:8848 10 | boot: 11 | admin: 12 | client: 13 | # admin 服务端的地址 14 | url: http://localhost:8095 15 | 16 | management: 17 | endpoints: 18 | web: 19 | exposure: 20 | include: '*' 21 | endpoint: 22 | health: 23 | # 展示详情 24 | show-details: always -------------------------------------------------------------------------------- /Finchley/spring-boot-admin-client-example/src/test/java/com/mtcarpenter/admin/client/example/SpringBootAdminClientExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.admin.client.example; 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 SpringBootAdminClientExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/spring-boot-admin-server-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | spring-boot-admin-server-example 13 | 0.0.1-SNAPSHOT 14 | spring-boot-admin-server-example 15 | 16 | 17 | 1.8 18 | 2.1.2.RELEASE 19 | Greenwich.RELEASE 20 | 2.1.6 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter 27 | 28 | 29 | 30 | com.alibaba.cloud 31 | spring-cloud-starter-alibaba-nacos-discovery 32 | 33 | 34 | 35 | de.codecentric 36 | spring-boot-admin-starter-server 37 | ${spring.boot.admin.version} 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-web 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-security 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-test 51 | test 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.springframework.cloud 59 | spring-cloud-dependencies 60 | ${spring.cloud.version} 61 | pom 62 | import 63 | 64 | 65 | com.alibaba.cloud 66 | spring-cloud-alibaba-dependencies 67 | ${spring.cloud.alibaba.version} 68 | pom 69 | import 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | org.springframework.boot 79 | spring-boot-maven-plugin 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /Finchley/spring-boot-admin-server-example/src/main/java/com/mtcarpenter/admin/server/example/SecuritySecureConfig.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.admin.server.example; 2 | 3 | 4 | import de.codecentric.boot.admin.server.config.AdminServerProperties; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 7 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 8 | import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; 9 | 10 | /** 11 | * @author mtcarpenter 12 | * @github https://github.com/mtcarpenter/spring-cloud-learning 13 | * @desc 微信公众号:山间木匠 14 | */ 15 | @Configuration 16 | public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { 17 | 18 | private final String adminContextPath; 19 | 20 | public SecuritySecureConfig(AdminServerProperties adminServerProperties) { 21 | this.adminContextPath = adminServerProperties.getContextPath(); 22 | } 23 | 24 | @Override 25 | protected void configure(HttpSecurity http) throws Exception { 26 | // @formatter:off 27 | SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); 28 | successHandler.setTargetUrlParameter( "redirectTo" ); 29 | 30 | http.authorizeRequests() 31 | // 配置签名后放行路径 32 | .antMatchers( adminContextPath + "/assets/**" ).permitAll() 33 | .antMatchers( adminContextPath + "/login" ).permitAll() 34 | .anyRequest().authenticated() 35 | .and() 36 | .formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and() 37 | .logout().logoutUrl( adminContextPath + "/logout" ).and() 38 | .httpBasic().and() 39 | .csrf().disable(); 40 | } 41 | } -------------------------------------------------------------------------------- /Finchley/spring-boot-admin-server-example/src/main/java/com/mtcarpenter/admin/server/example/SpringBootAdminServerExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.admin.server.example; 2 | 3 | import de.codecentric.boot.admin.server.config.EnableAdminServer; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication 8 | @EnableAdminServer 9 | public class SpringBootAdminServerExampleApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootAdminServerExampleApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Finchley/spring-boot-admin-server-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8095 3 | spring: 4 | application: 5 | name: admin-server 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: 127.0.0.1:8848 10 | security: 11 | user: 12 | name: admin 13 | password: admin -------------------------------------------------------------------------------- /Finchley/spring-boot-admin-server-example/src/test/java/com/mtcarpenter/admin/server/example/SpringBootAdminServerExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.admin.server.example; 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 SpringBootAdminServerExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/consumer-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-dubbo-zookeeper-example 7 | com.mtcarpenter 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | consumer-example 13 | 14 | 15 | 16 | com.mtcarpenter 17 | provider-interface-example 18 | 0.0.1-SNAPSHOT 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/consumer-example/src/main/java/com/mtcarpenter/spring/boot/dubbo/zookeeper/ConsumerHelloServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.boot.dubbo.zookeeper; 2 | 3 | import org.apache.dubbo.config.annotation.DubboReference; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | * @author mtcarpenter 8 | * @github https://github.com/mtcarpenter/mall-cloud-alibaba 9 | * @desc 微信公众号:山间木匠 10 | */ 11 | @Service 12 | public class ConsumerHelloServiceImpl { 13 | 14 | @DubboReference(version = "1.0.0") 15 | private HelloService helloService; 16 | 17 | public String sayHello(String username){ 18 | return helloService.sayHello(username); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/consumer-example/src/main/java/com/mtcarpenter/spring/boot/dubbo/zookeeper/DubboConsumerExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.boot.dubbo.zookeeper; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | 11 | /** 12 | * @author mtcarpenter 13 | * @github https://github.com/mtcarpenter/spring-cloud-learning 14 | * @desc 微信公众号:山间木匠 15 | */ 16 | @SpringBootApplication 17 | @RestController 18 | public class DubboConsumerExampleApplication { 19 | 20 | @Autowired 21 | private ConsumerHelloServiceImpl consumerHelloService; 22 | 23 | @GetMapping("/consumer/{name}") 24 | public String say(@PathVariable("name") String name) { 25 | return consumerHelloService.sayHello(name); 26 | } 27 | 28 | public static void main(String[] args) { 29 | SpringApplication.run(DubboConsumerExampleApplication.class, args); 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/consumer-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8082 2 | dubbo.application.name=dubbo-consumer 3 | dubbo.registry.address=zookeeper://127.0.0.1:2181 4 | -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | pom 6 | 7 | provider-example 8 | provider-interface-example 9 | consumer-example 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 2.1.15.RELEASE 15 | 16 | 17 | com.mtcarpenter 18 | spring-boot-dubbo-zookeeper-example 19 | 0.0.1-SNAPSHOT 20 | spring-boot-dubbo-zookeeper-example 21 | 22 | 23 | 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/provider-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-dubbo-zookeeper-example 7 | com.mtcarpenter 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | provider-example 13 | 14 | 15 | 16 | 17 | com.mtcarpenter 18 | provider-interface-example 19 | 0.0.1-SNAPSHOT 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/provider-example/src/main/java/com/mtcarpenter/spring/boot/dubbo/zookeeper/DubboProviderExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.boot.dubbo.zookeeper; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * @author mtcarpenter 8 | * @github https://github.com/mtcarpenter/spring-cloud-learning 9 | * @desc 微信公众号:山间木匠 10 | */ 11 | @SpringBootApplication 12 | public class DubboProviderExampleApplication { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(DubboProviderExampleApplication.class, args); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/provider-example/src/main/java/com/mtcarpenter/spring/boot/dubbo/zookeeper/HelloServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.boot.dubbo.zookeeper; 2 | 3 | import org.apache.dubbo.config.annotation.DubboService; 4 | 5 | /** 6 | * @author mtcarpenter 7 | * @github https://github.com/mtcarpenter/mall-cloud-alibaba 8 | * @desc 微信公众号:山间木匠 9 | */ 10 | 11 | @DubboService(version = "1.0.0") 12 | public class HelloServiceImpl implements HelloService { 13 | @Override 14 | public String sayHello(String username) { 15 | return username +" say hello provider"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/provider-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 2 | # 1、指定当前服务名称(同服务名称可相同 3 | dubbo.application.name=dubbo-provider 4 | # 2 注册中心配置,使用zookeeper注册中心暴露服务地址 5 | dubbo.registry.address=zookeeper://127.0.0.1:2181 6 | #dubbo.registry.protocol=zookeeper 7 | #dubbo.registry.address=127.0.0.1:2181 8 | 9 | # 3 Dubbo Protocol 协议 10 | dubbo.protocol.name=dubbo 11 | # -1 则会分配一个没有被占用的端口 dubbo 协议缺省端口为 20880 12 | dubbo.protocol.port=20880 13 | 14 | dubbo.scan.base-packages=com.mtcarpenter.spring.boot.dubbo.zookeeper -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/provider-interface-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-dubbo-zookeeper-example 7 | com.mtcarpenter 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | provider-interface-example 13 | 14 | 15 | 16 | 2.1.15.RELEASE 17 | 2.7.7 18 | 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | 27 | org.apache.dubbo 28 | dubbo-spring-boot-starter 29 | ${dubbo.version} 30 | 31 | 32 | 33 | org.apache.dubbo 34 | dubbo-dependencies-zookeeper 35 | 2.7.7 36 | pom 37 | 38 | 39 | org.slf4j 40 | slf4j-log4j12 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-dependencies 53 | ${spring-boot.version} 54 | pom 55 | import 56 | 57 | 58 | 59 | 60 | org.apache.dubbo 61 | dubbo-dependencies-bom 62 | ${dubbo.version} 63 | pom 64 | import 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /Finchley/spring-boot-dubbo-zookeeper-example/provider-interface-example/src/main/java/com/mtcarpenter/spring/boot/dubbo/zookeeper/HelloService.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.boot.dubbo.zookeeper; 2 | 3 | /** 4 | * @author mtcarpenter 5 | * @github https://github.com/mtcarpenter/mall-cloud-alibaba 6 | * @desc 微信公众号:山间木匠 7 | */ 8 | public interface HelloService { 9 | 10 | String sayHello(String username); 11 | } 12 | -------------------------------------------------------------------------------- /Finchley/spring-boot-rabbitmq-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | spring-boot-rabbitmq-example 13 | 0.0.1-SNAPSHOT 14 | spring-boot-rabbitmq-example 15 | 16 | 17 | 1.8 18 | 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-amqp 28 | 29 | 30 | org.projectlombok 31 | lombok 32 | 33 | 34 | com.fasterxml.jackson.core 35 | jackson-databind 36 | 2.9.10.7 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-web 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Finchley/spring-boot-rabbitmq-example/src/main/java/com/mtcarpenter/rabbitmq/example/Book.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.rabbitmq.example; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | * @author mtcarpenter 11 | * @github https://github.com/mtcarpenter/spring-cloud-learning 12 | * @desc 微信公众号:山间木匠 13 | */ 14 | @Data 15 | @NoArgsConstructor 16 | @AllArgsConstructor 17 | public class Book implements Serializable { 18 | 19 | private static final long serialVersionUID = -2647663884508887444L; 20 | private int id; 21 | private String name; 22 | private String author; 23 | 24 | } -------------------------------------------------------------------------------- /Finchley/spring-boot-rabbitmq-example/src/main/java/com/mtcarpenter/rabbitmq/example/RabbitConfig.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.rabbitmq.example; 2 | 3 | 4 | import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; 5 | import org.springframework.amqp.support.converter.MessageConverter; 6 | import org.springframework.context.annotation.Bean; 7 | 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | /** 11 | * @author mtcarpenter 12 | * @github https://github.com/mtcarpenter/spring-cloud-learning 13 | * @desc 微信公众号:山间木匠 14 | */ 15 | @Configuration 16 | public class RabbitConfig { 17 | 18 | @Bean 19 | public MessageConverter messageConverter(){ 20 | return new Jackson2JsonMessageConverter(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Finchley/spring-boot-rabbitmq-example/src/main/java/com/mtcarpenter/rabbitmq/example/SpringBootRabbitmqExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.rabbitmq.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootRabbitmqExampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootRabbitmqExampleApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/spring-boot-rabbitmq-example/src/main/java/com/mtcarpenter/rabbitmq/example/direct/DirectConfig.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.rabbitmq.example.direct; 2 | 3 | import org.springframework.amqp.core.Queue; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * 9 | * @author mtcarpenter 10 | * @github https://github.com/mtcarpenter/spring-cloud-learning 11 | * @desc 微信公众号:山间木匠 12 | */ 13 | @Configuration 14 | public class DirectConfig { 15 | 16 | @Bean 17 | public Queue queue(){ 18 | return new Queue("direct.queue",true); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /Finchley/spring-boot-rabbitmq-example/src/main/java/com/mtcarpenter/rabbitmq/example/direct/DirectReceiver.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.rabbitmq.example.direct; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.amqp.core.Message; 5 | import org.springframework.amqp.rabbit.annotation.Queue; 6 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 7 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * 12 | * @author mtcarpenter 13 | * @github https://github.com/mtcarpenter/spring-cloud-learning 14 | * @desc 微信公众号:山间木匠 15 | */ 16 | @Component 17 | @Slf4j 18 | public class DirectReceiver { 19 | 20 | /** 21 | * 简单直连消息接收 22 | * @param message 23 | */ 24 | @RabbitListener(queuesToDeclare=@Queue("direct.queue")) 25 | @RabbitHandler 26 | public void message(Message message) { 27 | log.info("message result = {}",message); 28 | } 29 | 30 | 31 | } -------------------------------------------------------------------------------- /Finchley/spring-boot-rabbitmq-example/src/main/java/com/mtcarpenter/rabbitmq/example/topic/TopicReceiver.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.rabbitmq.example.topic; 2 | 3 | import com.mtcarpenter.rabbitmq.example.Book; 4 | import com.rabbitmq.client.Channel; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.amqp.core.Message; 7 | import org.springframework.amqp.rabbit.annotation.*; 8 | import org.springframework.amqp.support.AmqpHeaders; 9 | import org.springframework.messaging.handler.annotation.Headers; 10 | import org.springframework.messaging.handler.annotation.Payload; 11 | import org.springframework.stereotype.Component; 12 | 13 | import java.util.Map; 14 | 15 | /** 16 | * 17 | * @author mtcarpenter 18 | * @github https://github.com/mtcarpenter/spring-cloud-learning 19 | * @desc 微信公众号:山间木匠 20 | */ 21 | @Component 22 | @Slf4j 23 | public class TopicReceiver { 24 | 25 | 26 | @RabbitListener(bindings = @QueueBinding( 27 | value = @Queue(value = "magazineOrder",durable = "true"), 28 | exchange = @Exchange(value = "order",type = "topic"), 29 | key = "magazine" 30 | )) 31 | @RabbitHandler 32 | public void magazineMessage(Message message) { 33 | log.info("magazine result = {}",message); 34 | } 35 | 36 | @RabbitListener(bindings = @QueueBinding( 37 | value = @Queue(value = "historyOrder",durable = "true"), 38 | exchange = @Exchange(value = "order",type = "topic"), 39 | key = "history" 40 | )) 41 | @RabbitHandler 42 | public void historyMessage(Message message) { 43 | log.info("history result = {}",message); 44 | } 45 | 46 | 47 | @RabbitListener(bindings = @QueueBinding( 48 | value = @Queue(value = "encyclopediaOrder",durable = "true"), 49 | exchange = @Exchange(value = "order",type = "topic", 50 | ignoreDeclarationExceptions = "true"), 51 | key = "encyclopedia" 52 | )) 53 | @RabbitHandler 54 | public void onOrderMessage(@Payload Book book, 55 | Channel channel, 56 | @Headers Map headers) throws Exception { 57 | log.info("book id = {}" + book.getId()); 58 | Long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG); 59 | //手工ACK 60 | channel.basicAck(deliveryTag, false); 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /Finchley/spring-boot-rabbitmq-example/src/main/java/com/mtcarpenter/rabbitmq/example/topic/TopicSender.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.rabbitmq.example.topic; 2 | 3 | import com.mtcarpenter.rabbitmq.example.Book; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.amqp.core.Message; 6 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * @author mtcarpenter 12 | * @github https://github.com/mtcarpenter/spring-cloud-learning 13 | * @desc 微信公众号:山间木匠 14 | */ 15 | @Component 16 | @Slf4j 17 | public class TopicSender implements RabbitTemplate.ReturnCallback { 18 | 19 | @Autowired 20 | private RabbitTemplate rabbitTemplate; 21 | 22 | 23 | /** 24 | * 发送消息方法调用: 构建Message消息 25 | * @param book 26 | * @throws Exception 27 | */ 28 | public void send(Book book) throws Exception { 29 | //设置消息退回后的回调处理机制 30 | this.rabbitTemplate.setReturnCallback(this); 31 | // CorrelationData是一个当发送原始消息时,由客户机提供的对象。 32 | // ack是一个boolean值,当ack(确认)的时候,值为true;当nack(不确认)的时候,值为false 33 | // 经测试发现,只要 rabbitTemplate.convertAndSend() 能正确找到exchange,无论是否能将消息路由到正确的queue,ack值都为true 34 | // 只有当rabbitTemplate.convertAndSend()无法找到exchange时,ack 值才为false 35 | // cause是附加原因,比如说当nack的时候附加的原因 36 | this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> { 37 | if (!ack) { 38 | log.error("消息发送失败 cause = {} ,correlationData={} ", cause, correlationData.toString()); 39 | } else { 40 | log.info("消息发送成功 "); 41 | } 42 | }); 43 | this.rabbitTemplate.convertAndSend("order", "encyclopedia", book); 44 | } 45 | 46 | 47 | @Override 48 | public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { 49 | log.info("return message={} , exchange = {} , routingKey = {},replyCode={}, replyText{}", 50 | message, exchange, routingKey, replyCode, replyText); 51 | 52 | } 53 | } -------------------------------------------------------------------------------- /Finchley/spring-boot-rabbitmq-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | application: 5 | name: rabbitmq-example 6 | rabbitmq: 7 | host: localhost 8 | port: 5672 9 | username: guest 10 | password: guest 11 | virtual-host: / 12 | # 连接超时 13 | connection-timeout: 1500 14 | # 开启发送确认 15 | publisher-confirms: true 16 | # 开启发送失败退回 17 | publisher-returns: true 18 | template: 19 | mandatory: true 20 | # 开启ACK 21 | listener: 22 | direct: 23 | acknowledge-mode: manual 24 | simple: 25 | acknowledge-mode: manual 26 | max-concurrency: 10 27 | concurrency: 5 28 | -------------------------------------------------------------------------------- /Finchley/spring-boot-rabbitmq-example/src/test/java/com/mtcarpenter/rabbitmq/example/SpringBootRabbitmqExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.rabbitmq.example; 2 | 3 | import com.mtcarpenter.rabbitmq.example.topic.TopicSender; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.amqp.core.AmqpTemplate; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.test.context.junit4.SpringRunner; 10 | 11 | @RunWith(SpringRunner.class) 12 | @SpringBootTest 13 | public class SpringBootRabbitmqExampleApplicationTests { 14 | @Autowired 15 | private AmqpTemplate amqpTemplate; 16 | 17 | @Test 18 | public void directReceiver() { 19 | Book book = new Book(1,"spring cloud","mtcarpenter"); 20 | amqpTemplate.convertAndSend("direct.queue",book); 21 | } 22 | 23 | @Test 24 | public void topicReceiver(){ 25 | Book magazine = new Book(1,"杂志图书","mtcarpenter1"); 26 | amqpTemplate.convertAndSend("order","magazine",magazine); 27 | Book history = new Book(2,"历史图书","mtcarpenter2"); 28 | amqpTemplate.convertAndSend("order","history",history); 29 | } 30 | 31 | @Autowired 32 | private TopicSender topicSender; 33 | 34 | @Test 35 | public void topicSender() throws Exception { 36 | topicSender.send(new Book(3,"百科图书","mtcarpenter")); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-basic-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | spring-cloud-feign-basic-example 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-feign-basic-example 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.2.RELEASE 20 | Greenwich.RELEASE 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | com.alibaba.cloud 35 | spring-cloud-starter-alibaba-nacos-discovery 36 | 37 | 38 | 39 | org.springframework.cloud 40 | spring-cloud-starter-openfeign 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | 50 | io.github.openfeign 51 | feign-httpclient 52 | 53 | 54 | 55 | io.github.openfeign 56 | feign-okhttp 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.springframework.cloud 65 | spring-cloud-dependencies 66 | ${spring.cloud.version} 67 | pom 68 | import 69 | 70 | 71 | com.alibaba.cloud 72 | spring-cloud-alibaba-dependencies 73 | ${spring.cloud.alibaba.version} 74 | pom 75 | import 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | org.springframework.boot 86 | spring-boot-maven-plugin 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-basic-example/src/main/java/com/mtcarpenter/spring/cloud/feign/basic/example/SpringCloudFeignBasicExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.feign.basic.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.openfeign.EnableFeignClients; 6 | 7 | @SpringBootApplication 8 | @EnableFeignClients 9 | public class SpringCloudFeignBasicExampleApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringCloudFeignBasicExampleApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-basic-example/src/main/java/com/mtcarpenter/spring/cloud/feign/basic/example/config/FeignServerConfig.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.feign.basic.example.config; 2 | 3 | import feign.Logger; 4 | import org.springframework.context.annotation.Bean; 5 | 6 | 7 | /** 8 | * @author mtcarpenter 9 | * @github https://github.com/mtcarpenter/spring-cloud-learning 10 | * @desc 微信公众号:山间木匠 11 | */ 12 | //如果使用添加了 @Configuration 需要将此配置类移到启动类之外的包(避免@ComponentScan) 避免父子上下文重复扫描 13 | public class FeignServerConfig { 14 | 15 | @Bean 16 | public Logger.Level logger() { 17 | // 请求的详细信息 18 | return Logger.Level.FULL; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-basic-example/src/main/java/com/mtcarpenter/spring/cloud/feign/basic/example/config/OkHttpConfig.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.feign.basic.example.config; 2 | 3 | import feign.Feign; 4 | import feign.codec.Encoder; 5 | import feign.form.FormEncoder; 6 | import okhttp3.ConnectionPool; 7 | import org.springframework.boot.autoconfigure.AutoConfigureBefore; 8 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 9 | import org.springframework.cloud.openfeign.FeignAutoConfiguration; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | 13 | import java.util.concurrent.TimeUnit; 14 | 15 | /** 16 | * @author mtcarpenter 17 | * @github https://github.com/mtcarpenter/spring-cloud-learning 18 | * @desc 微信公众号:山间木匠 19 | */ 20 | 21 | @Configuration 22 | @ConditionalOnClass(Feign.class) 23 | @AutoConfigureBefore(FeignAutoConfiguration.class) //SpringBoot自动配置 24 | public class OkHttpConfig { 25 | 26 | /** 27 | * 中文乱码 28 | * 29 | * @return 30 | */ 31 | @Bean 32 | public Encoder encoder() { 33 | return new FormEncoder(); 34 | } 35 | 36 | /** 37 | * 配置 okhttp 与连接池 38 | * ConnectionPool 默认创建5个线程,保持5分钟长连接 39 | */ 40 | @Bean 41 | public okhttp3.OkHttpClient okHttpClient() { 42 | return new okhttp3.OkHttpClient.Builder() 43 | //设置连接超时 44 | .connectTimeout(10, TimeUnit.SECONDS) 45 | //设置读超时 46 | .readTimeout(10, TimeUnit.SECONDS) 47 | //设置写超时 48 | .writeTimeout(10, TimeUnit.SECONDS) 49 | //是否自动重连 50 | .retryOnConnectionFailure(true) 51 | .connectionPool(new ConnectionPool(10, 5L, TimeUnit.MINUTES)) 52 | .build(); 53 | } 54 | } -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-basic-example/src/main/java/com/mtcarpenter/spring/cloud/feign/basic/example/controller/BasicController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.feign.basic.example.controller; 2 | 3 | import com.mtcarpenter.spring.cloud.feign.basic.example.feign.FeignServerClient; 4 | import com.mtcarpenter.spring.cloud.feign.basic.example.feign.FeignUrlClient; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | /** 12 | * @author mtcarpenter 13 | * @github https://github.com/mtcarpenter/spring-cloud-learning 14 | * @desc 微信公众号:山间木匠 15 | */ 16 | 17 | @RestController 18 | @RequestMapping("/basic") 19 | public class BasicController { 20 | 21 | @Autowired 22 | private FeignServerClient feignServerClient; 23 | 24 | @Autowired 25 | private FeignUrlClient feignUrlClient; 26 | 27 | @GetMapping("/say/{name}") 28 | public String sayHello(@PathVariable("name") String name) { 29 | return feignServerClient.sayHello(name); 30 | } 31 | 32 | @GetMapping("/cloud") 33 | public String cloud() { 34 | return feignUrlClient.cloud(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-basic-example/src/main/java/com/mtcarpenter/spring/cloud/feign/basic/example/feign/FeignServerClient.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.feign.basic.example.feign; 2 | 3 | import com.mtcarpenter.spring.cloud.feign.basic.example.config.FeignServerConfig; 4 | import org.springframework.cloud.openfeign.FeignClient; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | 8 | /** 9 | * @author mtcarpenter 10 | * @github https://github.com/mtcarpenter/spring-cloud-learning 11 | * @desc 微信公众号:山间木匠 12 | */ 13 | 14 | //@FeignClient(name="feign-server",path = "feign",configuration = FeignServerConfig.class) 15 | @FeignClient(name = "feign-server", path = "feign") 16 | public interface FeignServerClient { 17 | /** 18 | * 请求服务端接口 http://feign-server/feign/say/{name} 19 | * 20 | * @param name 21 | * @return 22 | */ 23 | @GetMapping("/say/{name}") 24 | String sayHello(@PathVariable("name") String name); 25 | } 26 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-basic-example/src/main/java/com/mtcarpenter/spring/cloud/feign/basic/example/feign/FeignUrlClient.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.feign.basic.example.feign; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | 6 | /** 7 | * @author mtcarpenter 8 | * @github https://github.com/mtcarpenter/spring-cloud-learning 9 | * @desc 微信公众号:山间木匠 10 | */ 11 | @FeignClient(name = "url-client", url = "https://blog.lixc.top/spring-cloud.html") 12 | public interface FeignUrlClient { 13 | 14 | /** 15 | * 地址请求 16 | * 17 | * @return 18 | */ 19 | @GetMapping("") 20 | String cloud(); 21 | } 22 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-basic-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8081 3 | spring: 4 | application: 5 | name: feign-basic 6 | cloud: 7 | nacos: 8 | server-addr: 127.0.0.1:8848 9 | logging: 10 | level: 11 | com.mtcarpenter.spring.cloud.feign.basic.example.feign.FeignServerClient: debug 12 | # feign 日志级别属性配置 13 | feign: 14 | client: 15 | config: 16 | # 微服务实例名称 17 | feign-server: 18 | loggerLevel: FULL 19 | # 启用 okhttp 关闭默认 httpclient 20 | httpclient: 21 | enabled: false #关闭httpclient 22 | # 配置连接池 23 | max-connections: 200 #feign的最大连接数 24 | max-connections-per-route: 50 #fegin单个路径的最大连接数 25 | okhttp: 26 | enabled: true 27 | # 请求与响应的压缩以提高通信效率 28 | compression: 29 | request: 30 | enabled: true 31 | min-request-size: 2048 32 | mime-types: text/xml,application/xml,application/json 33 | response: 34 | enabled: true 35 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-basic-example/src/test/java/com/mtcarpenter/spring/cloud/feign/basic/example/SpringCloudFeignBasicExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.feign.basic.example; 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 SpringCloudFeignBasicExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-server-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | spring-cloud-feign-server-example 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-feign-server-example 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.2.RELEASE 20 | Greenwich.RELEASE 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter 27 | 28 | 29 | 30 | com.alibaba.cloud 31 | spring-cloud-starter-alibaba-nacos-discovery 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-dependencies 50 | ${spring.cloud.version} 51 | pom 52 | import 53 | 54 | 55 | com.alibaba.cloud 56 | spring-cloud-alibaba-dependencies 57 | ${spring.cloud.alibaba.version} 58 | pom 59 | import 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | org.springframework.boot 69 | spring-boot-maven-plugin 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-server-example/src/main/java/com/mtcarpenter/spring/cloud/feign/server/example/FeignServerController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.feign.server.example; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.PathVariable; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @author mtcarpenter 10 | * @github https://github.com/mtcarpenter/spring-cloud-learning 11 | * @desc 微信公众号:山间木匠 12 | */ 13 | @RestController 14 | @RequestMapping("/feign") 15 | public class FeignServerController { 16 | 17 | @GetMapping("/say/{name}") 18 | public String sayHello(@PathVariable("name") String name) { 19 | return name + " say hello"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-server-example/src/main/java/com/mtcarpenter/spring/cloud/feign/server/example/SpringCloudFeignServerExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.feign.server.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringCloudFeignServerExampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringCloudFeignServerExampleApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-server-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | application: 5 | name: feign-server 6 | cloud: 7 | nacos: 8 | server-addr: 127.0.0.1:8848 -------------------------------------------------------------------------------- /Finchley/spring-cloud-feign-server-example/src/test/java/com/mtcarpenter/spring/cloud/feign/server/example/SpringCloudFeignServerExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.feign.server.example; 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 SpringCloudFeignServerExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-resttemplate-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | spring-cloud-ribbon-resttemplate-example 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-ribbon-resttemplate-example 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.2.RELEASE 20 | Greenwich.RELEASE 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter 27 | 28 | 29 | 30 | com.alibaba.cloud 31 | spring-cloud-starter-alibaba-nacos-discovery 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | org.projectlombok 39 | lombok 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.cloud 53 | spring-cloud-dependencies 54 | ${spring.cloud.version} 55 | pom 56 | import 57 | 58 | 59 | com.alibaba.cloud 60 | spring-cloud-alibaba-dependencies 61 | ${spring.cloud.alibaba.version} 62 | pom 63 | import 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | org.springframework.boot 73 | spring-boot-maven-plugin 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-resttemplate-example/src/main/java/com/mtcarpenter/ribbon/RibbonConfig.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.ribbon; 2 | 3 | import com.netflix.loadbalancer.IPing; 4 | import com.netflix.loadbalancer.IRule; 5 | import com.netflix.loadbalancer.PingUrl; 6 | import com.netflix.loadbalancer.RandomRule; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | /** 11 | * @author mtcarpenter 12 | * @github https://github.com/mtcarpenter/spring-cloud-learning 13 | * @desc 微信公众号:山间木匠 14 | */ 15 | @Configuration 16 | public class RibbonConfig { 17 | 18 | @Bean 19 | public IRule iRule(){ 20 | return new RandomRule(); 21 | } 22 | 23 | @Bean 24 | public IPing ribbonPing() { 25 | return new PingUrl(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-resttemplate-example/src/main/java/com/mtcarpenter/spring/cloud/ribbon/resttemplate/example/SpringCloudRibbonResttemplateExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.ribbon.resttemplate.example; 2 | 3 | import com.mtcarpenter.ribbon.RibbonConfig; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.netflix.ribbon.RibbonClient; 7 | import org.springframework.cloud.netflix.ribbon.RibbonClients; 8 | 9 | @SpringBootApplication 10 | // 针对某一个服务 11 | //@RibbonClient(name = "ribbon-server",configuration = RibbonConfig.class) 12 | // 全局配置 13 | //@RibbonClients(defaultConfiguration = RibbonConfig.class ) 14 | public class SpringCloudRibbonResttemplateExampleApplication { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(SpringCloudRibbonResttemplateExampleApplication.class, args); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-resttemplate-example/src/main/java/com/mtcarpenter/spring/cloud/ribbon/resttemplate/example/TestController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.ribbon.resttemplate.example; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.apache.commons.logging.Log; 5 | import org.apache.commons.logging.LogFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.cloud.client.ServiceInstance; 8 | import org.springframework.cloud.client.discovery.DiscoveryClient; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | import org.springframework.web.client.RestTemplate; 13 | 14 | import java.util.List; 15 | import java.util.concurrent.ThreadLocalRandom; 16 | import java.util.stream.Collectors; 17 | 18 | /** 19 | * @author mtcarpenter 20 | * @github https://github.com/mtcarpenter/spring-cloud-learning 21 | * @desc 微信公众号:山间木匠 22 | */ 23 | @RestController 24 | @RequestMapping("/test") 25 | public class TestController { 26 | private static final Log log = LogFactory.getLog(TestController.class); 27 | 28 | @Autowired 29 | private DiscoveryClient discoveryClient; 30 | 31 | @Autowired 32 | private RestTemplate restTemplate; 33 | 34 | @GetMapping("/instances") 35 | public List instances(){ 36 | List instances = discoveryClient.getInstances("ribbon-server"); 37 | return instances; 38 | } 39 | 40 | @GetMapping("/randomServerUrl") 41 | public String randomServerUrl(){ 42 | List instances = discoveryClient.getInstances("ribbon-server"); 43 | List urls = instances.stream().map(s->s.getUri().toString()).collect(Collectors.toList()); 44 | // 随机获取 45 | int i = ThreadLocalRandom.current().nextInt(urls.size()); 46 | String requestUrl = urls.get(i).concat("/user/sayHello"); 47 | log.info("request url = "+requestUrl); 48 | String result = restTemplate.getForObject(requestUrl, String.class); 49 | return result; 50 | } 51 | 52 | @GetMapping("/serviceId") 53 | public String serviceId(){ 54 | String result = restTemplate.getForObject("http://ribbon-server/user/sayHello", String.class); 55 | return result; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-resttemplate-example/src/main/java/com/mtcarpenter/spring/cloud/ribbon/resttemplate/example/config/RestTemplateConfig.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.ribbon.resttemplate.example.config; 2 | 3 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | /** 9 | * @author mtcarpenter 10 | * @github https://github.com/mtcarpenter/spring-cloud-learning 11 | * @desc 微信公众号:山间木匠 12 | */ 13 | @Configuration 14 | public class RestTemplateConfig { 15 | 16 | @Bean 17 | @LoadBalanced 18 | public RestTemplate restTemplate(){ 19 | return new RestTemplate(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-resttemplate-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8083 3 | spring: 4 | cloud: 5 | nacos: 6 | discovery: 7 | server-addr: 127.0.0.1:8848 8 | application: 9 | name: ribbon-rest 10 | # 针对 ribbon-server ribbon配置 11 | ribbon-server: 12 | ribbon: 13 | # 加载规则 14 | NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule 15 | # 饥饿加载 16 | ribbon: 17 | eager-load: 18 | enabled: true 19 | # 加载列表 多个 使用逗号隔开 20 | clients: ribbon-server -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-resttemplate-example/src/test/java/com/mtcarpenter/spring/cloud/ribbon/resttemplate/example/SpringCloudRibbonResttemplateExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.ribbon.resttemplate.example; 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 SpringCloudRibbonResttemplateExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-server-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | spring-cloud-ribbon-server-example 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-ribbon-server-example 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.2.RELEASE 20 | Greenwich.RELEASE 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter 27 | 28 | 29 | 30 | com.alibaba.cloud 31 | spring-cloud-starter-alibaba-nacos-discovery 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-dependencies 50 | ${spring.cloud.version} 51 | pom 52 | import 53 | 54 | 55 | com.alibaba.cloud 56 | spring-cloud-alibaba-dependencies 57 | ${spring.cloud.alibaba.version} 58 | pom 59 | import 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | org.springframework.boot 69 | spring-boot-maven-plugin 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-server-example/src/main/java/com/mtcarpenter/spring/cloud/ribbon/server/example/SpringCloudRibbonServerExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.ribbon.server.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringCloudRibbonServerExampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringCloudRibbonServerExampleApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-server-example/src/main/java/com/mtcarpenter/spring/cloud/ribbon/server/example/UserController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.ribbon.server.example; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @author mtcarpenter 11 | * @github https://github.com/mtcarpenter/spring-cloud-learning 12 | * @desc 微信公众号:山间木匠 13 | */ 14 | @RestController 15 | @RequestMapping("/user") 16 | public class UserController { 17 | 18 | private static final Log log = LogFactory.getLog(UserController.class); 19 | 20 | @GetMapping("/sayHello") 21 | public String sayHello(){ 22 | log.info("say hello"); 23 | return "sayHello"; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-server-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8081 3 | spring: 4 | application: 5 | name: ribbon-server 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: 127.0.0.1:8848 -------------------------------------------------------------------------------- /Finchley/spring-cloud-ribbon-server-example/src/test/java/com/mtcarpenter/spring/cloud/ribbon/server/example/SpringCloudRibbonServerExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.ribbon.server.example; 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 SpringCloudRibbonServerExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-client-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | spring-cloud-sleuth-client-example 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-sleuth-client-example 15 | Demo project for Spring Boot 16 | 17 | 18 | 19 | 1.8 20 | 2.1.2.RELEASE 21 | Greenwich.RELEASE 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter 28 | 29 | 30 | 31 | com.alibaba.cloud 32 | spring-cloud-starter-alibaba-nacos-discovery 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-web 37 | 38 | 39 | org.springframework.cloud 40 | spring-cloud-starter-openfeign 41 | 42 | 43 | 44 | 45 | org.springframework.cloud 46 | spring-cloud-starter-zipkin 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-starter-test 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.springframework.cloud 60 | spring-cloud-dependencies 61 | ${spring.cloud.version} 62 | pom 63 | import 64 | 65 | 66 | com.alibaba.cloud 67 | spring-cloud-alibaba-dependencies 68 | ${spring.cloud.alibaba.version} 69 | pom 70 | import 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | org.springframework.boot 81 | spring-boot-maven-plugin 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-client-example/src/main/java/com/mtcarpenter/spring/cloud/sleuth/client/example/SleuthClientController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.sleuth.client.example; 2 | 3 | import com.mtcarpenter.spring.cloud.sleuth.client.example.feign.SleuthServerFeign; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @author mtcarpenter 11 | * @github https://github.com/mtcarpenter/spring-cloud-learning 12 | * @desc 微信公众号:山间木匠 13 | */ 14 | @RestController 15 | @RequestMapping("/client") 16 | public class SleuthClientController { 17 | @Autowired 18 | private SleuthServerFeign sleuthServerFeign; 19 | 20 | @GetMapping("/sayHello") 21 | public String sayHello() { 22 | return sleuthServerFeign.sayHello(); 23 | } 24 | 25 | @GetMapping("/error") 26 | public String error() { 27 | return sleuthServerFeign.error(); 28 | } 29 | } -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-client-example/src/main/java/com/mtcarpenter/spring/cloud/sleuth/client/example/SpringCloudSleuthClientExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.sleuth.client.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.openfeign.EnableFeignClients; 6 | 7 | @SpringBootApplication 8 | @EnableFeignClients 9 | public class SpringCloudSleuthClientExampleApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringCloudSleuthClientExampleApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-client-example/src/main/java/com/mtcarpenter/spring/cloud/sleuth/client/example/feign/SleuthServerFeign.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.sleuth.client.example.feign; 2 | 3 | 4 | import org.springframework.cloud.openfeign.FeignClient; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | 7 | /** 8 | * @author mtcarpenter 9 | * @github https://github.com/mtcarpenter/spring-cloud-learning 10 | * @desc 微信公众号:山间木匠 11 | */ 12 | @FeignClient(name = "sleuth-server", path = "server") 13 | public interface SleuthServerFeign { 14 | 15 | @GetMapping("/sayHello") 16 | String sayHello(); 17 | 18 | @GetMapping("/error") 19 | String error(); 20 | } -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-client-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8083 3 | spring: 4 | application: 5 | name: sleuth-client 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: 127.0.0.1:8848 10 | zipkin: 11 | base-url: http://localhost:9411 12 | # 支持通过服务发现来定位host name 13 | locator: 14 | discovery: 15 | enabled: true 16 | sleuth: 17 | sampler: 18 | # zipkin 抽样比例 在默认情况下,该值为0.1 19 | probability: 1.0 20 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-client-example/src/test/java/com/mtcarpenter/spring/cloud/sleuth/client/example/SpringCloudSleuthClientExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.sleuth.client.example; 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 SpringCloudSleuthClientExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-server-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | spring-cloud-sleuth-server-example 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-sleuth-server-example 15 | 16 | 17 | 1.8 18 | 2.1.2.RELEASE 19 | Greenwich.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | 29 | com.alibaba.cloud 30 | spring-cloud-starter-alibaba-nacos-discovery 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-starter-zipkin 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.cloud 57 | spring-cloud-dependencies 58 | ${spring.cloud.version} 59 | pom 60 | import 61 | 62 | 63 | com.alibaba.cloud 64 | spring-cloud-alibaba-dependencies 65 | ${spring.cloud.alibaba.version} 66 | pom 67 | import 68 | 69 | 70 | org.springframework.cloud 71 | spring-cloud-sleuth-dependencies 72 | ${spring.cloud.alibaba.version} 73 | pom 74 | import 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | org.springframework.boot 85 | spring-boot-maven-plugin 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-server-example/src/main/java/com/mtcarpenter/spring/cloud/sleuth/server/example/SleuthServerController.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.sleuth.server.example; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @author mtcarpenter 11 | * @github https://github.com/mtcarpenter/spring-cloud-learning 12 | * @desc 微信公众号:山间木匠 13 | */ 14 | @RestController 15 | @RequestMapping("/server") 16 | public class SleuthServerController { 17 | 18 | private static final Log log = LogFactory.getLog(SleuthServerController.class); 19 | 20 | @GetMapping("/sayHello") 21 | public String sayHello(){ 22 | log.info("say hello"); 23 | return "sayHello"; 24 | } 25 | 26 | @GetMapping("/error") 27 | public String error(){ 28 | throw new RuntimeException(); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-server-example/src/main/java/com/mtcarpenter/spring/cloud/sleuth/server/example/SpringCloudSleuthServerExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.sleuth.server.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringCloudSleuthServerExampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringCloudSleuthServerExampleApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-server-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | application: 5 | name: sleuth-server 6 | cloud: 7 | nacos: 8 | discovery: 9 | server-addr: 127.0.0.1:8848 10 | zipkin: 11 | base-url: http://localhost:9411 12 | # 支持通过服务发现来定位host name 13 | locator: 14 | discovery: 15 | enabled: true 16 | sleuth: 17 | sampler: 18 | # zipkin 抽样比例 在默认情况下,该值为0.1 19 | probability: 1.0 20 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-sleuth-server-example/src/test/java/com/mtcarpenter/spring/cloud/sleuth/server/example/SpringCloudSleuthServerExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.spring.cloud.sleuth.server.example; 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 SpringCloudSleuthServerExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-stream-rabbitmq-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.13.RELEASE 9 | 10 | 11 | com.mtcarpenter 12 | spring-cloud-stream-rabbitmq-example 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-stream-rabbitmq-example 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Greenwich.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-amqp 30 | 31 | 32 | org.springframework.cloud 33 | spring-cloud-starter-stream-rabbit 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | 41 | org.projectlombok 42 | lombok 43 | true 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.cloud 57 | spring-cloud-dependencies 58 | ${spring.cloud.version} 59 | pom 60 | import 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | org.springframework.boot 69 | spring-boot-maven-plugin 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-stream-rabbitmq-example/src/main/java/com/mtcarpenter/stream/rabbitmq/example/Book.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.stream.rabbitmq.example; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | * @author mtcarpenter 11 | * @github https://github.com/mtcarpenter/spring-cloud-learning 12 | * @desc 微信公众号:山间木匠 13 | */ 14 | @Data 15 | @NoArgsConstructor 16 | @AllArgsConstructor 17 | public class Book implements Serializable { 18 | 19 | 20 | private static final long serialVersionUID = 3679570604506079906L; 21 | private int id; 22 | private String name; 23 | private String author; 24 | 25 | } -------------------------------------------------------------------------------- /Finchley/spring-cloud-stream-rabbitmq-example/src/main/java/com/mtcarpenter/stream/rabbitmq/example/SpringCloudStreamRabbitmqExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.stream.rabbitmq.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringCloudStreamRabbitmqExampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringCloudStreamRabbitmqExampleApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Finchley/spring-cloud-stream-rabbitmq-example/src/main/java/com/mtcarpenter/stream/rabbitmq/example/StreamBinding.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.stream.rabbitmq.example; 2 | 3 | import org.springframework.cloud.stream.annotation.Input; 4 | import org.springframework.cloud.stream.annotation.Output; 5 | import org.springframework.messaging.MessageChannel; 6 | import org.springframework.messaging.SubscribableChannel; 7 | 8 | /** 9 | * @author mtcarpenter 10 | * @github https://github.com/mtcarpenter/spring-cloud-learning 11 | * @desc 微信公众号:山间木匠 12 | */ 13 | public interface StreamBinding { 14 | 15 | String INPUT = "input"; 16 | 17 | String OUTPUT = "output"; 18 | 19 | 20 | @Input(StreamBinding.INPUT) 21 | SubscribableChannel input(); 22 | 23 | @Output(StreamBinding.OUTPUT) 24 | MessageChannel output(); 25 | } -------------------------------------------------------------------------------- /Finchley/spring-cloud-stream-rabbitmq-example/src/main/java/com/mtcarpenter/stream/rabbitmq/example/StreamReceiver.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.stream.rabbitmq.example; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.cloud.stream.annotation.EnableBinding; 5 | import org.springframework.cloud.stream.annotation.StreamListener; 6 | import org.springframework.messaging.Message; 7 | import org.springframework.messaging.handler.annotation.SendTo; 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * @author mtcarpenter 12 | * @github https://github.com/mtcarpenter/spring-cloud-learning 13 | * @desc 微信公众号:山间木匠 14 | */ 15 | @Component 16 | @EnableBinding(StreamBinding.class) 17 | @Slf4j 18 | public class StreamReceiver { 19 | @StreamListener(StreamBinding.INPUT) 20 | public int inputMessage(Message message) { 21 | log.info("StreamReceiver:{}", message); 22 | return 0; 23 | } 24 | 25 | 26 | } -------------------------------------------------------------------------------- /Finchley/spring-cloud-stream-rabbitmq-example/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | application: 5 | name: stream-rabbitmq-example 6 | rabbitmq: 7 | host: localhost 8 | port: 5672 9 | username: guest 10 | password: guest 11 | cloud: 12 | stream: 13 | bindings: 14 | input: 15 | destination: stream-channel 16 | output: 17 | destination: stream-channel -------------------------------------------------------------------------------- /Finchley/spring-cloud-stream-rabbitmq-example/src/test/java/com/mtcarpenter/stream/rabbitmq/example/SpringCloudStreamRabbitmqExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mtcarpenter.stream.rabbitmq.example; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.messaging.Message; 8 | import org.springframework.messaging.MessageHeaders; 9 | import org.springframework.messaging.support.MessageBuilder; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | 15 | @RunWith(SpringRunner.class) 16 | @SpringBootTest 17 | public class SpringCloudStreamRabbitmqExampleApplicationTests { 18 | 19 | @Autowired 20 | private StreamBinding streamBinding; 21 | 22 | @Test 23 | public void contextLoads() { 24 | Map properties = new HashMap(); 25 | properties.put("book", new Book(3, "百科图书", "mtcarpenter")); 26 | MessageHeaders mhs = new MessageHeaders(properties); 27 | Message msg = MessageBuilder.createMessage("book order ", mhs); 28 | streamBinding.output().send(msg); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-cloud-learning 2 | Spring Cloud 微服务系列学习教程 3 | 4 | ## Spring Cloud Alibaba 微服务教程 5 | 6 | ### Spring Cloud Finchley 版本 7 | 8 | **分布式系统微服务注册中心与配置中心:Nacos** 9 | 10 | - [Spring Cloud Alibaba Nacos 系列教程(一):Nacos 的基本概述](https://blog.lixc.top/springcloud/2020/04/30/spring-cloud-nacos-introduce.html) 11 | 12 | - [Spring Cloud Alibaba Nacos 系列教程(一):Nacos 快速安装](http://blog.lixc.top/springcloud/2020/05/01/spring-cloud-nacos-setting.html) 13 | - [Spring Cloud Alibaba Nacos 系列教程(三):Nacos 服务注册发现(RestTemplate,Feign)](http://blog.lixc.top/springcloud/2020/05/02/spring-cloud-nacos-discovery.html) 14 | - [Spring Cloud Alibaba Nacos 系列教程(四):Nacos 作为配置中心](http://blog.lixc.top/springcloud/2020/05/03/spring-cloud-nacos-config.html) 15 | 16 | **分布式系统微服务流量防卫兵:Sentinel** 17 | 18 | - [Spring Cloud Alibaba Sentinel 系列教程(一):Sentinel 快速入门](https://blog.lixc.top/springcloud/2020/05/04/spring-cloud-sentinel-start.html) 19 | 20 | - [Spring Cloud Alibaba Sentinel 系列教程(一):Sentinel 流量控制](https://blog.lixc.top/springcloud/2020/05/05/spring-cloud-sentinel-flow.html) 21 | - [Spring Cloud Alibaba Sentinel 系列教程(三):Sentinel @SentinelResource 注解使用](https://blog.lixc.top/springcloud/2020/05/06/spring-cloud-sentinel-@SentinelResource.html) 22 | - [Spring Cloud Alibaba Sentinel 系列教程(四):Sentinel API 配置规则](https://blog.lixc.top/springcloud/2020/05/07/spring-cloud-sentinel-api.html) 23 | 24 | **分布式系统微服务网关:Gateway** 25 | 26 | - [Spring Cloud 服务网关系列教程(一):Spring Cloud Gateway 路由断言工厂](https://blog.lixc.top/springcloud/2020/05/12/spring-cloud-gateway-route.html) 27 | - [Spring Cloud 服务网关系列教程(二):Spring Cloud Gateway 过滤器工厂](https://blog.lixc.top/springcloud/2020/05/13/spring-cloud-gateway-filter.html) 28 | - [Spring Cloud 服务网关系列教程(三):Spring Cloud Gateway 端点监控(actuator)](https://blog.lixc.top/springcloud/2020/05/14/spring-cloud-gateway-actuator.html) 29 | 30 | **分布式系统微服务负载均衡:Ribbon,Feign** 31 | 32 | - [Spring Cloud 负载均衡系列教程(一):Spring Cloud Ribbon 基本使用](http://blog.lixc.top/springcloud/2020/05/17/spring-cloud-ribbon.html) 33 | 34 | - [Spring Cloud 负载均衡系列教程(二):Spring Cloud Feign 基本使用](https://blog.lixc.top/springcloud/2020/05/18/spring-cloud-feign-basic.html) 35 | 36 | **分布式系统微服务链路追踪:Sleuth,Zipkin** 37 | 38 | - [Spring Cloud 链路追踪系列教程(一):Spring Cloud Sleuth 整合 Zipkin](https://blog.lixc.top/springcloud/2020/06/05/spring-cloud-sleuth-zipkin.html) 39 | 40 | **分布式系统微服务端点监控:spring boot admin** 41 | 42 | - [Spring Cloud 端点监控系列教程(一):Spring Boot Admin 监控](https://blog.lixc.top/springcloud/2020/05/15/spring-boot-admin.html) 43 | 44 | **分布式系统微服务中间件:RabbitMQ** 45 | 46 | - [Spring Cloud 中间件系列教程(一):Spring Boot RbbitMQ](https://blog.lixc.top/springcloud/2020/06/08/spring-boot-rbbitmq.html) 47 | 48 | - [Spring Cloud 中间件系列教程(二):Spring Cloud Stream RbbitMQ](https://blog.lixc.top/springcloud/2020/06/10/spring-cloud-stream-rbbitmq.html) 49 | --------------------------------------------------------------------------------