spring cloud gateway 负载均衡


spring cloud gateway 负载均衡

 

*****************************

网关应用

 

**********************

配置文件

 

application.yml

spring:
  application:
    name: hello-gateway
  cloud:
    consul:
      host: 172.18.0.20
      port: 8500
    gateway:
      routes:
        - id: loadbalance
          uri: lb://hello-service
          predicates:
            - Path=/hello-service/hello
          filters:
            - StripPrefix=1

 

 

*****************************

服务提供方

 

**********************

配置文件

 

application.yml

spring:
  application:
    name: hello-service
  cloud:
    consul:
      host: 172.18.0.20
      port: 8500
      discovery:
        instance-id: ${spring.application.name}-${random.int}
        tags: version-1

 

**********************

controller层

 

同名hello-service应用1

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello world";
    }
}

 

同名hello-service应用2

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello world2";
    }
}

 

 

*********************

测试输出

 

        spring cloud gateway 负载均衡_第1张图片

说明:同名应用hello-service轮流被调用

 

 

你可能感兴趣的:(spring,cloud)