SpringCloud(2)--服务调用

SpringCloud

技术栈

服务注册与发现

EUREKA(停更),Zookeeper,Consul,Nocos

服务调用

RIBBON(NETFLIX网飞), LoadBalancer, FELGN(停更) , OpenFeign

服务熔断降级

HYSTRIX (不推荐) ,resilience4j (国外推荐) ,sentienl (国内推荐)

服务网关

ZUUL (不推荐) , gateWay

服务分布配置

SpringCloud Config (不推荐) , Nocos

服务总线

Bus (不推荐) , Nocos


Ribbon

是什么?

客户端的负载均衡和服务调用工具

能干什么?

负载均衡+RestTemplate

LB负载均衡?

Load Balance – 将用户的请求平摊分配到多个服务上,从而到达系统的HA(高可用)

Ribbon与Nginx的区别?

Ribbon :本地的负载均衡 --在调用微服务接口时,会在注册中心获取注册信息服务列表,存到Jvm,从而在本地实现PRC
Nginx : 服务器的负载均衡 --客户端的所有请求都会交给nginx ,然后再转发到不同的服务器

RestTemplate

RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值类型即可

RestTemplate

Ribbon的负载均衡

@LoadBalanced //负载均衡
默认 轮询算法

pom.xml

在这里插入图片描述

ApplicationConfiguration
@Configuration
public class ApplicationConfiguration {

    @Bean
    @LoadBalanced //负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

IRule

在这里插入图片描述

如何替换IRule

编写配置类

注意不能在==@ComponentScan== 注解能扫描的地方定义配置

package com.wu.myRule;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @program: IClould2020Wu
 * @author: Mr-Jies
 * @create: 2020-05-16 10:54
 **/

@Configuration
public class MySelfRule {
    @Bean
    public IRule iRule(){
        //定义为随机
        return new RandomRule();
    }
}

控制层
package com.wu.icloud.controller;

@RestController
public class OrderController {

    @GetMapping(value = "/consumer/payment1/get/{id}")
    public CommonResult getPaymentById2(@PathVariable("id") Long id) {

        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(URL_PATH + "/payment/get/" + id, CommonResult.class);
        if (forEntity.getStatusCode().is2xxSuccessful()){
            CommonResult body = forEntity.getBody();
            return body;
        }

        return new CommonResult(444,"error");
    }
    
}
主启动
@EnableEurekaClient
@SpringBootApplication
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class);
    }
}

手写轮询算法

学完juc再回头写


OpenFeign

是什么?

声明式的web服务客户端,只需创建一个接口并在注解上添加注解即可做到服务调用

能干什么?

使编写Java HTTP客户端变的简单 在使用Ribbon+ RestTemplate时, 利用RestTemplate对http请求的封装处理, 形成了一套模版化的调用方法。在Feign的实现下我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),(方便了)

Feign集成了Ribbon

利用Ribbon维护了Payment的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用

简单使用

@EnableFeignClients
@FeignClient(name = “CLOUD-PAYMENT-SERVICE”)

pom.xml
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
application.yml
server:
  port: 80
spring:
  application:
    name: cloud-order-service
    
eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  # 集群版

主启动

@EnableFeignClients

@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class OpenFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OpenFeignMain80.class,args);
    }
}
Service

通过接口与服务端映射
@FeignClient(name = “CLOUD-PAYMENT-SERVICE”)

@Component
@FeignClient(name = "CLOUD-PAYMENT-SERVICE")
public interface OpenfeignPaymentService {

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);

    @GetMapping("/payment/timeout")
    public String timeout();
}
Controller

直接调用service就行了,不需要自己再用RestTempate请求

@RestController
@Slf4j
public class OpenFeignOrder80Controller {

    @Autowired
    private OpenfeignPaymentService openfeignPaymentService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        return openfeignPaymentService.getPaymentById(id);
    }

    @GetMapping("/consumer/payment/timeout")
    public String timeout(){
        return openfeignPaymentService.timeout();
    }
}

OpenFeign超时控制

在这里插入图片描述
OpenFeign客户端只等待一秒,如果服务端处理需要超过一秒,导致客户端不想等待,直接返回报错

解决

openFeign是ribbon的集成

ribbon:
  # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
  # 指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

OpenFeign日志打印

Config
@Configuration
public class FeignConfig {

    @Bean
    public Logger.Level feignLogger(){
        return Logger.Level.FULL;
    }
}
yml
logging:
  level:
    com.wu.scf.service.OpenfeignPaymentService: debug

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值