SpringCloud Zuul微服务网关搭建
创建一个maven工程,打开pom文件引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- zuul网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
创建application.yml文件
###注册中心
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/
server:
port: 80
###网关名称
spring:
application:
name: service-zuul
### 配置网关反向代理
zuul:
routes:
api-a:
### 客户端法师请求127.0.0.1:80/api-mabber开头的,都转发到会员服务
path: /api-member/**
###服务别名 zuul网关默认整合ribbon 自动实现本地负载均衡轮循环效果
serviceId: app-songzixian-member
api-b:
### 客户端法师请求127.0.0.1:80/api-order开头的,都转发到订单服务
path: /api-order/**
serviceId: app-songzixian-order
启动测试
@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class AppZuul {
public static void main(String[] args) {
SpringApplication.run(AppZuul.class,args);
}
}
网关转发效果图
推荐阅读:使用过滤器验证客户端是否有登陆
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »