Ribbon--學習筆記(2)
目錄
一、參考spring cloud的官方文檔
--1、客戶端負載平衡器:Ribbon
--2、如何加入Ribbon
--3、自定義Ribbon客戶端
--4、使用屬性自定義Ribbon客戶端
--5、如何使用Ribbon不使用Eureka
--6、在Ribbon中禁用Eureka使用
二、實操
--1、在這個示例中有三個角色:注冊中心、服務提供方、服務消費方
--2、消費方是如何配置的
--3、啟動的效果
--4、原理圖
一、參考spring cloud的官方文檔
1、客戶端負載平衡器:Ribbon
Ribbon是一個客戶端負載均衡器,它可以很好地控制HTTP和TCP客戶端的行為。Feign已經使用Ribbon,所以如果您使用@FeignClient
,則本節也適用。
Ribbon中的中心概念是指定客戶端的概念。每個負載平衡器是組合的組合的一部分,它們一起工作以根據需要聯系遠程服務器,并且集合具有您將其作為應用程序開發人員(例如使用@FeignClient
注釋)的名稱。Spring Cloud使用RibbonClientConfiguration
為每個命名的客戶端根據需要創建一個新的合奏作為ApplicationContext
。這包含(除其他外)ILoadBalancer
,RestClient
和ServerListFilter
。
2、如何加入Ribbon
要在項目中包含Ribbon,請使用組org.springframework.cloud
和工件ID spring-cloud-starter-ribbon
的起始器。
3、自定義Ribbon客戶端
您可以使用<client>.ribbon.*
中的外部屬性來配置Ribbon客戶端的某些位,這與使用Netflix API本身沒有什么不同,只能使用Spring Boot配置文件。本機選項可以在CommonClientConfigKey
(功能區內核心部分)中作為靜態字段進行檢查。
Spring Cloud還允許您通過使用@RibbonClient
聲明其他配置(位于RibbonClientConfiguration
之上)來完全控制客戶端。例:
@Configuration
@RibbonClient(name = "foo", configuration = FooConfiguration.class)
public class TestConfiguration {
}
在這種情況下,客戶端由RibbonClientConfiguration
中已經存在的組件與FooConfiguration
中的任何組件組成(后者通常會覆蓋前者)。
警告:
FooConfiguration
必須是@Configuration
,但請注意,它不在主應用程序上下文的@ComponentScan
中,否則將由所有@RibbonClients
共享。如果您使用@ComponentScan
(或@SpringBootApplication
),則需要采取措施避免包含(例如將其放在一個單獨的,不重疊的包中,或者指定要在@ComponentScan
)。
Spring Cloud Netflix默認情況下為Ribbon(BeanType
beanName:ClassName
)提供以下bean:
IClientConfig
ribbonClientConfig:DefaultClientConfigImpl
IRule
ribbonRule:ZoneAvoidanceRule
IPing
ribbonPing:NoOpPing
ServerList<Server>
ribbonServerList:ConfigurationBasedServerList
ServerListFilter<Server>
ribbonServerListFilter:ZonePreferenceServerListFilter
ILoadBalancer
ribbonLoadBalancer:ZoneAwareLoadBalancer
ServerListUpdater
ribbonServerListUpdater:PollingServerListUpdater
創建一個類型的bean并將其放置在@RibbonClient
配置(例如上面的FooConfiguration
)中)允許您覆蓋所描述的每個bean。例:
@Configuration
public class FooConfiguration {
@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}
}
這用PingUrl
代替NoOpPing
。
4、使用屬性自定義Ribbon客戶端
從版本1.2.0開始,Spring Cloud Netflix現在支持使用屬性與Ribbon文檔兼容來自定義Ribbon客戶端。
這允許您在不同環境中更改啟動時的行為。
支持的屬性如下所示,應以<clientName>.ribbon.
為前綴:
NFLoadBalancerClassName
:應實施ILoadBalancer
NFLoadBalancerRuleClassName
:應實施IRule
NFLoadBalancerPingClassName
:應實施IPing
NIWSServerListClassName
:應實施ServerList
NIWSServerListFilterClassName
應實施ServerListFilter
注意:
在這些屬性中定義的類優先于使用@RibbonClient(configuration=MyRibbonConfig.class)
定義的bean和由Spring Cloud Netflix提供的默認值。
要設置服務名稱users
的IRule
,您可以設置以下內容:
application.yml
users:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
5、如何使用Ribbon不使用Eureka
Eureka是一種方便的方式來抽象遠程服務器的發現,因此您不必在客戶端中對其URL進行硬編碼,但如果您不想使用它,Ribbon和Feign仍然是適用的。假設您已經為“商店”申請了@RibbonClient,并且Eureka未被使用(甚至不在類路徑上)。Ribbon客戶端默認為已配置的服務器列表,您可以提供這樣的配置
application.yml
stores:
ribbon:
listOfServers: example.com,google.com
6、在Ribbon中禁用Eureka使用
設置屬性ribbon.eureka.enabled = false將明確禁用在Ribbon中使用Eureka。
application.yml
ribbon:
eureka:
enabled: false
二、實操
1、在這個示例中有三個角色:注冊中心、服務提供方、服務消費方
- 注冊中心是eureka-demo-server(參考 Eureka--學習筆記(1))
- 服務提供方是eureka-demo-client(參考 Eureka--學習筆記(1))
- 消費方是ribbon-demo
2、消費方是如何配置的
pom.xml
<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>
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8082
spring:
application:
name: ribbon-demo-consumer
啟動類
@SpringBootApplication
public class RibbonDemoApplication {
@Bean
@LoadBalanced //負載均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonDemoApplication.class, args);
}
}
調用類
@RestController
@RequestMapping("/call")
public class CallController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello")
public String hello(){
return restTemplate.getForObject("http://EUREKA-DEMO-CLIENT-1/",String.class);
}
@GetMapping("/hi/{name}")
public String hi(@PathVariable("name") String name){
return restTemplate.getForObject("http://EUREKA-DEMO-CLIENT-1/test/say/hello/" + name,String.class);
}
}
Ps:提供者(eureka-demo-client)的調用類參考
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("say/hello/{name}")
public String sayHello(@PathVariable("name") String name ){
return "hello " + name;
}
}
3、啟動的效果
UI界面顯示了提供者和消費者
瀏覽器中輸入http://localhost:8082/call/hello/
瀏覽器中輸入http://localhost:8082/call/hi/caowo