java负载均衡框架_java架构之-负载均衡-Ribbon 的使用

本文介绍了Java负载均衡的基本概念,包括服务器端和客户端负载均衡,并详细讲解了如何在Spring Cloud中使用Ribbon进行客户端负载均衡。通过创建服务提供者和消费者,展示了RestTemplate结合@LoadBalanced进行调用的过程。文中强调了通过启动多个服务实例,Ribbon会采用轮询算法实现负载均衡,并提供了相关视频教程链接。
摘要由CSDN通过智能技术生成

一、 什么是负载均衡

负载均衡就是分发请求流量到不同的服务器。

负载均衡一般分为两种:

1、 服务器端负载均衡(nginx)

8c3b1d428821852c1f729b1d25352d19.png

2、 客户端负载均衡(Ribbon)

1a966c990a50c5fe46f3cb8658acf4c5.png

二、 spring- - cloud- - provide) (服务提供者) :

实体类 :

package com.roncoo.education.bean;

import java.util.Date;

/**

* 实体类

*

* @author wujing

*/

public class User {

private int id;

private String name;

private Date createTime;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

@Override

public String toString() {

return "RoncooUser [id=" + id + ", name=" + name + ",

createTime=" + createTime + "]";

}

}

接口 类:

package com.roncoo.education.controller;

import java.util.Date;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import com.roncoo.education.bean.User;

/**

* @author wujing

*/

@RestController

@RequestMapping(value = "/api/user")

public class ApiUserController {

protected final Logger logger =

LoggerFactory.getLogger(this.getClass());

@RequestMapping(value = "/{id}", method =

RequestMethod.GET)

public User view(@PathVariable int id) {

User user = new User();

user.setId(id);

user.setName("无境");

user.setCreateTime(new Date());

logger.info("请求接口返回:{}", user);

return user;

}

}

三、 spring- - cloud- - consumer( 服务 消费者) ) :

package com.roncoo.education.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

/**

* @author wujing

*/

@RestController

@RequestMapping(value = "/user", method = RequestMethod.POST)

public class UserController {

private static final String

URL="http://localhost:7777/api/user/{id}";

@Autowired

private RestTemplate restTemplate;

@RequestMapping(value = "/{id}", method =

RequestMethod.GET)

public String get(@PathVariable(value = "id") int id) {

return restTemplate.getForObject(URL, String.class, id);

}

}

使用RestTemplate进行调用,所以要先定义这个Bean

@Bean

protected RestTemplate restTemplate() {

return new RestTemplate();

}

四、 查看

provide : http://localhost:7777 /api/user/1

consumer: http://localhost:8888/user/1

五、 如何 通过 Ribbon 进行调用

1、 在 RestTemplate 中添加注解 @LoadBalanced

2、 修改调用的 URL=http://spring-cloud-provider/api/user/{id}

注意:控制台的应用名字为大写,我们统一为小写,更不能大小写都存在。

六、 如何实现负载均衡

我们把提供者的应用,启动多个,进行测试。

为了区分,我们修改实体的名字。

注意:修改应用的时候,端口也要修改。

七、 结论

1 1、 、 Ribbon 通过@LoadBalanced 进行负载均衡。

2 2、 、 默认的负载策略是轮询算法。

java视频教程:http://www.angelasp.com/news/20196231880.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值