【微服务】Ribbon(负载均衡,服务调用)+ OpenFeign(服务发现,远程调用)

news/2024/7/24 11:20:12 标签: 微服务, ribbon, 负载均衡, OpenFeign

文章目录

    • 1.Ribbon
    • 2.OpenFeign
        • 1.基本介绍
        • 2.示意图
        • 3.OpenFeign实例
          • 1.创建新模块 member-service-consumer-openfeign-81
          • 2.pom.xml引入依赖
          • 3.application.yml 配置端口,服务注册和服务发现
          • 4.创建启动类
          • 5.启动9001和9002以及这个模块,查看注册情况
          • 6.starter-actuator 监控系统查看81模块是否健康
            • 浏览器输入(http://localhost:81/actuator/health)
          • 7.com/sun/springcloud/service/MemberFeignService.java 发现服务,声明要调用的方法
          • 8.src/main/java/com/sun/springcloud/controller/MemberConsumerOpenfeignController.java 远程调用服务提供者的方法
          • 9.启动类添加@EnableFeignClients注解
          • 10.启动9001,10001,81测试
          • 11.注意事项
        • 4.OpenFeign日志配置
          • 1.基本介绍
          • 2.com/sun/springcloud/config/OpenFeignConfig.java 编写配置类,注入配置等级
          • 3.application.yml 配置debug形式输出日志
          • 4.启动服务进行测试
            • 1.要启动的服务
            • 2.postman测试
            • 3.日志输出
          • 5.关闭日志
            • 1.注销OpenFeignConfig.java的内容
            • 2.注销application.yml的日志配置
        • 5.OpenFeign超时配置
          • 1.默认一秒就算超时
          • 2.application.yml 配置超时时间
          • 3.关闭超时配置,注销掉application.yml 的配置

1.Ribbon

1.基本介绍
1.Ribbon是什么?

image-20240325140528581

2.LB(负载均衡)分类

image-20240325142400285

3.Ribben架构图

image-20240325143301839

2.负载均衡
1.负载均衡常用算法

image-20240325143606740

2.切换负载均衡算法的实例
1.com/sun/springcloud/config/RibbonRule.java 编写配置类来注入Ribbon的负载均衡算法
package com.sun.springcloud.config;

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

/**
 * Description: 配置Ribbon的负载均衡算法为RandomRule
 *
 * @Author sun
 * @Create 2024/3/25 14:43
 * @Version 1.0
 */
@Configuration
public class RibbonRule {
    @Bean
    public IRule myRibbonRule() {
        return new RandomRule();
    }
}

2.启动类添加注解@RibbonClient指定Ribbion负载均衡算法的配置类

image-20240325145103511

3.启动全部服务测试

由于使用的负载均衡算法是RandomRule,所以在发现服务之后的调用是随机的

image-20240325145547869

image-20240325145555068

OpenFeign_65">2.OpenFeign

1.基本介绍

image-20240325150247410

2.示意图

image-20240325150520511

OpenFeign_75">3.OpenFeign实例
1.创建新模块 member-service-consumer-openfeign-81

image-20240325150752913

2.pom.xml引入依赖
    <dependencies>
        <!-- 引入openfeign的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- 引入eureka的客户端场景启动器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <!-- 使用版本仲裁 -->
        </dependency>
        <!-- springboot web starter 用来监听端口-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 如果在子工程/模块指定了 version,则以指定为准 -->
        </dependency>
        <!--
        1. starter-actuator 是 springboot 程序的监控系统,可以实现健康检查,info 信息
        等
        2. 访问 http://localhost:10000/actuator 可以看到相关链接, 还可以做相关设置. -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 公共模块的jar包 -->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>e_commerce_center-common-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>
3.application.yml 配置端口,服务注册和服务发现
server:
  port: 81 # 监听81端口
spring:
  application:
    name: member-service-consumer-openfeign-81
eureka: # eureka客户端配置
  client:
    register-with-eureka: true # 将自己注册到eureka服务
    fetch-registry: true # 发现服务功能,如果是集群,必须要能发现服务才能配合ribben进行负载均衡
    service-url:
      # 需要注册到两个服务,则只需要用逗号间隔
      defaultZone: http://eureka9001.com:9001/eureka/, http://eureka9002.com:9002/eureka/
4.创建启动类
package com.sun.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * Description:
 *
 * @Author sun
 * @Create 2024/3/25 15:23
 * @Version 1.0
 */
@SpringBootApplication
@EnableEurekaClient
public class MemberConsumerOpenfeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(MemberConsumerOpenfeignApplication.class, args);
    }
}

