
新建一个配置文件


准备好的配置文件
# Max number of retries on the same server (excluding the first try)
jiangzh-client.ribbon.MaxAutoRetries=1
# Max number of next servers to retry (excluding the first server)
jiangzh-client.ribbon.MaxAutoRetriesNextServer=1
# Whether all operations can be retried for this client
jiangzh-client.ribbon.OkToRetryOnAllOperations=true
# Interval to refresh the server list from the source
jiangzh-client.ribbon.ServerListRefreshInterval=2000
# Connect timeout used by Apache HttpClient
jiangzh-client.ribbon.ConnectTimeout=3000
# Read timeout used by Apache HttpClient
jiangzh-client.ribbon.ReadTimeout=3000
# Initial list of servers, can be changed via Archaius dynamic property at runtime
jiangzh-client.ribbon.listOfServers=www.baidu.com:80,www.jd.com:80
复制到新建的配置文件里

主要的是listOfServer.

创建包ribbon

在下面创建App的类

准备好的测试类

先把包都导入机那里。这些在官方的演示里面其实都有。

Ribbon官方文档有段时间没有更新了。它的官方文档和它的版本已经不一致了。
下面复制过来并且抛出了异常。

读取的是自己创建的配置文件

抛出URI的异常


执行测试

默认以这种轮询的方式在访问。


抛出异常

上面的代码先注释掉

for循环代码注释掉。保留RestClient和HttpRequest



把上面的代码放开,一起输出看下效果


下面是演示的动态修改serverList

很明显冬天修改服务的地址比固定的好。
动态修改也存在一个问题,serverList从哪里来。没有任何情况比从注册中来是更好的。

下面就看着正式环境Ribbon怎么和eureka做整合。
原文:https://www.cnblogs.com/wangjunwei/p/12776463.html