网关Zuul

1. 导入jar包


   <!--        网关的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

        <!--        Eureka 客户端的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>    <!--    约束整个项目的SpringCloud版本-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2. 入口类

@SpringBootApplication
/**
 * 声明当前客户端为可被发现
 * 不仅可以注册到Eureka Server  也可以注册到其他注册中心
 */
@EnableDiscoveryClient
/**
 * 声明当前服务为网管服务
 */
@EnableZuulProxy
//@EnableZuulServer
public class ZuulApplication {

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

}

3. 配置文件

server:
  port: 8010

# 指定当前服务名称  这个名称会注册到注册中心
spring:
  application:
    name: Zuul

#  指定 服务注册中心的地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka,http://localhost:8889/eureka

#路由的配置
zuul:
  routes:
    # 自定义  定义路由规则
    product-server:
      # 路径
      path: /product-server/**
      #  serviceId 写要转发的 服务名称   原因:写服务名称可以负载均衡
      serviceId: SpringBoot-Cloud
      # 这样的方式不可以负载均衡   原因: 路径是写死的
      url: http://localhost:8080/
posted @ 2020-12-24 21:25  花红  阅读(72)  评论(0编辑  收藏  举报