SpringCloud:Spring Cloud Gateway中如何配置LoadBalancer

要在Spring-Cloud-Gateway中配置LoadBalancer,需要用到spring-cloud-starter-netflix-ribbon,因此需要将它引入。

pom.xml:


    UTF-8
    UTF-8
    1.8
    2.2.1.RELEASE


    org.springframework.boot
    spring-boot-starter-parent
    2.2.1.RELEASE
    


    
        org.springframework.boot
        spring-boot-starter
    

    
        org.springframework.boot
        spring-boot-starter-webflux
    

    
        org.springframework.boot
        spring-boot-starter-actuator
    

    
        org.springframework.cloud
        spring-cloud-starter
        ${spring-cloud.version}
    

    
        org.springframework.cloud
        spring-cloud-starter-gateway
        ${spring-cloud.version}
    

    
        org.springframework.cloud
        spring-cloud-starter-netflix-ribbon
        ${spring-cloud.version}
    

 java代码:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AppBoot {
	
	public static void main(String[] args) {
		SpringApplication.run(AppBoot.class, args);
	}
}

application.properties:

server.port=8080
spring.application.name=gateway_server

 # default true
management.endpoint.gateway.enabled=true
management.endpoints.web.exposure.include=*

logging.level.org.springframework.cloud.gateway=TRACE

 # default true
ribbon.eureka.enabled=false

 # default true
spring.cloud.loadbalancer.ribbon.enabled=true
 # default false
spring.cloud.gateway.actuator.verbose.enabled=true

########################################################################
spring.cloud.gateway.routes[0].id=my_route
spring.cloud.gateway.routes[0].uri=lb://my-load-balanced-service
spring.cloud.gateway.routes[0].predicates[0]=Path=/test/**

my-load-balanced-service.ribbon.listOfServers=localhost:9090, 127.0.0.1:9090
my-load-balanced-service.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule

或者application.yaml:

server:
  port: 8080

spring:
  application:
    name: gateway_server

spring:
  cloud:
    gateway:
      routes:
        - id: my_route
          uri: lb://my-load-balanced-service
          predicates:
            - Path=/test/**

my-load-balanced-service:
  ribbon:
    listOfServers: localhost:9090, 127.0.0.1:9090
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule

可以从com.netflix.client.config.CommonClientConfigKey这类中找到其它可配置的参数。

注意:lb://后面的一定不要使用下划线,即不要使用比如:my_load_balanced_service这种形式,否则LoadBanancer不会起作用。

NFLoadBalancerRuleClassName中可以配置的值:

SpringCloud:Spring Cloud Gateway中如何配置LoadBalancer_第1张图片

其它可配置项:

.ribbon.NFLoadBalancerClassName: ILoadBalancer
.ribbon.NFLoadBalancerRuleClassName: IRule
.ribbon.NFLoadBalancerPingClassName: IPing
.ribbon.NIWSServerListClassName: ServerList
.ribbon.NIWSServerListFilterClassName: ServerListFilter

下表中列出了Spring Cloud Netflix为Ribbon提供的默认值:

Bean Type Bean Name Class Name
IClientConfig ribbonClientConfig DefaultClientConfigImpl
IRule ribbonRule ZoneAvoidanceRule
IPing ribbonPing DummyPing
ServerList ribbonServerList ConfigurationBasedServerList
ServerListFilter ribbonServerListFilter ZonePreferenceServerListFilter
ILoadBalancer ribbonLoadBalancer ZoneAwareLoadBalancer
ServerListUpdater ribbonServerListUpdater PollingServerListUpdater

如果创建了以上类型的一个bean,并将它们放入使用了@RibbonClient的一个配置中,默认值将会被覆盖。

完成以上工作后,通过访问比如:http://127.0.0.1:8080/test/test.jsp,LoadBalancer就会将请求转发到http://127.0.0.1:9090/test/test.jsp,或http://localhost:9090/test/test.jsp

 

参考文档

Spring Cloud Gateway:The LoadBalancerClient Filter
https://cloud.spring.io/spring-cloud-gateway/reference/html/#the-loadbalancerclient-filter

Spring Cloud Netflix: Client Side Load Balancer: Ribbon
https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.1.RELEASE/reference/html/#spring-cloud-ribbon

Working with load balancers
https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers#components-of-load-balancer

Client Side Load Balancing with Ribbon and Spring Cloud
https://spring.io/guides/gs/client-side-load-balancing/

spring-cloud-gateway负载普通web项目
https://www.cnblogs.com/bener/p/10638814.html

 

 

 

你可能感兴趣的:(Java,Spring,SpringCloud)