SpringCloud:Spring Cloud Gateway中如何配置LoadBalancer

19 篇文章 0 订阅
10 篇文章 0 订阅

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

pom.xml:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>2.2.1.RELEASE</spring-cloud.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath />
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter</artifactId>
        <version>${spring-cloud.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
        <version>${spring-cloud.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        <version>${spring-cloud.version}</version>
    </dependency>

</dependencies>

 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://后面的<clientName>一定不要使用下划线,即不要使用比如:my_load_balanced_service这种形式,否则LoadBanancer不会起作用。

NFLoadBalancerRuleClassName中可以配置的值:

其它可配置项:

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

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

Bean TypeBean NameClass Name
IClientConfigribbonClientConfigDefaultClientConfigImpl
IRuleribbonRuleZoneAvoidanceRule
IPingribbonPingDummyPing
ServerList<Server>ribbonServerListConfigurationBasedServerList
ServerListFilter<Server>ribbonServerListFilterZonePreferenceServerListFilter
ILoadBalancerribbonLoadBalancerZoneAwareLoadBalancer
ServerListUpdaterribbonServerListUpdaterPollingServerListUpdater

如果创建了以上类型的一个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

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值