博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java springcloud B2B2C o2o多用户商城 springcloud架构- ribbon
阅读量:5799 次
发布时间:2019-06-18

本文共 5115 字,大约阅读时间需要 17 分钟。

ribbon用以实现负载均衡;实现软负载均衡,核心有三点:

需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 一零三八七七四六二六

服务发现,发现依赖服务的列表

服务选择规则,在多个服务中如何选择一个有效服务

服务监听,检测失效的服务,高效剔除失效服务

netflix ribbon

一个简单的demo

配置文件:

# Max number of retries on the same server (excluding the first try)sample-client.ribbon.MaxAutoRetries=1# Max number of next servers to retry (excluding the first server)sample-client.ribbon.MaxAutoRetriesNextServer=1# Whether all operations can be retried for this clientsample-client.ribbon.OkToRetryOnAllOperations=true# Interval to refresh the server list from the sourcesample-client.ribbon.ServerListRefreshInterval=2000# Connect timeout used by Apache HttpClientsample-client.ribbon.ConnectTimeout=3000# Read timeout used by Apache HttpClientsample-client.ribbon.ReadTimeout=3000# Initial list of servers, can be changed via Archaius dynamic property at runtimesample-client.ribbon.listOfServers=www.u51.xin:80,www.baidu.com:80,www.163.com:80,www.csdn.net:80复制代码

配置格式为 clientName.spaceName.params

核心配置sample-client.ribbon.listOfServers 表明有效的服务列表

BasicTest.javapackage com.lkl.springcloud.ribbon;import com.netflix.client.ClientFactory;import com.netflix.client.http.HttpRequest;import com.netflix.client.http.HttpResponse;import com.netflix.config.ConfigurationManager;import com.netflix.loadbalancer.ZoneAwareLoadBalancer;import com.netflix.niws.client.http.RestClient;import java.net.URI;/** * Created by liaokailin on 16/5/6. */public class BasicTest {    public static void main(String[] args) throws Exception{        ConfigurationManager.loadPropertiesFromResources("sample-client.properties"); // 1        System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.listOfServers"));        RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client"); // 2        HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build(); // 3        for (int i = 0; i < 20; i++) {            HttpResponse response = client.executeWithLoadBalancer(request); // 4            System.out.println("Status code for " + response.getRequestedURI() + "  :" + response.getStatus());        }        ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();        System.out.println(lb.getLoadBalancerStats());        ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers",                "www.qq.com:80,www.u51.xin"); // 5        System.out.println("changing servers ...");        Thread.sleep(3000);         for (int i = 0; i < 20; i++) {            HttpResponse response = client.executeWithLoadBalancer(request);            System.out.println("Status code for " + response.getRequestedURI() + "  : " + response.getStatus());        }        System.out.println(lb.getLoadBalancerStats()); // 6    }}复制代码

1.通过archaius加载配置文件

2.通过ClientFactory获取指定名称client,在该方法中就获取了loadBalancer

3.构建请求

4.执行loadBalancer,会发现调用的服务会在listOfServers列表中的数据切换

5.修改可用服务列表

6.获取loadBalancer状态

spring cloud ribbon

archaius默认加载配置config.properties

ribbon配置信息

config.propertiesmyclient.ribbon.listOfServers=www.u51.xin:80,www.baidu.com:80,www.163.com:80,www.csdn.net:80application.properties 配置client名称ribbon.client.name=myclient复制代码

对应java类

package com.lkl.springcloud.ribbon;import com.netflix.loadbalancer.Server;import com.netflix.loadbalancer.ServerList;import com.netflix.loadbalancer.ZoneAwareLoadBalancer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.netflix.ribbon.RibbonClients;import org.springframework.cloud.netflix.ribbon.SpringClientFactory;import org.springframework.util.CollectionUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;/** * Created by liaokailin on 16/5/6. */@SpringBootApplication@RibbonClients@RestControllerpublic class RibbonDefaultConfigApplication {    @Autowired    private SpringClientFactory clientFactory;    @RequestMapping("/")    public void getServerList() throws Exception {        ZoneAwareLoadBalancer
lb = (ZoneAwareLoadBalancer
) clientFactory.getLoadBalancer("myclient"); ServerList
serverList = lb.getServerListImpl(); List
serverDetailList = serverList.getInitialListOfServers(); if (!CollectionUtils.isEmpty(serverDetailList)) { for (Server s : serverDetailList) { System.out.println(s.getHost() + "," + s.getPort()); } } else { System.out.println("no service"); } } public static void main(String[] args) { SpringApplication app = new SpringApplicationBuilder(RibbonDefaultConfigApplication.class).build(); app.run(args); }}复制代码

使用@RibbonClients注解

在getServerList方法中,首先获取ZoneAwareLoadBalancer,然后获取serverList,获取到serverList以后即可选择一个有效服务进行调用。

转载于:https://juejin.im/post/5c47c527e51d457d1b7f4f7e

你可能感兴趣的文章
Android开发教程 - 使用Data Binding(一) 介绍
查看>>
java B2B2C Springboot电子商城系统- SSO单点登录之OAuth2.0 登出流程(3)
查看>>
12月26日云栖精选夜读:CDN新品发布:阿里云SCDN安全加速开放公测
查看>>
USB 通信原理
查看>>
7zZip zip RAR iOS
查看>>
ssh无密码登陆远程主机
查看>>
date命令的详细用法!
查看>>
分布式存储ceph集群部署
查看>>
UiAutomator源码分析之UiAutomatorBridge框架
查看>>
python 开发之selenium
查看>>
Xcode3.2.5中找不到Mac OS X - Command Line Utility -...
查看>>
css的div垂直居中的方法,百分比div垂直居中
查看>>
如何理解EM算法
查看>>
nginx 域名跳转一例~~~(rewrite、proxy)
查看>>
我的友情链接
查看>>
linux用户家目录无损迁移到独立硬盘
查看>>
文件查找
查看>>
shell编程前言(一)
查看>>
5、centos7.*配置yum的EPEL源及其它源
查看>>
JSON前后台简单操作
查看>>