简介
zuul网关组件;如现在有两个微服务部署在异地,访问A服务ipA:8001/getData,访问B服务ipB:8002/getView,即需要访问多个ip和端口,添加zuul网关服务,则只需访问网关服务的ip和其端口即可;
前置内容:
SpringCloud极简入门>断路器监控#turbine
实战
1、创建《zuul》模块
1.1、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cc.catface</groupId>
<artifactId>simple-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zuul</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--zuul依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
</project>
1.2、yml
server:
port: 8062
spring:
application:
name: zuul
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/
#zuul路由配置
zuul:
routes:
api-a:
path: /ac/**
serviceId: SERVER-ACCOUNT
api-b:
path: /or/**
serviceId: SERVER-ORDER
1.3、application
application声明注解@EnableZuulProxy
;
2、效果
分别启动《server-center》、《server-config》,《server-account》、《server-order》、《hystrix-dashboard》、《turbine-dashboard》、《zuul》,发起请求,发现通过网关服务的ip和端口可以完成对外部服务的访问;