java架構之-負載均衡-Ribbon 的使用


一、 什么是負載均衡
負載均衡就是分發請求流量到不同的服務器。
負載均衡一般分為兩種:
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視頻教程:http://www.angelasp.com/news/20196231880.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM