栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud学习笔记(Ribbon)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud学习笔记(Ribbon)

Spring Cloud学习笔记(Ribbon)
  • 什么负载均衡
  • Ribbon
  • 第一个Ribbon实例

什么负载均衡

负载均衡是高可用网络基础架构的一个关键组成部分,有了负载均衡,我们可以通常可以部署多台应用服务器,然后通过负载均衡将用户的请求分发到不同的服务器用来提高网站、应用、数据库或其他服务性能以及可靠性。
负载均衡分为硬件负载均衡和软件负载均衡两种,具体介绍如下:
(1)硬件负载均衡的解决方案就是直接在服务器和外部网络间安装负载均衡设备,通常这种设备称为负载均衡器。
(2)软件负载均衡器的解决方案是指在一台或多台服务器相应的操作系统上安装一个或多个附加软件来实现负载均衡。

Ribbon

Ribbon作为服务消费者的负载均衡器,有两种使用方式,一种是与RestTemplate相结合,另一种是与Feign相结合,Feign已经默认继承了Ribbon。
Ribbon包含了许多子模块:
(1)ribbon-core:包括定义负载均衡接口、客户端接口、内置负载均衡实现接口等的API
(2)ribbon-eureka:提供Eureka客户端负载均衡的API
(3)ribbon-httpclient:对Apache的HttpClient进行封装,该模块提供了含有负载均衡功能的REST的客户端。

第一个Ribbon实例

1.改造服务提供者
修改eureka-provider和eureka-provider-another中新建controller包,

package comeurekaprovideranother.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PortController {
    @Value("${server.port}")
    String port;
    @RequestMapping("port")
    public String getPort(){
        return "hello word,I'm from port:"+port;
    }
}

2.搭建含有Ribbon服务消费者
创建一个名称为eureka-ribbon-client的Spring Boot项目,这里将有Group命名为com.itheima,添加EurekaClient、Ribbon和Web依赖
在全局配置文件application.yml中进行相关配置文件,包括指定应用名称、端口号、服务器注册地址等信息

spring:
  application:
    name: eureka-ribbon-client
server:
  port: 8674
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7070/eureka/

在项目启动类中激活Eureka Client的相关配置,在启动类中加入@EnableEurekaClient
创建配置类新建config包,并在该包下创建RibbonConfig类,在类中注入restTemplate的Bean对象,并在Bean对象中加上@LoadBalanceed注解。

package com.itheima.eurekaribbonclient.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RibbonConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        return builder.build();
    }
}

创建Service类,新建com.itheima.eurekaaribbonclient下新建service包,并在该包下创建RibbonService类。

package com.itheima.eurekaribbonclient.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class RibbonService {
    @Autowired
    RestTemplate restTemplate;
    public String hi(){
        return  restTemplate.getForObject("http://eureka-provider/port",String.class);
    }
}

创建Controller类,在com.itheima.eurekaaribbonclient下新建controller的包,在该包下新建一个RibbonController的类。

package com.itheima.eurekaribbonclient.Controller;

import com.itheima.eurekaribbonclient.Service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RibbonController {
    @Autowired
    RibbonService ribbonService;
    @RequestMapping("/hi")
    public String hi(){
        return ribbonService.hi();
    }
}

3.测试运行
启动服务器eureka-server和eureka-server-another。
访问:http://server2:7009/和http://server1:7000/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032755.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号