1、pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--restTemplate要用到-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<!--客户端只有当我们有了这个依赖之后,才能有那些状态页面的查看,否则会报ErrorPage-->
</dependency>
2、application.yml
spring:
application:
name: eureka-client-consumer-ribbon
server:
port: 8601
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3、最基本的Ribbon访问
(1)暴露的服务
@RestController
public class BasicGetService {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/getProviderPort")
public String getProviderPort(){
String s = restTemplate.getForObject("http://eureka-client-provider/getServerPort", String.class);
System.out.println(s);
return s;
}
}
(2)启动类
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientConsumerRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientConsumerRibbonApplication.class, args);
}
@Bean
@LoadBalanced
//通过这个注解,就是用到了ribbon
RestTemplate restTemplate(){
return new RestTemplate();
}
}
(3)访问结果
将eureka-client-provider分别在两个端口进行启动,多次访问发现返回值为轮询规则,两个端口轮询调用
4、不使用默认的Ribbon,修改Ribbon的规则
(1)自定义一个Ribbon客户端,客户端的配置在FooConfiguration类中进行定义
@Configuration
@RibbonClient(name = "eureka-client-provider", configuration = FooConfiguration.class)
public class TestConfiguration {
}
(2)FooConfiguration 的定义
@Configuration
@ExcludeFromComponentScan
public class FooConfiguration {
@Bean
public IRule ribbonRule(){
return new RandomRule();
}
}
(3)FooConfiguration
必须是@Configuration
,但请注意,它不在主应用程序上下文的@ComponentScan
中,否则将由所有@RibbonClients
共享。
因此有下面的解决方案,在FooConfiguration的同级目录下,定义这个注释
public @interface ExcludeFromComponentScan {
}
(4)并且在启动类中
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value = ExcludeFromComponentScan.class)})
public class EurekaClientConsumerRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientConsumerRibbonApplication.class, args);
}
@Bean
@LoadBalanced
//通过这个注解,就是用到了ribbon
RestTemplate restTemplate(){
return new RestTemplate();
}
}
即可
这样我们配置完成之后,我们所有的调用eureka-client-provider这个实例下的服务的时候,都会实现随机调用。
5、我们还可以使用配置文件进行更改Ribbon配置的操作
eureka-client-provider:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
配置文件配置的优先级高于Java代码配置的优先级
备注:Ribbon几大重要的配置:
-
IClientConfig
ribbonClientConfig:DefaultClientConfigImpl
-
IRule
ribbonRule:ZoneAvoidanceRule
-
IPing
ribbonPing:NoOpPing
-
ServerList<Server>
ribbonServerList:ConfigurationBasedServerList
-
ServerListFilter<Server>
ribbonServerListFilter:ZonePreferenceServerListFilter
-
ILoadBalancer
ribbonLoadBalancer:ZoneAwareLoadBalancer
-
ServerListUpdater
ribbonServerListUpdater:PollingServerListUpdater
6、单独使用Ribbon
示例:如何使用Ribbon不使用Eureka
Eureka是一种方便的方式来抽象远程服务器的发现,因此您不必在客户端中对其URL进行硬编码,但如果您不想使用它,Ribbon和Feign仍然是适用的。假设您已经为“商店”申请了@RibbonClient
,并且Eureka未被使用(甚至不在类路径上)。Ribbon客户端默认为已配置的服务器列表,您可以提供这样的配置
application.yml
<span style="color:rgba(0, 0, 0, 0.8)"><span style="color:rgba(0, 0, 0, 0.9)">stores:
ribbon:
listOfServers: example.com,google.com</span></span>
示例:在Ribbon中禁用Eureka使用
设置属性ribbon.eureka.enabled = false
将明确禁用在Ribbon中使用Eureka。
application.yml
<span style="color:rgba(0, 0, 0, 0.8)"><span style="color:rgba(0, 0, 0, 0.9)">ribbon:
eureka:
enabled: false</span></span>
直接使用Ribbon API
您也可以直接使用LoadBalancerClient
。例:
<span style="color:rgba(0, 0, 0, 0.8)"><span style="color:rgba(0, 0, 0, 0.9)"><code class="language-java">public class MyClass {
@Autowired
private LoadBalancerClient loadBalancer;
public void doStuff() {
ServiceInstance instance = loadBalancer.choose("stores");
URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));
// ... do something with the URI
}
}</code></span></span>