Overall design architecture diagram

Test the sample

First, environmental preparation

1. Download the nacos package and run the command

Nacos Default account password: nacos/nacos

The service list is as follows

2. Download the Redis package and run it

Mysql Docker is running

4. Download git source code

Github.com/aliyun/alib…

Project address: github.com/aliyun/alib…

Note: The springCloud-Dubbo test was selected to address the refactoring of old projects under refactoring real-world scenarios, the process of fast access to HTTP <-> RPC conversion with low threshold, and the strong ecosystem of SpringCloud.

Environment tidy up

The project uses zuul gateway to access all services, HTTP access, REST service and DuBBO protocol to process user requests.

Modify the configuration

Service health

Effect after successful operation of the project

Two, technical points

1. Zuul routing configuration

Commodity service Interface for querying commodity information Routing and forwarding

zuul.routes.api-a.id=api-a
zuul.routes.api-a.path=/api/a/**
zuul.routes.api-a.service-id=productservice
Copy the code

You can delegate all services to localhost:8080/xx/xx as follows to provide a server externally

zuul.routes.api-b.id=api-b
zuul.routes.api-b.path=/**
zuul.routes.api-b.service-id=productservice
Copy the code

2, HTTP protocol to RPC protocol, how to achieve the code level?

Cartservice – API – Shopping cart API interface

Cartservice -provider- cartservice provider

Cartservice -consumer- shopping cartservice for consumers

Note: Only THE API interface and provider service are created in the project, and the Consumer service is the aggregation service or other basic services. The API interface is imported through the @Reference annotation, and the consumer service will be registered with the registry when the project starts.

Ex. :

import org.apache.dubbo.config.annotation.Reference; @reference (version = "1.0.0", check = false) private CartService CartService;Copy the code

Need to introduce dependencies

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-openfeign-dependencies</artifactId> <version>${spring-cloud-openfeign.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring.cloud.alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.spring.version}</version> </dependency> <dependency>  <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> < the groupId > com. Spring4all < / groupId > < artifactId > swagger - spring - the boot - starter < / artifactId > < version > 1.7.0. RELEASE < / version > </dependency>Copy the code

API interface

Service providers implement API interfaces

The service consumer can be created directly in the restController layer using the API interface, as shown in the following figure

Dubbo configuration

Springcloud configuration

Swagger API document aggregation

<dependency> <groupId>com.spring4all</groupId> <artifactId>swagger-spring-boot-starter</artifactId> <version>1.7.0.RELEASE</version> </ Dependency > @enablesWagger2Doc Public Class Application {swagger.enabled=true swagger.base-package= com.alibabacloud.hipstershop swagger.base-path=/**Copy the code

4, Zuul filter expansion

4.1,PreRequestLogFilter extension for filtering between request creation

public class PreRequestLogFilter extends ZuulFilter { @Override public String filterType() { return FilterConstants.PRE_TYPE; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() { RequestContext currentContext = RequestContext.getCurrentContext(); HttpServletRequest request = currentContext.getRequest(); System.out.print(String.format("send %s request to %s",request.getMethod(),request.getRequestURL())); return null; }}Copy the code

Springapplication adds the following

@Bean
public PreRequestLogFilter preRequestLogFilter(){
    return new PreRequestLogFilter();
}
Copy the code

Custom filters are used

zuul.PreRequestLogFilter.pre.disable= true
Copy the code

Filter Description

Note:

  • FilterType: this function needs to return a string representing the filterType, which is defined at each stage of the HTTP request. There are four filter types with different life cycles defined by default in Zuul, as shown below.
  • Pre: can be called before the request is routed.
  • Routing: Called when routing a request.
  • Post: called after routing and error filters.
  • Error: Called when an error occurs while processing a request.
  • FilterOrder: Defines the order in which filters are executed using an int value. A smaller value indicates a higher priority.
  • ShouldFilter: Returns a Boolean value to determine whether the filter should be executed. We can use this method to specify the valid range of the filter.
  • Run: specifies the logic of the filter. In this function, we can implement custom filtering logic to determine whether to intercept the current request and not route it further, or to do something with the result of the processing after the request route returns the result, etc.