栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring cloud day(4) ribbon和feign

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring cloud day(4) ribbon和feign

Spring cloud day(4) ribbon和feign
  • 一、ribbon
    • 1.1 ribbon概念
    • 1.2 负载均衡
    • 1.3 ribbon架构
    • 1.4 ribbom pom
    • 1.5 restemplate 中的object和entity
    • 1.6 IRule
    • 1.7 ribbon规则替换
  • 二、feign
    • 2.1 概念
    • 2.2 Feign和openFeign
    • 2.3 openFeign的调用步骤
    • 2.4 feign过程图
    • 2.5 feign超市控制
    • 2.6 日志增强

一、ribbon 1.1 ribbon概念

1.2 负载均衡

  • 集中式LB

  • 进程内LB

1.3 ribbon架构

1.4 ribbom pom


    org.springframework.cloud
    spring-cloud-starter-netflix-ribbon
    2.2.10.RELEASE


1.5 restemplate 中的object和entity

  • entity举例
public CommenResult getPaymentEntity(@PathVariable("id") int id){
        ResponseEntity forEntity = restTemplate.getForEntity(PAYMENT_URL + "/getPayment?id=" + id, CommenResult.class);
        if(forEntity.getStatusCode().is2xxSuccessful()){
            return forEntity.getBody();
        }else {
            return  new CommenResult<>(444,"failed");
        }
    }
1.6 IRule
  • 根据特定的算法从服务列表中选择一个要访问的服务


1.7 ribbon规则替换
  • 步骤
  • 注意
    替换规则不能与componentscan注解扫描到的在同一个包或者子包中
  • 替换配置类
@Configuration
public class ruleConfig {
    @Bean
    public IRule myRule(){
        //定义为随机
        return new RandomRule();
    }
}

  • 主启动类添加注解:@RibbonClient
  • name:服务名称,configuration :规则配置类
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "payment-services-8002",configuration = ruleConfig.class)
public class OrderBoot {
    public static void main(String[] args) {
        SpringApplication.run(OrderBoot.class, args);
    }
}

二、feign 2.1 概念

2.2 Feign和openFeign

2.3 openFeign的调用步骤
  • pom

        
            org.example
            CommModule
            ${project.version}
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
        
        
        
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
  • yml
server:
  port: 80

#spring:
#  application:
#    name: cloud-order-service

eureka:
  client:
    register-with-eureka: false # 不注册进eureka服务中心
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

  • 主启动类:@EnableFeignClients
@SpringBootApplication
@EnableFeignClients // 启动feign
public class feignMain {

    public static void main(String[] args) {
        SpringApplication.run(feignMain.class, args);
        System.out.println("启动成功");

    }

}
  • 业务类:与远程接口提供的业务对应,@FeignClient
@Component
//微服务名称
@FeignClient(value = "PAYMENT-SERVICE")
public interface PaymentService {
    //服务端的接口
    @GetMapping("/getPayment")
    public CommenResult getPayment(@RequestParam("id")int id);
}
  • controller
@RestController
@RequestMapping("consumer")
@Slf4j
public class feignController{
    @Resource
    private PaymentService paymentService;

    @GetMapping(value = "/feign/payment/get/{id}")
    public CommenResult getPaymentById(@PathVariable("id") int id) {
        return paymentService.getPayment(id);
    }

}

  • 负载均衡
    feign自身集合了
2.4 feign过程图
  • feign把远程提供的服务在本地端包装到接口中,自己就可以调用

2.5 feign超市控制

  • 测试超时控制,服务端8081业务
    @GetMapping("/payment/timeout")
    public String testTimeOut() throws InterruptedException {
        TimeUnit.SECONDS.sleep(3);
        return serverport;
    }
  • 相关yml配置
ribbon:
  #指的是建立连接所用的时间,适用于网络状况正常的情况下, 两端连接所用的时间
  ReadTimeout: 5000
  #指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

2.6 日志增强
  • 日志级别
  • 配置类
@Configuration
public class feignConfig {
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

  • 对应yml配置
logging:
  level:
    # feign日志以 debug 级别监控 com.atguigu.springcloud.service.PaymentFeignService 接口
    com.atguigu.springcloud.service.PaymentFeignService: debug

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号