springcloud of zuul small chestnuts

Write in front

  • The source code is
    here .
  • Read this article to prepare
    This article continues the analysis on the basis of this blog post .
    This article will create a zuul gateway service and call the service created in the previous blog post.

1: Createmy-service-zuul

new-> module,Follow the prompts to enter gav and other information, select maven
, java8, etc. and then finish, the current project module structure of finish is as shown in the figure below:
Insert picture description here

2: Set parent

<parent>
    <groupId>dongshi.daddy</groupId>
    <artifactId>my-eureka</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

3: Introduce dependencies

<dependencies>
    <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-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
</dependencies>

4: Complete pom

<?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>

    <groupId>dongshi.daddy</groupId>
    <artifactId>my-service-zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>my-service-zuul</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>dongshi.daddy</groupId>
        <artifactId>my-eureka</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
    </dependencies>
</project>

5: Configuration file

server:
  port: 8769

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: service-zuul
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: dongshidaddy-first-ribbon
    api-b:
      path: /api-b/**
      serviceId: dongshidaddy-first-feign-client

By zuul.routes.{api-name}.pathsetting the path of the API to be routed, and by zuul.routes.{api-name}.serviceIdsetting the service id of the specific processing API path, the (在eureka服务端注册的id)following mapping relationship exists here:

/api-a/** -> dongshidaddy-first-ribbon
/api-b/** -> dongshidaddy-first-feign-client

6: Enable related solutions

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient

The role is to enable related functions.

7: Test

7.1: Startmy-enreka-server

The service is 注册中心used.
Insert picture description here
You can see this time No instances available.

7.2: Startmyeureka-client

This service serves as an application of the service that is finally provided to the outside world. Note that this is not for us in my-service-zuulthe application.ymlconfigured serviceId.
Insert picture description here
The service id DONGSHIDADDY-FIRST-EUREKA-CLIENTis now registered.

7.3: Startmy-service-feign

The service is what we my-service-zuulconfigure in:

api-b:
      path: /api-b/**
      serviceId: dongshidaddy-first-feign-client

One of the serviceId: dongshidaddy-first-feign-clientcorresponding specific services. The registration information after startup is as follows:
Insert picture description here

7.4: Startmy-service-ribbon

This service is what we are my-service-zuulconfiguring

api-a:
      path: /api-a/**
      serviceId: dongshidaddy-first-ribbon

The dongshidaddy-first-ribboncorresponding specific services in the eureka server are as follows after startup:
Insert picture description here

7.5: Startmy-service-zuul

After startup:
Insert picture description here

7.6: Access/api-a/**

xbdeMacBook-Air:~ xb$ curl http://localhost:8769/api-a/hi?name=dongshidaddy
hi dongshidaddy ,i am from port:8763

7.7: Access/api-b/**

xbdeMacBook-Air:~ xb$ curl http://localhost:8769/api-b/hi?name=dongshidaddy
hi dongshidaddy ,i am from port:8763

8: Add filtering function

In addition to the routing function, zuul also supports filtering and interception functions. It only needs to implement com.netflix.zuul.ZuulFilterabstract classes, as defined as follows:

@Component
public class MyFilter extends ZuulFilter {
    
    

    private static Logger log = LoggerFactory.getLogger(MyFilter.class);
    @Override
    public String filterType() {
    
    
        return "pre";
    }

    @Override
    public int filterOrder() {
    
    
        return 0;
    }

    @Override
    public boolean shouldFilter() {
    
    
        return true;
    }

    @Override
    public Object run() {
    
    
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
        Object accessToken = request.getParameter("token");
        if(accessToken == null) {
    
    
            log.warn("token is empty");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            try {
    
    
                ctx.getResponse().getWriter().write("token is empty");
            }catch (Exception e){
    
    }

            return null;
        }
        log.info("ok");
        return null;
    }
}

8.1: Testing

Pay attention to restart my-service-zuul.

xbdeMacBook-Air:~ xb$ curl http://localhost:8769/api-b/hi?name=dongshidaddy
token is empty
xbdeMacBook-Air:~ xb$ curl "http://localhost:8769/api-b/hi?name=dongshidaddy&token=111"
hi dongshidaddy ,i am from port:8763

Finally: get out of the way, I want to drink Luckin

Insert picture description here

Guess you like

Origin blog.csdn.net/wang0907/article/details/109256769