高并发架构实践指南——资源部署(路由和过滤器Zuul)

开发Zuul Gateway

Core Annotation

package tech.foolfish.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class MicroserviceGatewayZuulApplication {

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

}

application.yml

---
eureka:
  client:
    fetchRegistry: true
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8079
spring:
  application:
    name: microservice-gateway-zuul
  profiles: default
---
eureka:
  client:
    fetchRegistry: true
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://eureka-peer1.com:8762/eureka/, http://eureka-peer2.com:8763/eureka/, http://eureka-peer3.com:8764/eureka/
server:
  port: 8079
spring:
  application:
    name: microservice-gateway-zuul
  profiles: zuul1
---
eureka:
  client:
    fetchRegistry: true
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://eureka-peer1.com:8762/eureka/, http://eureka-peer2.com:8763/eureka/, http://eureka-peer3.com:8764/eureka/
server:
  port: 8078
spring:
  application:
    name: microservice-gateway-zuul
  profiles: zuul2

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>tech.foolfish</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>microservice-gateway-zuul</name>
    <description>Gateway</description>
    <properties>
        <java.version>11</java.version>
        <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

部署Zuul Gateway

xuej@dev-server:~/Documents/sts4/microservice-gateway-zuul$ mvn clean package -DskipTests=true
xuej@dev-server:~/Documents/sts4/microservice-gateway-zuul$ java -jar -Dspring.profiles.active=zuul1 target/gateway-0.0.1-SNAPSHOT.jar
xuej@dev-server:~/Documents/sts4/microservice-gateway-zuul$ java -jar -Dspring.profiles.active=zuul2 target/gateway-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.3.12.RELEASE)

2022-07-26 00:40:50.177  INFO 27784 --- [           main] t.f.g.MicroserviceGatewayZuulApplication : The following profiles are active: zuul2
2022-07-26 00:40:50.920  WARN 27784 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2022-07-26 00:40:50.934  WARN 27784 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'hystrix.stream' contains invalid characters, please migrate to a valid format.
2022-07-26 00:40:51.094  INFO 27784 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=4f761e5d-a84c-3fcb-af31-3ce817b6a804
2022-07-26 00:40:51.467  INFO 27784 --- [           main] org.eclipse.jetty.util.log               : Logging initialized @2914ms to org.eclipse.jetty.util.log.Slf4jLog
2022-07-26 00:40:51.628  INFO 27784 --- [           main] o.s.b.w.e.j.JettyServletWebServerFactory : Server initialized with port: 8078
2022-07-26 00:40:51.631  INFO 27784 --- [           main] org.eclipse.jetty.server.Server          : jetty-9.4.42.v20210604; built: 2021-06-04T17:33:38.939Z; git: 5cd5e6d2375eeab146813b0de9f19eda6ab6e6cb; jvm 11.0.11+9
2022-07-26 00:40:51.663  INFO 27784 --- [           main] o.e.j.s.h.ContextHandler.application     : Initializing Spring embedded WebApplicationContext
2022-07-26 00:40:51.663  INFO 27784 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1461 ms
2022-07-26 00:40:51.809  WARN 27784 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2022-07-26 00:40:51.810  INFO 27784 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2022-07-26 00:40:51.824  INFO 27784 --- [           main] c.netflix.config.DynamicPropertyFactory  : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@757f675c
2022-07-26 00:40:52.085  INFO 27784 --- [           main] org.eclipse.jetty.server.session         : DefaultSessionIdManager workerName=node0
2022-07-26 00:40:52.085  INFO 27784 --- [           main] org.eclipse.jetty.server.session         : No SessionScavenger set, using defaults
2022-07-26 00:40:52.086  INFO 27784 --- [           main] org.eclipse.jetty.server.session         : node0 Scavenging every 660000ms
2022-07-26 00:40:52.105  INFO 27784 --- [           main] o.e.jetty.server.handler.ContextHandler  : Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@56781d96{application,/,[file:///tmp/jetty-docbase.8078.15515249679750196809/],AVAILABLE}
2022-07-26 00:40:52.105  INFO 27784 --- [           main] org.eclipse.jetty.server.Server          : Started @3553ms
2022-07-26 00:40:52.158  WARN 27784 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2022-07-26 00:40:52.158  INFO 27784 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2022-07-26 00:40:52.464  INFO 27784 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2022-07-26 00:40:52.795  INFO 27784 --- [           main] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses Jersey
2022-07-26 00:40:52.849  WARN 27784 --- [           main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2022-07-26 00:40:53.160  INFO 27784 --- [           main] o.s.c.n.zuul.ZuulFilterInitializer       : Starting filter initializer
2022-07-26 00:40:53.180  INFO 27784 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2022-07-26 00:40:53.225  INFO 27784 --- [           main] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2022-07-26 00:40:53.302  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2022-07-26 00:40:53.356  INFO 27784 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
2022-07-26 00:40:53.356  INFO 27784 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
2022-07-26 00:40:53.541  INFO 27784 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
2022-07-26 00:40:53.541  INFO 27784 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
2022-07-26 00:40:53.743  INFO 27784 --- [           main] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2022-07-26 00:40:53.766  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2022-07-26 00:40:53.767  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2022-07-26 00:40:53.767  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2022-07-26 00:40:53.767  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Application is null : false
2022-07-26 00:40:53.767  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2022-07-26 00:40:53.767  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2022-07-26 00:40:53.767  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2022-07-26 00:40:53.933  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : The response status is 200
2022-07-26 00:40:53.937  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2022-07-26 00:40:53.940  INFO 27784 --- [           main] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2022-07-26 00:40:53.943  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1658767253943 with initial instances count: 4
2022-07-26 00:40:53.945  INFO 27784 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application MICROSERVICE-GATEWAY-ZUUL with eureka with status UP
2022-07-26 00:40:53.946  INFO 27784 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1658767253946, current=UP, previous=STARTING]
2022-07-26 00:40:53.949  INFO 27784 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_MICROSERVICE-GATEWAY-ZUUL/10.0.2.15:microservice-gateway-zuul:8078: registering service...
2022-07-26 00:40:53.956  INFO 27784 --- [           main] o.e.j.s.h.ContextHandler.application     : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-07-26 00:40:53.956  INFO 27784 --- [           main] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-07-26 00:40:53.964  INFO 27784 --- [           main] o.s.web.servlet.DispatcherServlet        : Completed initialization in 7 ms
2022-07-26 00:40:53.983  INFO 27784 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_MICROSERVICE-GATEWAY-ZUUL/10.0.2.15:microservice-gateway-zuul:8078 - registration status: 204
2022-07-26 00:40:53.985  INFO 27784 --- [           main] o.e.jetty.server.AbstractConnector       : Started ServerConnector@2d35442b{HTTP/1.1, (http/1.1)}{0.0.0.0:8078}
2022-07-26 00:40:53.986  INFO 27784 --- [           main] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 8078 (http/1.1) with context path '/'
2022-07-26 00:40:53.986  INFO 27784 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8078
2022-07-26 00:40:54.014  INFO 27784 --- [           main] t.f.g.MicroserviceGatewayZuulApplication : Started MicroserviceGatewayZuulApplication in 4.952 seconds (JVM running for 5.462)