Eureka 服务注册与发现
pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
application.yml
1 | spring: |
application-eureka1.yml
1 | eureka: |
application-eureka2.yml
1 | eureka: |
启动类EurekaApplication
1 | package com.imooc.eureka; |
Eureka控制台
应用模块改造 - product子项目
全局pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
server模块
pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
application.yml
1 | server: |
启动类 ProductApplication
1 | package com.imooc.product; |
common模块
pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
DecreaseStockInput.java
1 | package com.imooc.product.common; |
ProductInfoOutput.java
1 | package com.imooc.product.common; |
client模块
pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
ProductClient.java
1 | package com.imooc.product.client; |
应用模块改造 - order子项目
全局pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
common模块
pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
client模块
pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
server模块
pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
bootstrap.yml
1 | spring: |
启动类 OrderApplication.java
1 | package com.imooc.order; |
Config 配置中心
RabbitMQ
Docker镜像
1 | docker pull daocloud.io/library/rabbitmq:3.7-management |
RabbitMQ 控制台
http://localhost:15672/
guest/gutst
消息队列
pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
application.yml
1 | server: |
配置类 GirlConfig.java
1 | package com.imooc.order.config; |
GirlController.java
1 | package com.imooc.order.controller; |
启动类 ConfigApplication.java
1 | package com.imooc.config; |
热更新接口服务 - RefreshController.java
1 | package com.imooc.config.controller; |
order-test.yml (Github repo)
1 | eureka: |
手动测试配置刷新
1 | curl -v -X POST http://localhost/actuator/bus-refresh |
Github webhooks
测试Github的webhooks时,网上说是要配置到/monitor
这个地址上,实测发现会报错,这里重写了refresh服务的接口,采用get方式接收webhooks调用,然后再转成post方式去请求真实的配置刷新接口:http://kwang.natapp1.cc/refresh (natapp是个内网穿透工具)
测试配置热更新
修改Github上的yaml文件,无需重启任何服务,即可实现配置热刷新。
消息与异步
RabbitMQ
pom.xml
1 | <dependency> |
product-dev.yml
1 | spring: |
ProductServiceImpl.java
1 | package com.imooc.product.service.impl; |
ProductInfoReceiver.java
1 | package com.imooc.order.message; |
Spring Cloud Stream
pom.xml
1 | <dependency> |
bootstrap.yml
1 | spring: |
StreamClient.java
1 | package com.imooc.order.message; |
StreamReceiver.java
1 | package com.imooc.order.message; |
Zuul 服务网关
基本配置
pom.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
bootstrap.yml
1 | spring: |
api-gateway-dev.yml (config repo)
1 | zuul: |
热刷新 ZuulConfig.java
1 | package com.imooc.apigateway; |
启动类 ApiGatewayApplication.java
1 | package com.imooc.apigateway; |
过滤器
限流(令牌桶)
RateLimitFilter.java
1 | package com.imooc.apigateway.filter; |
设置http header
addResponseHeaderFilter.java
1 | package com.imooc.apigateway.filter; |
自定义条件拦截
TokenFilter.java
1 | package com.imooc.apigateway.filter;//package com.imooc.apigateway.filter; |
Login权限校验
买家过滤器
AuthBuyerFilter.java
1 | package com.imooc.apigateway.filter; |
卖家过滤器
AuthSellerFilter.java
1 | package com.imooc.apigateway.filter; |
其他
CookieUtil.java
1 | package com.imooc.apigateway.utils; |
CookieConstant.java
1 | package com.imooc.apigateway.constant; |
RedisConstant.java
1 | package com.imooc.apigateway.constant; |
LoginController.java
1 | package com.imooc.user.controller; |
Hystrix 服务熔断与降级
Hystrix
pom.xml
1 | <dependency> |
bootstrap.yml
1 | hystrix: |
OrderApplication.java
1 | package com.imooc.order; |
HystrixController.java
1 | package com.imooc.order.controller; |
Hystrix Dashboard
控制台
http://localhost:8081/hystrix/
监控效果
http://localhost:8081/actuator/hystrix.stream
调用链路监控
sleuth
pom.xml
1 | <!--已经包含了spring-cloud-starter-sleuth和spring-cloud-sleuth-zipkin--> |
bootstrap.yml
1 | spring: |
zipkin
安装并运行zipkin
https://zipkin.io/pages/quickstart.html
1 | docker run -d -p 9411:9411 openzipkin/zipkin |
登录zipkin控制台
测试
访问要测试的服务
登录zipkin控制台,看是否有调用链路记录生成。