SpringCould (三) Zuul 负载均衡实现

Zuul 自带的Ribbon负载均衡,在项目配置默认开启

首先创建好1个服务中心

两个客户端

服务中心和客户端创建:https://blog.csdn.net/qq_37203082/article/details/111031105

两个客户端的配置的服务ID要相同

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8889
spring:
  application:
    name: order-server

测试接口Url也要相同,为了测试里面输出不同内容

客户端1

@RestController
public class TestOrderController {

    @RequestMapping("orderTest")
    public String orderTest(){
        return "this is service 1 order";
    }
}

 客户端2

@RestController
public class TestOrderController {

    @RequestMapping("orderTest")
    public String orderTest(){
        return "this is service 2 order";
    }
}

一个ZUUL网关

maven引用增加,其余引用跟客户端的一样

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

启动类,增加注解 @EnableZuulProxy

@SpringBootApplication
@EnableZuulProxy
public class SpringCloudZuulApplication {

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

}

配置文件

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8080
spring:
  application:
    name: zuul-server
zuul:
  routes:
    api-a:
      path: /server/**
      service-id: order-server

4个服务启动后效果

测试接口,同样的接口返回两个结果,代表了两个客户端都有进入到里面,默认使用的轮询策略,所以两个客户端轮流访问,形成两个节点

 

启动顺序:服务中心、zuul网关、客户端1、客户端2

后续的其他策略,再补充

猜你喜欢

转载自blog.csdn.net/qq_37203082/article/details/113482388