当前位置: 移动技术网 > IT编程>开发语言>Java > java架构之-负载均衡-Ribbon 的使用

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

2019年07月20日  | 移动技术网IT编程  | 我要评论

一、 什么是负载均衡
负载均衡就是分发请求流量到不同的服务器。
负载均衡一般分为两种:
1、 服务器端负载均衡(nginx)

 

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

 

二、 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视频教程:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网