一、 什么是负载均衡
负载均衡就是分发请求流量到不同的服务器。
负载均衡一般分为两种:
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

最新文章

  1. XE2 泛型练习1
  2. C# dev开发过程中的山炮问题汇总
  3. Codeforces Round #257 (Div. 1) (Codeforces 449B)
  4. iOS开发几年了,你清楚OC中的这些东西么1
  5. Python函数式编程:内置函数map()使用说明
  6. jQuery第六章
  7. opencv-jni -调试出错taking address of temporary [-fpermissive]
  8. MATLAB绘图功能(2) 二维底层绘图修饰
  9. 在vue中使用Echarts画曲线图(异步加载数据)
  10. TCHAR用法
  11. 【poj3415】 Common Substrings
  12. linux分区满了,如何进行扩容
  13. kao shi
  14. --oracle删除数据库表(无主键)中重复的记录
  15. RenderScript多输入参数
  16. Dive into Spring framework -- 了解基本原理(二)--设计模式-part2
  17. 分布式爬虫搭建系列 之三---scrapy框架初用
  18. mfs教程(四)
  19. Java java.lang.Thread#join()方法分析
  20. shell exec命令执行shell打印输出到一个文件

热门文章

  1. 牛客NOIP暑期七天营-提高组5+普及组5
  2. CSS flex 布局快速入门
  3. Tomcat系列(二)- EndPoint源码解析
  4. Python爬虫实战:爬取腾讯视频的评论
  5. CDN原理加速解析
  6. FCC---CSS Flexbox: Apply the flex-direction Property to Create a Column in the Tweet Embed
  7. CentOS 7 安装 Nginx 配置反向代理
  8. centos7配置Memcached
  9. com.alibaba.fastjson和net.sf.json的区别
  10. PlayJava Day026