SpringCloud(三):服务消费以及负载均衡(RestTemplate+Ribbon)

一、什么是Ribbon:

Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法。默认是使用轮询。

方法1:可以在springboot启动类之外定义一个配置类(@RibbonClient)配置对应的算法。

方法2:在配置文件yml中配置

相关链接:https://www.cnblogs.com/fengyuduke/p/10712569.html

怎么使用Ribbon实现负载均衡?

相关链接: https://www.cnblogs.com/xing-12/p/9889153.html

1.你要有两个服务,一个是服务消费方,一个是服务提供方,并且服务提供方要有两个实例,也就是xing-user有两个实例,分别运行在8070和8071端口。

2.所有服务注册到eureka server中。

3.通过注入bean:  用RestTemplate调用restTemplate.getForObject("http://xing-user/user/findByName/"+name, User.class)方法。

二、Eureka服务提供者集群

先启动上篇文章中的注册中心eureka-server:8001, 以及hello-service:8011,接着修改hello-service项目配置文件中的端口,改成8012,再次运行用户服务项目。

执行成功,查看Eureka注册中心,可以看到有用户服务应用有两个端口对应。

(如果是IDEA用户,需要修改Application.java的执行方式,默认是单实例运行,所以你在运行8011的项目,修改端口无法再次运行。)

三、RestTemplate+Ribbon消费者:  新建一个maven服务

 

 

 pom.xml:

复制代码
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
复制代码

application.properties:

spring.application.name=hello-consumer-ribbon
server.port=8021
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/spring.application.name=hello-consumer-ribbon
server.port=8021
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/

启动类:

复制代码
package cn.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class HelloConsumerRibbonApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate () {
        return new RestTemplate();
    }

    @Autowired
    private RestTemplate restTemplate;

    //获取服务实例,作用为之后console显示效果;"http://USER-SERVICE/hello?name= 标识注册的服务。USER-SERVICE在eureka注册的服务名
    @RequestMapping("hello")
    public ResponseEntity<String> hello (String name) {
        return restTemplate.getForEntity("http://USER-SERVICE/hello?name=" + name, String.class);
    }

}
复制代码

四、测试
测试服务消费
http://localhost:8021/hello?name=ribbon

测试负载均衡:
我们在hello-service hello方法中加上日志打印,然后再分别启动 hello-service:8012,8002,然后多次访问 http://localhost:8021/hello?name=ribbon,可以看到两个hello-service项目的控制台都有日志输出,表示实现了负载均衡。

使用RestTemplate+Ribbon必须指定调用服务名称,如上面的HELLO-SERVICE,为了方便使用,SpringCloud还集成了Feign消费方式。
————————————————

 

posted @   威兰达  阅读(484)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示