SpringCloud之Ribbon负载均衡[七]

SpringCloud之Ribbon负载均衡

什么是Ribbon

  Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随即连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。

  现在最流行的负载均衡分为软负载(nginx),和硬负载(如F5),但硬负载一般开发代价太贵,一个硬负载可能要几十万,这时候不得不考虑开发成本了,所以一般用的最多的都是软负载

  一般简单的项目是由用户直接访问服务经过一系列处理返回给客户想要的,而加入了负载均衡如图:

  

单独使用案例

  1.导入Gradle依赖

  

   compile group: 'com.netflix.ribbon', name: 'ribbon-core', version: '2.2.0'
    // https://mvnrepository.com/artifact/com.netflix.ribbon/ribbon-httpclient
    compile group: 'com.netflix.ribbon', name: 'ribbon-httpclient', version: '2.2.0'

 

  2.配置文件

# Max number of retries 最大重试次数
order.ribbon.MaxAutoRetries=1
#要重试的下一个服务器的最大数量(不包括第一个服务器)
# Max number of next servers to retry (excluding the first server)
order.ribbon.MaxAutoRetriesNextServer=1
# 是否可以对此客户端重新尝试所有操作
# Whether all operations can be retried for this client
order.ribbon.OkToRetryOnAllOperations=true
#间隔以从源刷新服务器列表。
# Interval to refresh the server list from the source
order.ribbon.ServerListRefreshInterval=2000
#Apachehttpclient使用的连接超时
# Connect timeout used by Apache HttpClient
order.ribbon.ConnectTimeout=3000
#读取Apache httpclient使用的超时
# Read timeout used by Apache HttpClient
order.ribbon.ReadTimeout=3000
#服务器的初始列表,可以在运行时通过Archaius动态属性进行更改。
# Initial list of servers, can be changed via Archaius dynamic property at runtime
order.ribbon.listOfServers=http://localhost:8001,http://localhost:8002

  3.代码循环模拟多用户

public class Test {
    public static void main(String[] args) throws Exception {
        //读取配置文件 通过配置管理.loadPropertiesFromResources方法进行加载
        ConfigurationManager.loadPropertiesFromResources("sample-client.properties");
        //输出配置文件sample-client.ribbon.listOfServers 测试配置文件引用是否成功
        System.out.println(ConfigurationManager.getConfigInstance().getProperty("order.ribbon.listOfServers"));
        //创建客户端负载均衡器 给定名称为 order
        RestClient client = (RestClient) ClientFactory.getNamedClient("order");
        //输入要访问的页面 调用orderService的select方法
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("/select")).build();
        for (int i = 0; i < 4; i++) {
           //用于负载均衡
            HttpResponse response = client.executeWithLoadBalancer(request);
            //将输入的内容给一个String类并输出
            System.out.println(response.getEntity(String.class));
            //显示调用的网页和调用网页后的状态
            System.out.println("Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus());
        }
    }
}

与SpringCloud集成项目案例:

  1.导入同上项目Gradle依赖

  2.代码编写

@SpringBootApplication
@EnableDiscoveryClient(autoRegister = false)
public class RibbonApplication {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(RibbonApplication.class,args);
    }
}
@RestController
public class RibbonTest {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/getuserinfo")
    public DtoClasses add() {
        return restTemplate.getForEntity("http://ORDER-SERVER/select", DtoClasses.class).getBody();
    }
}

以上就是SpringCloud集成Ribbon

源代码:https://github.com/zgc456/SpringCloud-Summary

里面包含ribbon zuul feign hystrix 等等只看Ribbon即可

 

posted on 2018-03-15 17:06  White_programmer  阅读(198)  评论(1编辑  收藏  举报

导航