5.启动9001和9002以及这个模块,查看注册情况

image-20240325153447964

image-20240325153504220

6.starter-actuator 监控系统查看81模块是否健康
浏览器输入(http://localhost:81/actuator/health)

image-20240325153755344

7.com/sun/springcloud/service/MemberFeignService.java 发现服务,声明要调用的方法
  • 首先这是一个接口注入容器
  • 然后使用了@FeignClient()注解来从server中发现服务,并将http://ip+端口放到"MEMBER-SERVICE-PROVIDER"
  • 所以只需要将想要远程调用的方法放到粘贴到这个接口声明一下即可
package com.sun.springcloud.service;

import com.sun.springcloud.util.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * Description: 使用@FeignClient注解进行服务发现,并准备好要远程调用的方法
 *
 * @Author sun
 * @Create 2024/3/25 15:49
 * @Version 1.0
 */
@Component // 注入容器
@FeignClient("MEMBER-SERVICE-PROVIDER") // 消费者进行服务发现找到指定的提供者的http://ip+端口
public interface MemberFeignService {
    /
     * 要远程调用的方法,直接粘贴过来即可
     * 此时的url = http://MEMBER-SERVICE-PROVIDER/member/get/{id}
     * @param id
     * @return
     */
    @GetMapping("/member/get/{id}") // 这里使用的路径参数
    public Result getMemberById(@PathVariable("id") Long id);
}

8.src/main/java/com/sun/springcloud/controller/MemberConsumerOpenfeignController.java 远程调用服务提供者的方法
  • 这里注入了针对MemberFeignService的代理对象
  • 当使用代理对象来调用接口的方法时,就会远程调用服务提供者的方法
package com.sun.springcloud.controller;

import com.sun.springcloud.service.MemberFeignService;
import com.sun.springcloud.util.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Description: 注入MemberFeignService的代理对象,使用代理对象进行远程调用
 *
 * @Author sun
 * @Create 2024/3/25 15:55
 * @Version 1.0
 */
@RestController
public class MemberConsumerOpenfeignController {
    /*
    注入一个针对接口MemberFeignService的代理对象,使用这个代理对象可以直接远程调用服务发现的方法
     */
    @Resource
    private MemberFeignService memberFeignService;

    @GetMapping("/member/get/{id}") // 这里使用的路径参数
    public Result getMemberById(@PathVariable("id") Long id) {
        return memberFeignService.getMemberById(id);
    }

}

9.启动类添加@EnableFeignClients注解
package com.sun.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * Description:
 *
 * @Author sun
 * @Create 2024/3/25 15:23
 * @Version 1.0
 */
@SpringBootApplication
@EnableEurekaClient // 作为EurekaClient启动
@EnableFeignClients
public class MemberConsumerOpenfeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(MemberConsumerOpenfeignApplication.class, args);
    }
}

10.启动9001,10001,81测试
  • 这里连接被拒绝的原因是这个消费者注册了两个server,而我才开了一个,没关系

image-20240325162506405

11.注意事项

image-20240325162639039

OpenFeign_307">4.OpenFeign日志配置
1.基本介绍

image-20240325163037351

image-20240325163147199

OpenFeignConfigjava__315">2.com/sun/springcloud/config/OpenFeignConfig.java 编写配置类,注入配置等级
package com.sun.springcloud.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description: 配置OpenFeign日志
 *
 * @Author sun
 * @Create 2024/3/25 16:34
 * @Version 1.0
 */
@Configuration
public class OpenFeignConfig {
    @Bean
    public Logger.Level level() {
        return Logger.Level.FULL;
    }
}

3.application.yml 配置debug形式输出日志
# 以debug的形式输出MemberFeignService接口的日志
logging:
  level:
    com.sun.springcloud.service.MemberFeignService: debug
