乐优商城:笔记(三):网关微服务:LyGateway

网关微服务:

  • 1 添加依赖
  • 2 编写启动类
  • 3 配置文件

1 添加依赖


<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>leyouartifactId>
        <groupId>com.leyou.parentgroupId>
        <version>1.0.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <groupId>com.leyou.commongroupId>
    <artifactId>ly-gatewayartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-zuulartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-netflix-zuulartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-netflix-zuulartifactId>
            <version>2.0.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>com.leyou.servicegroupId>
            <artifactId>ly-auth-commonartifactId>
            <version>1.0.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>com.leyou.commongroupId>
            <artifactId>ly-commonartifactId>
            <version>1.0.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

2 编写启动类

@SpringCloudApplication
@EnableZuulProxy
public class LyGateway {
    public static void main(String[] args) {
        SpringApplication.run(LyGateway.class);
    }
}

zuul的注解@EnableZuulServer和@EnableZuulProxy

  • @EnableZuulProxy简单理解为@EnableZuulServer的增强版,当Zuul与Eureka、Ribbon等组件配合使用时,我们使用@EnableZuulProxy
  • 使用@EnableZuulProxy注解会增加额外的几种过滤器 。

注解@SpringCloudApplication和@SpringBootApplication的区别
乐优商城:笔记(三):网关微服务:LyGateway_第1张图片
注解@SpringCloudApplication包括:

  • @SpringBootApplication:SpringBoot注解
  • @EnableDiscoveryClient:注册服务中心Eureka注解
  • @EnableCircuitBreaker,断路器注解。

       对于SpringCloud来说,这是每一微服务必须应有的三个注解,所以才推出了@SpringCloudApplication这一注解集合。

       因此,使用@SpringCloudApplication需要引入Springboot依赖外,还需要引入以下的两个依赖:

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-hystrixartifactId>
dependency>

       但是,spring-cloud-starter-netflix-zuul中已经引入了上述hystrix,因此可以简化配置。
乐优商城:笔记(三):网关微服务:LyGateway_第2张图片

3 配置文件

server:
  port: 10010
spring:
  application:
    name: api-gateway
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
zuul:
  prefix: /api # 添加路由前缀
  routes:
    item-service: /item/**
    search-service: /search/**
    user-service: /user/**
    auth-service: /auth/**
    cart-service: /cart/**
    order-service: /order/**
    ignored-services:
      - upload-service # 忽略upload-service服务
  add-host-header: true
  sensitive-headers:
#    upload-service: #/upload/**:不能这样写,网关捕获到upload之后页面的url只剩下/image,但是controller里面是/upload/image,所以不能去除upload的前缀
#      path: /upload/**
#      serviceId: upload-service
#      strip-prefix: false
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMillisecond: 5000 # 熔断超时时长:10000ms
ribbon:
  ConnectTimeout: 1000 # 连接超时时间(ms)
  ReadTimeout: 3500 # 通信超时时间(ms)
  MaxAutoRetriesNextServer: 0 # 同一服务不同实例的重试次数
  MaxAutoRetries: 0 # 同一实例的重试次数

你可能感兴趣的:(乐优商城)