网关可以拦截客户端所有请求,对该请求进行权限控制、负载均衡、路由转发、日志、监控等。
网关是拦截所有服务器请求进行控制
过滤器拦截某单个服务器请求进行控制
相同点:
Zuul和Nginx都可以实现负载均衡、反向代理、过滤请求、实现网关效果
不同点:
Nginx是采用服务器负载均衡进行转发
Nginx采用C语言编写
Nginx适合于服务器端负载均衡
Zuul依赖Ribbon和eureka实现本地负载均衡转发
使用java语言编写
Zuul适合微服务中实现网关
建议最好使用Nginx+Zuul实现网关,Nginx实现反向代理,Zuul实现网关拦截。
1、pom文件引入
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
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文件引入
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
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文件引入
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
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);
}
}
1、pom文件引入
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
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