10、zuul 网关配置

zuul 和 eureka 整合,将zuul自身注册为 Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul 跳转后获得

提供=代理+路由+过滤 三大功能

  • 创建 zuul 模块项目

  • pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parent-demo</artifactId>
        <groupId>com.spcd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>


    <artifactId>ms-zuul-gateway-9527</artifactId>
    <dependencies>
        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- zuul -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
        <dependency>
            <groupId>com.spcd</groupId>
            <artifactId>ms-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- actuator监控信息完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <!--<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!-- hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
    </dependencies>

</project>

 

  • yml

server:
  port: 9527

eureka:
  client: #客户端注册进eureka服务列表内
    service-url:
      #      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: ms-zuul-gateway-9527.com    # 防止 Eureka web页面报错 ,主机服务名称修改
    prefer-ip-address: true                  # 访问路径可以显示IP地址,(左下角IP显示)

info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

spring:
  application:
    name: ms-zuul-gateway-9527

 

  • 修改 hosts

127.0.0.1  myzuul.com

 

  • 主启动类

@EnableEurekaClient
@SpringBootApplication
@EnableZuulProxy
public class MsZullGatewayApplication9527 {

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

 

  • 测试(启动 eureka集群,服务提供者8001,zuul网关)可以成功访问即可

http://localhost:8001/api/dept/list

http://myzuul.com:9527/ms-provider/api/dept/list(ms-provider :服务名, myzuul.com:9527 : 网关)

 

  • 路由配置()

zuul:
  routes:
    mydept:
      serviceId: ms-provider
      path: /myProvider/**
#  ignored-services: "ms-provider"  # 忽略一个 
  ignored-services: "*"             # 没有此配置,用服务名也可以访问
  prefix: /ms                       # router 统一前缀

 

    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值