spring Cloud-Ribbon实现客户端的负载均衡

基本的Ribbon实现

负载均衡是为了满足访问量增加,单个服务无法满足承载的情况。

****原来的Eureka client 端口为8762,Eureka Service 端口为8761****

只需要复制Hello world服务,同时将原来的端口8762修改为8763。然后启动这两个Spring Boot应用, 就可以得到两个Hello World服务。

这两个Hello world都注册到了eureka服务中心。这时候再访问http://localhost:8761, 可以看到两个hello world服务已经注册。

(服务与注册参见Spring Cloud 入门教程(一): 服务注册)。

Spring 提供两辆种服务调度方式:Ribbon+restful和Feign。Ribbon就是一个基于客户端的负载均衡器, Ribbon提供了很多在HTTP和TCP客户端之上的控制. 

Feign内部也已经使用了Ribbon, 所以只要使用了@FeignClient注解,那么这一章的内容也都是适用的。 

hello world服务和ribbon均注册到服务中心

service-hi工程跑了两个副本,端口分别为8762,8763,分别向服务注册中心注册, 当sercvice-ribbon通过restTemplate调用service-Hellowworld的接口时,利用用ribbon进行负载均衡,会轮流的调用处于两个不同端口的Hello world服务

 2. 创建一个Ribbon服务

1) 创建一个maven工程,取名叫service-ribbon,、

2) 创建主类ServiceRibbonApplication

package springcloud.helloworld.ribbon.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {  
        return new RestTemplate();
    }
}

@EnableDiscoveryClient向服务中心注册,并且注册了一个叫restTemplate的bean。

@LoadBalanced注册表明,这个restRemplate是需要做负载均衡的。

 3). 创建获取一个获取Hello内容的service类

package springcloud.helloworld.ribbon.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {
    @Autowired 
   RestTemplate restTemplate;
public String getHelloContent() { return restTemplate.getForObject("http://SERVICE-HELLOWORLD/",String.class);//http get方法
               //
postForObject // http post方法

}
}

 restTemplate.getForObject方法会通过ribbon负载均衡机制, 自动选择一个Hello word服务(这个服务是eureka中的服务名),

这里的URL是“http://SERVICE-HELLOWORLD/",其中的SERVICE-HELLOWORLD是Hello world服务的名字,而注册到服务中心的有两个SERVICE-HELLOWORLD。 所以,这个调用本质是ribbon-service作为客户端根据负载均衡算法自主选择了一个作为服务端的SERVICE-HELLOWORLD服务。然后再访问选中的SERVICE-HELLOWORLD来执行真正的Hello world调用。

同时使用负载均衡和指定IP端口的调用

//注入负载均衡的调用方法
    @Autowired
    @LoadBalanced
    private RestTemplate restTemplat;
//注入IP+端口的调用方法
    @Autowired
    private RestTemplate restTemplateByIp;


    /**
     * 负载均衡post 方法调用开通中心接口工程
     * @param url
     * @param entity
     * @return
     */
    public String post2IomCloudInfService(String url, HttpEntity<String> entity) {
        return restTemplat.postForObject("http://IOM-CLOUD-INF-SERVICE"+url, entity, String.class);
    }


    /**
     * post 方法直接用IP和端口调用外系统
     * @param url
     * @param entity
     * @return
     */
    public String postByIpAndPort(String url, HttpEntity<String> entity) {
        return restTemplateByIp.postForObject(url, entity, String.class);
    }
原文地址:https://www.cnblogs.com/linhongwenBlog/p/8706590.html