001、POM配置
和普通Spring Boot工程相比,增加了Eureka Client、Zuul依赖和Spring Cloud依赖管理
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
002、使能Zuul Proxy
@SpringBootApplication @EnableZuulProxy //使能API网关 public class APIGatewayZuulApplication { public static void main(String[] args) { SpringApplication.run(APIGatewayZuulApplication.class, args); } }
003、src/main/resources下配置文件application.yml
spring:
application:
name: api-gateway-zuul
server:
port: 5001
eureka:
client:
serviceUrl:
defaultZone: http://discovery:1000/eureka/
使用步骤:
a)启动Eureka Server服务eureka-server
b)启动Hello Service服务hello-service-provider(可启动多个)
c)启动api-gateway-zuul服务
d)通过http://localhost:5001/hello-service-provider/hello即可访问Hello Service的服务
004、自定义服务路径
zuul:
routes:
sayhello: # 可以随便写,在zuul上面唯一即可;当这里的值 = service-id时,service-id可以不写。
path: /sayhello/** # 想要映射到的路径
service-id: hello-service-provider # Eureka中的serviceId
此时通过http://localhost:5001/sayhello/hello即可访问Hello Service的服务
005、忽略指定服务不代理
zuul:
ignored-services: hello-service-provider
此时hello-service-provider服务不会被代理