1. 程式人生 > >SpringCloud路由閘道器的設定(六)

SpringCloud路由閘道器的設定(六)

springcloud學習總結

6、路由閘道器的設定

一、新建模組zuul

pom

<dependencies>
        <!-- zuul路由閘道器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- actuator監控 --> <dependency> <groupId>org.springframework.
boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- hystrix容錯 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-
hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- 日常標配 --> <dependency> <groupId>springcloud</groupId> <artifactId>springcloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</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> </dependency> <!-- 熱部署外掛 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>

application.yml

server:
  port: 5555
spring:
  application:
    name: zuul
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
  instance:
    instance-id: zuul
    prefer-ip-address: true


info:
  app.name: atguigu-microcloud
  company.name: yourcompanyname
  build.artifactId: $project.artifactId$
  build.version: $project.version$
zuul:
  routes:
    h.serviceId: microservicecloud-dept
    h.path: /mydept/**
  prefix: /mist
  ignored-services: "*"

必要說明

zuul:
  routes:
    h.serviceId: microservicecloud-dept
    h.path: /mydept/**
  prefix: /mist
  ignored-services: "*"

這一段的意思為 訪問的微服務為microservicecloud-dept,並且需要在原來的
訪問路徑前加/mydept ,在這個路徑前面還得加字首/mist
最後一行為禁止原來的所有的訪問路徑生效。

主啟動類

package com.atguigu.springcloud;

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

@SpringBootApplication
@EnableZuulProxy
public class ZuulMainApplication {
    public static void main(String[] args) {

        SpringApplication.run(ZuulMainApplication.class,args);
    }
}

啟動註冊中心,服務提供者與zuul模組。訪問並檢視結果
http://localhost:5555/mist/mydept/dept/list 可以得到結果

http://localhost:5555/dept/list 則無法返回值

即為上述解釋的那個,禁止原來所有的訪問地址。 否則,上面兩個都可以返回正確的值

後續:待續…