网关作用
网关可以拦截客户端所有请求,对该请求进行权限控制、负载均衡、路由转发、日志、监控等。
网关与过滤器区别
- 网关是拦截所有服务器请求进行控制
- 过滤器拦截某单个服务器请求进行控制
Nginx与Zuul的区别
相同点:
- Zuul和Nginx都可以实现负载均衡、反向代理、过滤请求、实现网关效果
不同点:
- Nginx是采用服务器负载均衡进行转发
- Nginx采用C语言编写
- Nginx适合于服务器端负载均衡
- Zuul依赖Ribbon和eureka实现本地负载均衡转发
- 使用java语言编写
- Zuul适合微服务中实现网关
建议最好使用Nginx+Zuul实现网关,Nginx实现反向代理,Zuul实现网关拦截。
项目搭建
搭建Eureka注册中心
1、pom文件引入
<!-- 管理依赖 -->
<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>
<!--SpringCloud eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
2、配置文件
server:
port: 8100
eureka:
instance:
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
register-with-eureka: false
fetch-registry: false
3、启动项
@SpringBootApplication
// @EnableEurekaServer 表示开启EurekaServer服务 开启注册中心
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
搭建会员服务
1、pom文件引入
<!-- 管理依赖 -->
<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>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
2、配置文件
###服务启动端口号
server:
port: 8030
###服务名称(服务注册到eureka名称)
spring:
application:
name: app-lun-member
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
register-with-eureka: true
fetch-registry: true
3、启动项
@RestController
@EnableEurekaClient
@SpringBootApplication
public class AppLunMemberApplication {
@Value("${server.port}")
private String serverPort;
@RequestMapping("/")
public String index() {
return "我是member会员服务" + serverPort;
}
public static void main(String[] args) {
SpringApplication.run(AppLunMemberApplication.class, args);
}
}
搭建订单服务
1、pom文件引入
<!-- 管理依赖 -->
<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>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
2、配置文件
###服务启动端口号
server:
port: 8020
###服务名称(服务注册到eureka名称)
spring:
application:
name: app-lun-order
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
register-with-eureka: true
fetch-registry: true
3、启动项
@RestController
@EnableEurekaClient
@SpringBootApplication
public class AppLunOrderApplication {
@RequestMapping("/")
public String index() {
return "我是订单服务项目";
}
public static void main(String[] args) {
SpringApplication.run(AppLunOrderApplication.class, args);
}
}
搭建Zuul服务
1、pom文件引入
<!-- 管理依赖 -->
<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>
<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>
2、配置文件
###注册 中心
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-member开头的都会转发到会员服务
### 以 /api-member/访问转发到会员服务
path: /api-member/**
serviceId: app-lun-member
api-b:
### 以 /api-order/访问转发到订单服务
path: /api-order/**
serviceId: app-lun-order
3、启动项
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
注意:Zuul 默认开启了 Ribbon本地负载均衡功能
4、直接访问服务别名,Zuul实现反向代理,转发到会员服务。
访问127.0.0.1/api-member