【微服务】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 的配置
相关推荐
Lill_bin1 小时前
JVM内部结构解析
jvm·后端·spring cloud·微服务·云原生·ribbon
xmh-sxh-13144 小时前
微服务配置中心介绍
微服务
GoppViper17 小时前
golang学习笔记24——golang微服务中配置管理问题的深度剖析
笔记·后端·学习·微服务·golang·配置管理
2401_8566545117 小时前
员工疯狂打CALL!解锁企业微信新玩法,2024年必学秘籍来啦!
安全·微服务·微信·电脑·企业微信
世俗ˊ20 小时前
微服务-- Sentinel的使用
java·微服务·sentinel
你是我的天晴2 天前
Istio:微服务网格的强大工具,Istio介绍
微服务·云原生·istio
Lill_bin2 天前
Ribbon简介
分布式·后端·spring cloud·微服务·云原生·ribbon
jyl_sh2 天前
Ribbon (WPF)
ribbon·wpf·client·桌面程序开发·c/s客户端
HoweWWW2 天前
k8s 微服务 ingress-nginx 金丝雀发布
微服务·容器·kubernetes
珍珠是蚌的眼泪2 天前
微服务_1、入门
分布式·微服务·eureka·springcloud·注册中心