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

文章目录

1.Ribbon

1.基本介绍
1.Ribbon是什么?
2.LB(负载均衡)分类
3.Ribben架构图
2.负载均衡
1.负载均衡常用算法
2.切换负载均衡算法的实例
1.com/sun/springcloud/config/RibbonRule.java 编写配置类来注入Ribbon的负载均衡算法
java 复制代码
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负载均衡算法的配置类
3.启动全部服务测试

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

2.OpenFeign

1.基本介绍
2.示意图
3.OpenFeign实例
1.创建新模块 member-service-consumer-openfeign-81
2.pom.xml引入依赖
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 配置端口,服务注册和服务发现
yaml 复制代码
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.创建启动类
java 复制代码
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以及这个模块,查看注册情况
6.starter-actuator 监控系统查看81模块是否健康
浏览器输入(http://localhost:81/actuator/health)
7.com/sun/springcloud/service/MemberFeignService.java 发现服务,声明要调用的方法
  • 首先这是一个接口注入容器
  • 然后使用了@FeignClient()注解来从server中发现服务,并将http://ip+端口放到"MEMBER-SERVICE-PROVIDER"
  • 所以只需要将想要远程调用的方法放到粘贴到这个接口声明一下即可
java 复制代码
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的代理对象
  • 当使用代理对象来调用接口的方法时,就会远程调用服务提供者的方法
java 复制代码
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注解
java 复制代码
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,而我才开了一个,没关系
11.注意事项
4.OpenFeign日志配置
1.基本介绍
2.com/sun/springcloud/config/OpenFeignConfig.java 编写配置类,注入配置等级
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形式输出日志
yaml 复制代码
# 以debug的形式输出MemberFeignService接口的日志
logging:
  level:
    com.sun.springcloud.service.MemberFeignService: debug
4.启动服务进行测试
1.要启动的服务
2.postman测试
3.日志输出
5.关闭日志
1.注销OpenFeignConfig.java的内容
2.注销application.yml的日志配置
5.OpenFeign超时配置
1.默认一秒就算超时
2.application.yml 配置超时时间
yaml 复制代码
ribbon:
  ReadTimeout: 8000 # 从服务提供方获取到可用资源的全部时间
  ConnectionTimeout: 8000 # 两端建立连接时间
3.关闭超时配置,注销掉application.yml 的配置
相关推荐
SirLancelot110 小时前
K8s-kubernetes(二)资源限制-详细介绍
微服务·云原生·容器·kubernetes·k8s·devops·kubelet
为什么要内卷,摆烂不香吗17 小时前
Docker容器技术全面解析(一):入门
docker·微服务·容器
孤狼程序员19 小时前
【Spring Cloud 微服务】1.Hystrix断路器
java·spring boot·spring·微服务
为什么要内卷,摆烂不香吗1 天前
kubernetes(4) 微服务
linux·运维·微服务·容器·kubernetes
扶风呀2 天前
具有熔断能力和活性探测的服务负载均衡解决方案
运维·负载均衡
Hello World呀2 天前
springcloud负载均衡测试类
spring·spring cloud·负载均衡
鼠鼠我捏,要死了捏2 天前
基于Spring Boot与gRPC的高性能微服务架构设计分享
spring boot·微服务·grpc
勇往直前plus2 天前
一文学习nacos和openFeign
java·学习·微服务·openfeign
麦兜*2 天前
Spring Boot调用优化版AI推理微服务 集成 NVIDIA NIM指南
java·人工智能·spring boot·后端·spring cloud·微服务·ai编程
扶风呀3 天前
分布式与微服务宝典
分布式·微服务·架构