zuul基本认识
zuul是netflix开源的一个APIGateway服务器, 本质上是一个webservlet应用
网关是微服务项目的大门,相当于单体项目中的拦截器还有vue中的路由
zuul的实现
导入依赖
<!--springweb依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 表示注册到eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 网关zuul组件依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
配置文件
server:
port: 10040
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # 表示注册中心的地址
instance: # 打开IP注册
instance-id: ${spring.application.name}:${server.port} # 设置实例名称
prefer-ip-address: true # 开启IP注册
# 指定服务名称,此服务下的集群所有服务都叫此服务名
spring:
application:
name: zuul-server
# zuul网关配置
zuul:
ignoredServices: '*' #关闭所有服务服务名访问
routes: #服务的路由配置
#服务名称: 路由配置
order-server: /order/**
pay-server: /pay/**
user-server: /user/**
prefix: /noting
启动类加个注解@EnableZuulProxy启用网关
package com.noting;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
// 这个注解表示此项目是一个eureka-client服务,但是其实只要导入了client依赖就表示一个client,所以这个注解可以不加
@EnableEurekaClient
// 这个注解表示此项目启用Zuul API网关代理的功能(Spring Cloud Netflix提供的注解)
@EnableZuulProxy
public class ZuulServerApp {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApp.class, args);
}
}