Ribbon实现前端负载均衡(本地路由导向)-加在消费者端

1配置消费者路径能支持通用访问
如用provider-user替换localhost:7900

package com.spoon.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.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {
    @Autowired//通过模板对象远程发起对另外一个服务器请求,结构和httpClient相似
    private RestTemplate restTemplate;
    
    //调用服务提供者
    @RequestMapping("/hello/{name}")
    public String welcome(@PathVariable String name) {
        //通过Eureka获取链接信息,而且实现负载均衡,默认:轮询
        String url="http://provider-user/hello/"+name;  //服务提供者访问地址
        return restTemplate.getForObject(url, String.class);
    }
}

2配置要让这个application.name进行解析,到Eureka换成对应地址

package com.spoon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class RunAppConsumerClient {
    //让spring创建模板对象
    @Bean
    //支持负载均衡
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(RunAppConsumerClient.class, args);
    }
}

ribbon和nginx比较
1)nginx(外网转内网,没有进行业务加工)基于C语言,快速,性能高达5w/s
redis 5w/s,RibbitMQ (进行了业务加工)1.2w.s,ApacheActiveMQ 0.6w/s
kafuka 20w/s~50w/s大数据用(可以丢失数据,大数据取的是近似值)
Zuul2.0 200w/s
负载均衡、反向代理、代理后端服务器。隐藏真实地址,防火墙设置真实地址不能外网直接访问
2)Ribbon负载均衡,前端,客户端开始导向(转向)


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容