4.启动服务进行测试
1.要启动的服务

image-20240325164258175

2.postman测试

image-20240325164434194

3.日志输出

image-20240325164452848

5.关闭日志
OpenFeignConfigjava_366">1.注销OpenFeignConfig.java的内容

image-20240325164805927

2.注销application.yml的日志配置

image-20240325164814057

OpenFeign_374">5.OpenFeign超时配置
1.默认一秒就算超时

image-20240325165814721

2.application.yml 配置超时时间
ribbon:
  ReadTimeout: 8000 # 从服务提供方获取到可用资源的全部时间
  ConnectionTimeout: 8000 # 两端建立连接时间
3.关闭超时配置,注销掉application.yml 的配置

image-20240325170035560


http://www.niftyadmin.cn/n/5450365.html

相关文章

微服务(基础篇-004-Feign)

目录 http客户端Feign Feign替代RestTemplate&#xff08;1&#xff09; Feign的介绍&#xff08;1.1&#xff09; 使用Feign的步骤&#xff08;1.2&#xff09; 自定义配置&#xff08;2&#xff09; 配置Feign日志的两种方式&#xff08;2.1&#xff09; Feign使用优化…

申请GeoTrust数字证书

GeoTrust介绍&#xff1a; 大家应该都不陌生&#xff0c;作为最老资格的一批国际大牌证书&#xff0c;GeoTrust的品牌效益和使用群体非常庞大。在数字证书领域也是当之无愧的龙头地位&#xff0c;作为Symantec和Digicert的子品牌&#xff0c;证书安全性能方面毋庸置疑&#xf…

全量知识系统 灵活的模块化框架 (QA)

Q1.我前面谈到三种对象 ole对象、裸对象和值对象。你直接将裸对象理解为POJO了&#xff0c;这是欠妥的。我这里使用 它们&#xff0c;是为了表示变化(或者操作) 权限的不同--模块上的变化 模型上的变化和值上的变化 A1.OLE、裸对象与值对象的权限差异 好的&#xff0c;我会尝…

深度学习(三)vscode加jupyter notebook插件使用

0.前言 哎呀&#xff0c;我本次的实验是在新电脑上使用的&#xff0c;之前的笔记本上的环境什么的我都是很久以前弄好了的&#xff0c;结果到了新电脑上我直接忘了是该怎么配的了&#xff0c;不过万幸&#xff0c;花了点时间&#xff0c;查查补补&#xff0c;现在总算是可以了。…

【微服务】认识Dubbo+基本环境搭建

认识Dubbo Dubbo是阿里巴巴公司开源的一个高性能、轻量级的WEB和 RPC框架&#xff0c;可以和Spring框架无缝集成。Dubbo为构建企业级微服务提供了三大核心能力&#xff1a; 服务自动注册和发现、面向接口的 远程方法调用&#xff0c; 智能容错和负载均衡官网&#xff1a;https…

RDMA内核态通信测试krping学习

krping模块是一个内核可加载模块&#xff0c;它实现了客户机/服务器ping/pong程序&#xff0c;这个模块仅仅为了测试内核rdma的API&#xff08;单边的READ和Write&#xff1b;双边的SEND和RECEIVE&#xff09;。该模块允许通过一个名为/proc/krping的/proc条目建立连接并运行pi…

FFmpeg拉取RTSP流并定时生成10秒短视频

生成效果: 视频时长为10秒 生成格式为FLV 输出日志: 完整实现代码如下: 需要在Mac和终端先安装FFmpeg brew install ffmpeg CMake文件配置: cmake_minimum_required(VERSION 3.27) project(ffmpeg_open_stream) set(CMAKE_CXX_STANDARD 17)#头文件包目录 include_director…

TCP重传机制详解——04FACK

文章目录 TCP重传机制详解——04FACK什么是FACKFACK的发展为什么要引入FACK实战抓包讲解开启FACK场景&#xff0c;且达到dup ACK门限值开启FACK场景&#xff0c;未达到dup ACK门限值 为什么要淘汰FACK总结REF TCP重传机制详解——04FACK 什么是FACK FACK的全称是forward ackn…