问题描述
我了解@LoadBalanced
表示Rest模板应该基于使用Ribbon的客户端负载平衡,并检查Eureka服务器是否将服务名称解析为主机/端口.
I understand @LoadBalanced
indicates the Rest template should be based on Client Side Load Balancing using Ribbon and checks Eureka server for resolving the service name to host/port.
@RibbonClient
的用途是什么.
推荐答案
TL; DR :@LoadBalanced
是标记注释& @RibbonClient
用于配置.
TL;DR: @LoadBalanced
is a marker annotation & @RibbonClient
is used for configuration purposes.
用作标记注释,指示带注释的RestTemplate
应该使用RibbonLoadBalancerClient
与您的服务进行交互.
Used as a marker annotation indicating that the annotated RestTemplate
should use a RibbonLoadBalancerClient
for interacting with your service(s).
这又使您可以对传递给RestTemplate
的URL使用逻辑标识符".这些逻辑标识符通常是服务的名称.例如:
In turn, this allows you to use "logical identifiers" for the URLs you pass to the RestTemplate
. These logical identifiers are typically the name of a service. For example:
restTemplate.getForObject("http://some-service-name/user/{id}", String.class, 1);
其中some-service-name
是逻辑标识符.
用于配置功能区客户端.
Used for configuring your Ribbon client(s).
需要@RibbonClient吗?
不!如果您正在使用服务发现,并且对所有默认的功能区设置都没问题,那么甚至不需要使用@RibbonClient
批注.
No! If you're using Service Discovery and you're ok with all of the default Ribbon settings, you don't even need to use the @RibbonClient
annotation.
我什么时候应该使用@RibbonClient
?
When should I use @RibbonClient
?
至少有两种情况需要使用@RibbonClient
There are at least two cases where you need to use @RibbonClient
- 您需要为特定的Ribbon用户端自定义功能区设置
- 您没有使用任何服务发现
自定义功能区设置:
Customizing your Ribbon settings:
定义一个@RibbonClient
@RibbonClient(name = "some-service", configuration = SomeServiceConfig.class)
-
name
-将其设置为与功能区调用的服务相同的名称,但需要其他自定义功能区以与功能区进行交互. -
configuration
-将其所有自定义定义为@Beans
的类设置为@Configuration
类.确保@ComponentScan
没有选择该类 ,否则它将覆盖所有Ribbon功能区客户端的默认设置. name
- set it to the same name of the service you're calling with Ribbon but need additional customizations for how Ribbon interacts with that service.configuration
- set it to an@Configuration
class with all of your customizations defined as@Beans
. Make sure this class is not picked up by@ComponentScan
otherwise it will override the defaults for ALL Ribbon clients.
请参阅Spring Cloud Netflix文档中的自定义RibbonClient"部分(链接)
See the section "Customizing the RibbonClient` in the Spring Cloud Netflix documentation (link)
使用没有服务发现的功能区
Using Ribbon without Service Discovery
如果您不使用服务发现,则@RibbonClient
批注的name
字段将用于在application.properties
中为您的配置加上前缀,并在您传递给.
If you're not using Service Discovery, the name
field of the @RibbonClient
annotation will be used to prefix your configuration in the application.properties
as well as "logical identifier" in the URL you pass to RestTemplate
.
定义@RibbonClient
@RibbonClient(name = "myservice")
然后在您的application.properties
myservice.ribbon.eureka.enabled=false
myservice.ribbon.listOfServers=http://localhost:5000, http://localhost:5001
这篇关于@RibbonClient和@LoadBalanced之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!