首页 > 其他分享 >使用spring cloud loadbalancer 替代ribbon

使用spring cloud loadbalancer 替代ribbon

时间:2024-04-25 10:14:08浏览次数:18  
标签:spring server client cloud eureka loadbalancer

简介

spring cloud loadbalancer是spring自家推出的负载均衡器,可以平替ribbon。

spring cloud loadbalancer + RestClient

RestClient是spring framework 6.1(对应spring boot 3.2)里内置的一个http rest api调用器。相比RestTemplateRestClient的链式写法使用起来十分流畅丝滑。spring cloud loadbalancer自然也可以与RestClient/RestTemplate进行集成。

使用步骤

1、启动一个服务注册中心(兼容Eureka/Nacos/Consul/K8s等服务注册中心),此处以Eureka为例

依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

配置:

server:
  port: 8761
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0
  dashboard:
    enabled: true

代码:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApp {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApp.class, args);
    }

}

2、服务提供端(provider)

依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

配置:

server:
  port: 8001

spring:
  application:
    name: spring-httpexchange-server
  cloud:
    loadbalancer:
      enabled: true

eureka:
  client:
    enabled: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

2、调用端(client)

依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

配置:

server:
  port: 8002

spring:
  application:
    name: spring-httpexchange-client
  cloud:
    loadbalancer:
      enabled: true

eureka:
  client:
    enabled: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

配置类代码:

@Configuration
public class HttpClientConfig {

    @LoadBalanced
    @Bean
    RestClient.Builder restClientBuilder() {
        return RestClient.builder();
    }

    @Bean
    public HelloClient helloClient(RestClient.Builder restClientBuilder) {
        return HttpServiceProxyFactory
                .builder()
                .exchangeAdapter(
                        RestClientAdapter.create(
                                restClientBuilder.baseUrl("http://spring-httpexchange-server").build()
                        )
                )
                .build().createClient(HelloClient.class);
    }

}

client调用provider提供的远程http服务的代码无需做任何变化。

标签:spring,server,client,cloud,eureka,loadbalancer
From: https://www.cnblogs.com/jiayuan2006/p/18156964

相关文章

  • 使用Spring HttpExchange替代FeignClient进行http远程服务调用
    背景springboot3.0使用的springframework6.0里有一个全新的http服务调用注解@HttpExchange,该注解的用途是可以进行申明式http远程服务调用。与Feign作用相同,在springboot3.x里,由于本身spring内置,相比Feign可以大幅减少第三方包依赖,且比Feign进轻巧。依赖:@HttpExchange位......
  • SpringBoot项目实现日志打印SQL明细(包括SQL语句和参数)几种方式
    前言我们在开发项目的时候,都会连接数据库。有时候遇到问题需要根据我们编写的SQL进行分析,但如果不进行一些开发或者配置的话,这些SQL是不会打印到控制台的,它们默认是隐藏的。下面给大家介绍几种常用的方法。第一种、代码形式Mybatis框架是Java程序员最常用的数据库映射框架,MyBa......
  • spring-boot学习记录
    ......
  • 9.prometheus监控--监控springboot2.x(Java)
    一、环境部署yumsearchjava|grepjdkyuminstall-yjava-11-openjdk-devel二、监控java应用(tomcat/jar)JMXexporter负责收集Java虚拟机信息---没举例,等以后再做测试进入到tomcat安装目录,vimPROMETHEUS_JMX_EXPORTER_OPTS="-javaagent:../prometheus-exporter......
  • SpringCloud-MQ
    同步通讯和异步通讯微服务间通讯有同步和异步两种方式。同步通讯就像打电话,需要实时响应;异步通讯就像发邮件,不需要马上回复。两种方式各有优劣,打电话可以立即得到响应,但是却不能跟多个人同时通话。发送邮件可以同时与多个人收发邮件,但是往往响应会有延迟。Feign调用就属于同步方......
  • 什么是spring.factories
    对于maven中引入其他外部包加入容器的过程,需要用到spring.factoriesspring.factories的作用:将自动配置类与对应的配置类集中在一起,方便springboot自动装配,用KV记录了所需加入容器的类,正常情况下,通过@CompentScan注解就可以扫描springboot内的bean,而当我们需要调用包外的bean,就......
  • springboot的netty代码实操
    参考:https://www.cnblogs.com/mc-74120/p/13622008.htmlpom文件<dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId></dependency>启动类@EnableFeignClients@EnableDiscoveryClient@EnableSchedu......
  • Spring MVC拦截器实现,记录访问请求日志
    SpringMVC拦截器实现,记录访问请求日志1.创建拦截器类并实现HandlerInterceptor拦截器packagecom.jxdinfo.hussar.sys.interceptor;importcom.jxdinfo.hussar.base.entity.UserInfo;importcom.jxdinfo.hussar.common.util.UserUtils;importcom.jxdinfo.hussar.core.util......
  • 【Elasticsearch】在spring环境中 进行es的数据读取
    在Spring环境中进行Elasticsearch(ES)的数据读取,通常会利用SpringDataElasticsearch项目提供的功能。SpringDataElasticsearch提供了高度抽象的Repository接口,允许你以面向对象的方式操作Elasticsearch,而无需直接编写底层的HTTP请求或JSON解析代码。下面是一个简单的示例,演示如......
  • SpringMVC学习总结 + 【手写SpringMVC底层机制核心】
    SpringMVC笔记SpringMVC介绍基本介绍SpringMVC是WEB层框架,接管了Web层组件,支持MVC的开发模式/开发架构SpringMVC通过注解,让POJO成为控制器,不需要继承类或者实现接口SpringMVC采用低耦合的组件设计方式,具有更好扩展和灵活性.支持REST格式的URL请求.SpringMV......