文章目录

1.Ribbon1.基本介绍1.Ribbon是什么?2.LB(负载均衡)分类3.Ribben架构图

2.负载均衡1.负载均衡常用算法2.切换负载均衡算法的实例1.com/sun/springcloud/config/RibbonRule.java 编写配置类来注入Ribbon的负载均衡算法2.启动类添加注解@RibbonClient指定Ribbion负载均衡算法的配置类3.启动全部服务测试

2.OpenFeign1.基本介绍2.示意图3.OpenFeign实例1.创建新模块 member-service-consumer-openfeign-812.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是什么?

2.LB(负载均衡)分类

3.Ribben架构图

2.负载均衡

1.负载均衡常用算法

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负载均衡算法的配置类

3.启动全部服务测试

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

2.OpenFeign

1.基本介绍

2.示意图

3.OpenFeign实例

1.创建新模块 member-service-consumer-openfeign-81

2.pom.xml引入依赖

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

org.example

e_commerce_center-common-api

1.0-SNAPSHOT

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以及这个模块,查看注册情况

6.starter-actuator 监控系统查看81模块是否健康

浏览器输入(http://localhost:81/actuator/health)

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,而我才开了一个,没关系

11.注意事项

4.OpenFeign日志配置

1.基本介绍

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.要启动的服务

2.postman测试

3.日志输出

5.关闭日志

1.注销OpenFeignConfig.java的内容

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

5.OpenFeign超时配置

1.默认一秒就算超时

2.application.yml 配置超时时间

ribbon:

ReadTimeout: 8000 # 从服务提供方获取到可用资源的全部时间

ConnectionTimeout: 8000 # 两端建立连接时间

3.关闭超时配置,注销掉application.yml 的配置

推荐链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: