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 则无法返回值

即为上述解释的那个,禁止原来所有的访问地址。 否则,上面两个都可以返回正确的值

后续:待续…

你可能感兴趣的:(SpringCloud学习笔记)