spring cloud(二) zuul

spring cloud 网关 zuul 搭建过程

1. 新建boot工程 pom引入依赖

 <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
 </dependency>
 <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>

2.启动类添加注解@EnableZuulProxy

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class, args);
    }

}

3 application.yml添加配置

这里使用yml文件 更好的显示网关的路由规则

server:
  port: 9006
spring:
  application:
    name: zuul-server
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${eureka.instance.hostname}:${server.port}
zuul:
  routes:
    user-api:
      path: /userService/*
      service-id: user-service
    product-api:
      path: /productService/*
      service-id: product-service

4.调用验证

如 user-service 提供接口/getUserInfoList   则通过网关调用地址为http://localhost:9006/userService/getUserInfoList

项目github 地址  https://github.com/yongxiangliu123/SpringCloud

posted @ 2019-06-25 10:54  农夫与蛇丶  阅读(133)  评论(0)    收藏  举报