SpringCloud學習三-zuul網關搭建

由於最後技術選型使用的是GateWay,所以zuul只是搭建的基本功能。

基本配置

1.依賴

   <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.配置文件

server:
  port: 8762
spring:
  application:
    name: zuul-server
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: law-server

3.啓動類

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

4.測試

啓動兩個服務law-server端口分別是8888 和9999
訪問 http://localhost:8762/api-a/hi?name=zuul 兩次,服務端返回數據如下:

Hi zuul, I am from port: 8888
Hi zuul, I am from port: 9999

這說明Zuul 起到了 路由 的作用。如果某個 服務 存在 多個實例,Zuul 會結合 Ribbon 做 負載均衡,將請求 均分並路由 到不同的 服務實例。
更多zuul相關配置參考文章:Spring Cloud實戰系列(五) - 服務網關Zuul
http://www.ityouknow.com/springcloud/2017/06/01/gateway-service-zuul.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章