# cloud-base **Repository Path**: zhuangsen/cloud-base ## Basic Information - **Project Name**: cloud-base - **Description**: SpringCloud全家桶demo - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-10-20 - **Last Updated**: 2022-01-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ##### 1.发现不使用@EnableEurekaClient也能注册到服务中(应该是新版Spring Cloud的一个特性。) # 自定义实例编号 instance: instance-id: ${spring.application.name}:${server.port}:@project.version@ # 配置使用主机名注册服务 hostname: node1 node1是我本机配置的其中一个主机名 OS X/Linux系统下修改主机名 # 不向注册中心注册 , 是否注册到服务中心 , false = 不注册,true = 注册 eureka.client.register-with-eureka: false, 简而言之就是当我们引用了EurekaClient的依赖后, 并且我们的两个开关(eureka.client.enabled和spring.cloud.service-registry.auto-registration.enabled)不手动置为false, Spring就会自动帮助我们执行EurekaAutoServiceRegistration类里的start()方法,而注册的动作就是在该方法里完成的。 所以,我们的EurekaClient工程,并不需要显式的在SpringBoot的启动类上标注@EnableEurekaClient注解。 ##### 2.HystrixDashboard HystrixDashboard集成: spring-cloud-starter-netflix-hystrix-dashboard有web依赖,不手动添加web依赖也可以 @EnableHystrixDashboard 访问http://localhost:8100/hystrix #解决Dashboard 出现错误Unable to connect to Command Metric Stream hystrix: dashboard: proxy-stream-allow-list: localhost HystrixClient集成: @EnableHystrix(里面集成了@EnableCircuitBreaker) //@EnableCircuitBreaker datashboard地址里面输入:http://localhost:8007/actuator/hystrix.stream management: endpoints: web: exposure: include: '*' ##### 3.hystrix-feign整合报错 java.lang.RuntimeException: java.io.IOException: Broken pipe(有待实验) 因为我使用spring-boot-starter-undertow 而不是tomcat??? (又不报错了) ##### 4.turbine: hystrix-turbine整合: datashboard地址填写:http://localhost:8009/turbine.stream hystrix-turbine-stream 整合: datashboard地址填写: http://localhost:8110 ##### 5.启动config-client报错: Could not resolve placeholder 'neo.hello' in value "${neo.hello}" 1.因为我都是配置在application.yml里面 config配置应配置在bootstrap.yml config client必须要在bootstrap.yml或bootstrap.properties中配置 原因:bootstrap配置会系统会优先加载;加载优先级比application高 2.还是报错,还有一个原因是 spring: application: name: 配置的和配置仓库里面配置文件名字不一样 ###### 6.启用config-client refresh: 添加actuator依赖 // 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。 @RefreshScope management: endpoints: web: exposure: #打开部分 include: refresh,health,info 最后curl -X POST http://localhost:8012/actuator/refresh ###### 7.bus refresh one config client org.springframework.cloud spring-cloud-starter-bus-amqp management: endpoints: web: exposure: #打开部分 include: bus-refresh,refresh,health,info 最后curl -X POST http://localhost:8014/actuator/bus-refresh ##### 8.bus refresh config server 在上面的流程中,我们已经到达了利用消息总线触发一个客户端bus/refresh,而刷新所有客户端的配置的目的。但这种方式并不优雅。原因如下: 打破了微服务的职责单一性。微服务本身是业务模块,它本不应该承担配置刷新的职责。 破坏了微服务各节点的对等性。 有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就不得不修改WebHook的配置。 因此使用bus refresh config server ###### 9.zuul路由问题 zuul: #必须加/ prefix: /pre 注意:private String servletPath = "/zuul"; 访问地址前面还可以加/zuul 当 stripPrefix=true 的时 (会移除) (http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/user/list), 当stripPrefix=false的时(不会移除) (http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/api/user/list #因为zuul有默认的隐射机制,如果没有以下的配置, 那么访问http://ip:port/c/也可以访问到你的c服务, 如果你不想向外界暴露除你配置的隐射之外的服务, 可以加上zuul.ignored-services:* zuul解决com.netflix.client.ClientException: Load balancer does not have available server for clien:xxx https://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html |通配符|含义|举例|解释| |---|---|---|---| |?|匹配任意单个字符|/feign-consumer/?|匹配/feign-consumer/a,/feign-consumer/b,/feign-consumer/c等| |*|匹配任意数量的字符|/feign-consumer/*|匹配/feign-consumer/aaa,feign-consumer/bbb,/feign-consumer/ccc等,无法匹配/feign-consumer/a/b/c| |**|匹配任意数量的字符|/feign-consumer/*|匹配/feign-consumer/aaa,feign-consumer/bbb,/feign-consumer/ccc等,也可以匹配/feign-consumer/a/b/c| Zuul大部分功能都是通过过滤器来实现的,这些过滤器类型对应于请求的典型生命周期。 PRE: 这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择请求的微服务、记录调试信息等。 ROUTING:这种过滤器将请求路由到微服务。这种过滤器用于构建发送给微服务的请求,并使用Apache HttpClient或Netfilx Ribbon请求微服务。 POST:这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。 ERROR:在其他阶段发生错误时执行该过滤器。 除了默认的过滤器类型,Zuul还允许我们创建自定义的过滤器类型。例如,我们可以定制一种STATIC类型的过滤器,直接在Zuul中生成响应,而不将请求转发到后端的微服务。 Zuul中默认实现的Filter 类型 顺序 过滤器 功能 pre -3 ServletDetectionFilter 标记处理Servlet的类型 pre -2 Servlet30WrapperFilter 包装HttpServletRequest请求 pre -1 FormBodyWrapperFilter 包装请求体 route 1 DebugFilter 标记调试标志 route 5 PreDecorationFilter 处理请求上下文供后续使用 route 10 RibbonRoutingFilter serviceId请求转发 route 100 SimpleHostRoutingFilter url请求转发 route 500 SendForwardFilter forward请求转发 post 0 SendErrorFilter 处理有错误的请求响应 post 1000 SendResponseFilter 处理正常的请求响应 禁用指定的Filter 可以在application.yml中配置需要禁用的filter,格式: zuul: FormBodyWrapperFilter: pre: disable: true ##### 10.zuul重试机制 zuul: #是否开启重试功能 retryable: true ribbon: #对当前服务的重试次数 MaxAutoRetries: 6 #切换相同Server的次数 MaxAutoRetriesNextServer: 0 zuul重试的时候必须配置 ribbon.restclient.enabled=true, 不然会报错: com.netflix.client.ClientException: Load balancer does not have available server for clien:xxx 添加依赖: org.springframework.retry spring-retry https://www.jianshu.com/p/b94ae79bba7d ##### 11.zipkin-rabbitmq集成 zipkin: # base-url: http://localhost:9999 # 不需要配置base-url sender: type: rabbit rabbitmq: queue: zipkin-queue rabbitmq: host: localhost port: 5672 username: zipkin password: zipkin virtual-host: zipkin-vhost ##### 12.consul集成 http://localhost:8500/v1/agent/checks All service checks failing(http://192.168.31.99:8050/actuator/health 报404) 因为未集成spring-boot-starter-actuator ##### 13.spring cloud gateway https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-stripprefix-gatewayfilter-factory https://www.fangzhipeng.com/img/jianshu/12191355-7c74ff861a209cd9.png 一、Route Predicate Factories https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-request-predicates-factories # - Path=/oil/{segment} # - After=2020-10-15T20:30:30+08:00[Asia/Shanghai] # - Before=2020-10-15T20:31:30+08:00[Asia/Shanghai] # - Between=2020-10-15T20:33:30+08:00[Asia/Shanghai],2020-10-15T20:34:30+08:00[Asia/Shanghai] #curl http://localhost:8060 --cookie "name=zs" # - Cookie=name,zs #接收 2 个参数,一个 header 中属性名称和一个正则表达式,这个属性值和正则表达式匹配则执行。 #curl http://localhost:8060 -H "X-Request-Id:666666" # - Header=X-Request-Id, \d+ #curl http://localhost:8060 -H "Host: www.zhuangsen.live" # - Host=**.zhuangsen.live #curl http://localhost:8060 #curl -X POST http://localhost:8060 # - Method=GET # - Query=smile #只要当请求中包含 keep 属性并且参数值是以 pu 开头的长度为三位的字符串才会进行匹配和路由。 # - Query=keep, pu. #Predicate 也支持通过设置某个 ip 区间号段的请求才会路由, #RemoteAddr Route Predicate 接受 cidr 符号(IPv4 或 IPv6 )字符串的列表(最小大小为1), #例如 192.168.0.1/16 (其中 192.168.0.1 是 IP 地址,16 是子网掩码)。 # - RemoteAddr=192.168.31.99 # 将各种 Predicate 组合起来一起使用 # predicates: # - Host=**.foo.org # - Path=/headers # - Method=GET # - Header=X-Request-Id, \d+ # - Query=foo, ba. # - Query=baz # - Cookie=chocolate, ch.p # - After=2018-01-20T06:06:06+08:00[Asia/Shanghai] 二、GatewayFilter Factories https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories filters: - AddRequestHeader=X-Request-red, blue - AddRequestHeader=X-Request-Red, Blue-{segment} - AddRequestParameter=red, blue - AddRequestParameter=foo, bar-{segment} - AddResponseHeader=X-Response-Red, Blue - AddResponseHeader=foo, bar-{segment} - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin - CircuitBreaker=myCircuitBreaker - MapRequestHeader=Blue, X-Request-Red - RemoveRequestHeader=X-Request-Foo - RemoveResponseHeader=X-Response-Foo - RemoveRequestParameter=red - RewritePath=/red/?(?.*), /$\{segment} ##### 14.gateway-service spring.cloud.gateway.discovery.locator.enabled为true, 表明gateway开启服务注册和发现的功能, 并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router, 这个router将以服务名开头的请求路径转发到对应的服务。 spring.cloud.gateway.discovery.locator.lowerCaseServiceId 是将请求路径上的服务名配置为小写 (因为服务注册的时候,向注册中心注册时将服务名转成大写的了), 比如以/service-hi/*的请求路径被路由转发到服务名为service-hi的服务上。 ##### 15.Spring Cloud Sleuth 日志打印: [sleuth-demo-without-zipkin,6c00dba1679ee164,6c00dba1679ee164,false] 这一串数据里包含四个部分 appname - 应用程序名称 traceId - 追踪系统中的唯一标识 spanId - 具体操作的唯一标识 exportable - 是否日志要导出到Zipkin ##### 16.Spring Cloud Config springcloud2020 版本 把Bootstrap被默认禁用, 同时spring.config.import加入了对解密的支持。对于Config Client、Consul、Vault和Zookeeper的配置导入, 如果你需要使用原来的配置引导功能, 那么需要将org.springframework.cloud:spring-cloud-starter-bootstrap依赖引入到工程中 config-server refresh: https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#_push_notifications_and_spring_cloud_bus