- GET请求方式
● <T> T getForObject(String url, Class<T> responseType, Object... uriVariables);
● <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables);
● <T> T getForObject(URI url, Class<T> responseType);
● <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables);
● <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables);
● <T> ResponseEntity<T> getForEntity(URI var1, Class<T> responseType);
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables);
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables);
<T> T postForObject(URI url, @Nullable Object request, Class<T> responseType);
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables);
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables);
<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType);
GET http://localhost:80/consumer/payment/getObj/1
HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 13 Sep 2023 15:01:40 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 200,
"message": "successful query data to id =1 from port number : 8001",
"data": {
"id": 1,
"serial": "ljzTest01"
}
}
Response code: 200; Time: 1057ms; Content length: 116 bytes
GET http://localhost:80/consumer/payment/getEntity/1
HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 13 Sep 2023 15:04:05 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 200,
"message": "successful query data to id =1 from port number : 8002",
"data": {
"id": 1,
"serial": "ljzTest01"
}
}
Response code: 200; Time: 204ms; Content length: 116 bytes
官方文档明确给出了警告:
top.ljzstudy.myrule
package top.ljzstudy.myrule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MySelfRule{
@Bean
public IRule myRule(){
return new RandomRule();//定义为随机
}
}
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration= MySelfRule.class)
package top.ljzstudy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import top.ljzstudy.myrule.MySelfRule;
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration= MySelfRule.class) //使用自定义负载均衡策略
public class MainApp80{
public static void main(String[] args){
SpringApplication.run(MainApp80.class,args);
}
}
GET http://localhost:80/consumer/payment/getObj/1
HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 13 Sep 2023 15:29:05 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 200,
"message": "successful query data to id =1 from port number : 8001",
"data": {
"id": 1,
"serial": "ljzTest01"
}
}
Response code: 200; Time: 92ms; Content length: 116 bytes
负载均衡算法:rest接口第几次请求数 % 服务器集群总数量 = 实际调用服务器位置下标 ,每次服务重启动后rest接口计数从1开始。
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
如: List [0] instances = 127.0.0.1:8002
List [1] instances = 127.0.0.1:8001
8001+ 8002 组合成为集群,它们共计2台机器,集群总数为2, 按照轮询算法原理:
当总请求数为1时: 1 % 2 =1 对应下标位置为1 ,则获得服务地址为127.0.0.1:8001
当总请求数位2时: 2 % 2 =0 对应下标位置为0 ,则获得服务地址为127.0.0.1:8002
当总请求数位3时: 3 % 2 =1 对应下标位置为1 ,则获得服务地址为127.0.0.1:8001
当总请求数位4时: 4 % 2 =0 对应下标位置为0 ,则获得服务地址为127.0.0.1:8002
如此类推......
//添加访问服务端口方法,用于校验负载均衡器效果
@GetMapping(value = "/payment/lb")
public String getPaymentLB(){
return serverPort;
}
package top.ljzstudy.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
//@LoadBalanced//使用LoadBalance注解赋予RestTemplate负载均衡能力
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
package top.ljzstudy.springcloud.lb;
import org.springframework.cloud.client.ServiceInstance;
import java.util.List;
public interface LoadBalancer {
ServiceInstance instances(List<ServiceInstance> serviceInstances);
}
package top.ljzstudy.springcloud.lb.impl;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;
import top.ljzstudy.springcloud.lb.LoadBalancer;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@Component
public class MyLoadBalancerImpl implements LoadBalancer {
private AtomicInteger atomicInteger = new AtomicInteger(0);
public final int getAndIncrement() {
int current;
int next;
do {
current = this.atomicInteger.get();
next = current >= 2147483647 ? 0 : current + 1;
} while (!this.atomicInteger.compareAndSet(current, next));
System.out.println("*****next: " + next);
return next;
}
@Override
public ServiceInstance instances(List<ServiceInstance> serviceInstances) {
int index = getAndIncrement() % serviceInstances.size();
return serviceInstances.get(index);
}
}
@Resource
private DiscoveryClient discoveryClient;
@Resource
private LoadBalancer loadBalancer;
@GetMapping("/consumer/payment/lb")
public String getPaymentLB()
{
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
if(instances == null || instances.size()<=0) {
return null;
}
ServiceInstance serviceInstance = loadBalancer.instances(instances);
URI uri = serviceInstance.getUri();
return restTemplate.getForObject(uri+"/payment/lb",String.class);
}
GET http://localhost:80/consumer/payment/lb
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 4
Date: Wed, 13 Sep 2023 16:47:39 GMT
Keep-Alive: timeout=60
Connection: keep-alive
8001
Response code: 200; Time: 78ms; Content length: 4 bytes
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有