无为商城_创建Zuul网关

时间:2019-11-12
本文章向大家介绍无为商城_创建Zuul网关,主要包括无为商城_创建Zuul网关使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.创建工程

与上面类似,选择maven方式创建Module,然后填写项目名称,我们命名为:leyou-gateway

 填写保存的目录:

2.添加依赖

这里我们需要添加Zuul和EurekaClient的依赖:

<dependencies>
        <!--zuul网关的启动类-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!--zuul网关在注册中心注册,需要配置eureka的客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- springboot提供微服务检测接口,默认对外提供几个接口 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
</dependencies>

3.编写启动类

//组合注解,相当于@EnableAutoConfigration,@ComponentScan,@SpringBootConfiguration
@SpringBootApplication
//开启eureka的注册中心
@EnableDiscoveryClient
//开启Zuul网关代理
@EnableZuulProxy
public class WuweiGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(WuweiGatewayApplication.class);
    }
}

4.配置文件

#修改端口号
server:
  port: 10010

#自定义名字
spring:
  application:
    name: wuwei-gateway

#配置eureka的地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka

    #设置从eureka拉取服务的时间间隔
    registry-fetch-interval-seconds: 5

#设置zuul网关的前缀
zuul:
  prefix: /api
  routes:
    item-service: /item/** # 商品微服务的映射路径

5.项目结构

目前,leyou下有两个子模块:

  • leyou-registry:服务的注册中心(EurekaServer)

  • leyou-gateway:服务网关(Zuul)

 截止到这里,我们已经把基础服务搭建完毕,为了便于开发,统一配置中心(ConfigServer)我们留待以后添加。

原文地址:https://www.cnblogs.com/Tunan-Ki/p/11845099.html