1. 程式人生 > 其它 >20202303 實驗二 《資料結構與面向物件程式設計》實驗報告

20202303 實驗二 《資料結構與面向物件程式設計》實驗報告

目錄

Zuul路由閘道器:

建立閘道器服務:

匯入依賴:

zuul依賴和Eureka依賴必須

<!--實體類+web-->
    <dependencies>
        <!--zuul依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--Eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--Hystrix依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--監控頁面依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--ribbon依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.stt</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--熱部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>

application.yml配置檔案:

server:
  port: 9527
# 服務名稱
spring:
  application:
    name: springcloud-zuul
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: zuul9527.com # 修改eureka上的預設描述資訊
    prefer-ip-address: true  # 隱藏地址
#info配置
info:
  app.name: stt-springcloud
  company.name: mazha.stt.com
  
#閘道器配置
zuul:
  routes: # 底層是map,可以自定義配置
    mydept.serviceId: springcloud-provider-dept  # 自己原來的id(服務名稱)
    mydept.path: /mydept/**  # 修改為當前id訪問
  # 宣告不能使用的路徑訪問,ignored-services: "*" 表示隱藏全部訪問路徑
  ignored-services: springcloud-provider-dept
  prefix: /stt # 設定公共訪問字首

其中閘道器設定的 routes(路由)設定底層是map:

public void setRoutes(Map<String, ZuulProperties.ZuulRoute> routes) {    
    this.routes = routes;
}

可以自定義配置

啟動類:

註解@EnableZuulProxy開啟閘道器代理

package com.stt.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * @author leizi
 * @create 2019-12-10 22:49
 */
@SpringBootApplication
@EnableZuulProxy  // 開啟閘道器代理
public class ZuulApplication_9527 {
    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication_9527.class, args);
    }
}

測試: