javascript
SpringCloud学习笔记(6)----Spring Cloud Netflix之负载均衡-Ribbon的使用
1. 什么是負(fù)載均衡?
負(fù)載均衡,就是分發(fā)請(qǐng)求流量到不同的服務(wù)器。
負(fù)載均衡一般分為兩種
1. 服務(wù)器端負(fù)載均衡(nginx)
2. 客戶端負(fù)載均衡(Ribbon)
2. 服務(wù)提供者(spring-cloud-provider)
實(shí)體類User:
package com.wangx.cloud.model;import java.util.Date; public class User { private Integer id; private String name; private Date date; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", date=" + date + '}'; } }Controller接口:
package com.wangx.cloud.controller;import com.wangx.cloud.model.User; 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 java.util.Date; @RestController @RequestMapping("/api/user") public class UserController { @RequestMapping(value = "/{id}", method = RequestMethod.GET) public User view(@PathVariable int id) { User user = new User(); user.setId(id); user.setName("小張"); user.setDate(new Date()); System.out.println(user); return user; } }3. 服務(wù)消費(fèi)者(spring-cloud-consumer)
package com.wangx.cloud.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; @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); } }
分別查看兩個(gè)服務(wù)是否能正常調(diào)用。
4. 如何通過Ribbon進(jìn)行調(diào)用
1. 在創(chuàng)建RestTemplate bean的方法上添加注解@LoadBalanced
@Bean@LoadBalanced //默認(rèn)的負(fù)載策略是輪詢算法public RestTemplate restTemplate() {return new RestTemplate(); }
2. 修改調(diào)用的URL為URL=http://spring-cloud-provider/api/user/{id}
注意:控制臺(tái)的應(yīng)用名為大寫,我們統(tǒng)一為小寫,更不能大小寫都存在
5. 如何實(shí)現(xiàn)負(fù)載均衡
啟動(dòng)多個(gè)提供者,進(jìn)行測試
為了區(qū)分,可以修改實(shí)體類User的name屬性,
注意:修改應(yīng)用的時(shí)候,端口也要修改
?6. 結(jié)論
1. Ribbon通過@LoadBalanced進(jìn)行負(fù)載均衡
2.? 默認(rèn)的負(fù)載策略是輪詢算法
原文 SpringCloud學(xué)習(xí)筆記(6)----Spring Cloud Netflix之負(fù)載均衡-Ribbon的使用
轉(zhuǎn)載于:https://www.cnblogs.com/xiaoshen666/p/10844165.html
總結(jié)
以上是生活随笔為你收集整理的SpringCloud学习笔记(6)----Spring Cloud Netflix之负载均衡-Ribbon的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java实现回形数,只利用数组、循环和i
- 下一篇: SpringBoot基于易邮邮件服务器实