4SpringCloud部分

四SpringCloud部分

1具体功能

​ 1.1nacos注册中心/配置中心

​ 1.2GateWay网关

​ 1.3feign远程调用

​ 1.4hystrix熔断机

2具体知识点

​ 2.0cloud各板块流程介绍

  • 第一步接口化请求调用 只是定义 就是我们写接口那步

  • 第二步 feign就是根据服务名字找到服务调用

  • 第三步 hystrix 熔断机 如果之前vod突然宕机了 就直接熔断 不会再去调用 就是去看一下要调用的那个服务能不能调用 不能调就熔断 能调用就继续

  • 第四步 ribbon 负载均衡 如果vod放到集群位置 ribbon就会平均分到各个集群中的主机

  • 第五步 http client就是最终的调用

​ 2.1nacos注册中心

  • 下载nacos安装文件

  • 解压文件,找到startup.cmd,启动就行了(新版startup.cmd -m standalone)

  • 访问naocs

  • 服务注册步骤

    • 第一步:引入依赖

    • 第二步:application.properties添加配置信息

      • # nacos服务地址
        spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
        
    • 第三步:启动类添加注解

      • @EnableDiscoveryClient //nacos注册
        

​ 2.2GateWay网关

  • 创建一个新模块,引入网关依赖

  • 编写application.properties配置路由规则

    • # 服务端口
      server.port=8333
      
      # 服务名
      spring.application.name=service-gateway
      
      # nacos服务地址
      spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
      
      #使用服务发现路由
      spring.cloud.gateway.discovery.locator.enabled=true
      
      #service-vod模块配置
      #设置路由id
      spring.cloud.gateway.routes[0].id=service-vod
      #设置路由的uri
      spring.cloud.gateway.routes[0].uri=lb://service-vod
      #设置路由断言,代理servicerId为auth-service的/auth/路径
      spring.cloud.gateway.routes[0].predicates= Path=/*/vod/**
      
  • 解决跨域问题(在网关模块创建一个配置类)

    • @Configuration
      public class CorsConfig {
          //处理跨域
          @Bean
          public CorsWebFilter corsFilter() {
              CorsConfiguration config = new CorsConfiguration();
              config.addAllowedMethod("*");
              config.addAllowedOrigin("*");
              config.addAllowedHeader("*");
              UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
              source.registerCorsConfiguration("/**", config);
              return new CorsWebFilter(source);
          }
      }
      

​ 2.3feign远程调用

  • 前提条件是在nacos进行注册

  • 引入相关依赖

  • 在调用端启动类上添加注解

    • @EnableFeignClients  //feign
      
  • 在调用端创建interface,使用注解指定调用服务名称,定义调用的方法路径

    • @FeignClient(value = "模块名")
      
  • 也可以把所有远程调用接口放在一个公共模块下(这种更多使用)

    • @FeignClient(value = "service-activity")
      public interface CouponInfoFeignClient {
      
          //@PathVariable注意一定要指定参数名称 否则出错
          @ApiOperation(value = "获取优惠券")
          @GetMapping(value = "/api/activity/couponInfo/inner/getById/{couponId}")
          CouponInfo getById(@PathVariable("couponId") Long couponId);
      }
      
  • 注意点

    • 首先把需要的方法先直接复制过来
    • 然后把上面的路径改成完全路径就行了
    • 然后如果有@PathVariable要特别注意,一定要指定参数名称 否则出错

​ 2.4 hystrix熔断机

  • 添加依赖

  • 在调用端部分配置文件中开启熔断机制

    • #开启熔断机制
      feign.hystrix.enabled=true
      # 设置hystrix超时时间
      #hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
      
  • 在创建feign创建interface之后,还需要创建interface对应实现类,在实现类实现方法,出错了输出内容

    • @Component
      public class VodFileDegradeFeignClient implements VodClient {
         //出错之后会执行
          @Override
          public R removeAlyVideo(String id) {
              return R.error().message("删除视频失败,熔断器!");
          }
      }
      
  • 在feign创建的interface上面添加注解和属性(fallback = xxx)

    • @FeignClient(name = "service-vod",fallback = VodFileDegradeFeignClient.class)
      @Component
      public interface VodClient {
         //略
      }
      
  • 测试

​ 2.5nacos配置中心

  • 登录nacos主页,添加配置文件

  • Data ID为:服务名称-环境设置值-配置文件类型

    • service-statics.properties
  • 配置文件加载顺序

    • 1:bootstrap.properties
    • 2:application.properties
    • 3:application-dev.properties
  • 所以我们创建bootstrap.properties在里面进行配置

    • #配置中心地址
      spring.cloud.nacos.config.server-addr=127.0.0.1:8848
      
      #该配置影响统一配置中心的dataId
      spring.application.name=service-statics
      
      #配置中心分支
      spring.profiles.active=dev
      
      #配置中心命名空间
      spring.cloud.nacos.config.namespace=631a0632-2bf8-428e-80ab-c6f122c73e46
      
posted @ 2022-07-14 15:47  fao99  阅读(393)  评论(0)    收藏  举报