GuLi Mall-SpringCloud-Gateway gateway core concept, test API gateway

The gateway acts as the entrance of traffic, and its functions include routing and forwarding, authority verification, flow limit control, etc.

  • Route (routing): Contains unique routing id, destination URL, assertion set, filter set, assertion is used to judge

  • Whether the route can reach the destination URL

  • Predicats (assertion): used to determine whether the route can reach the destination URL

  • Filter (filter): can be modified before the request access is successful and after the response is successful


1. Register "gulimall-gateway" to Nacos

1) Create gulimall-gateway microservice

Introduce common dependencies, which contain 

2) Add "gulimall-common" dependency and "spring-cloud-starter-gateway" dependency, as shown in the figure above

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

3) Add the @EnableDiscoveryClient annotation to the GulimallGatewayApplication class 

4) Create the "gateway" namespace in Nacos, and create gulimall-gateway.yml in this namespace at the same time"

I didn't do this here, I still use dev as the namespace, and create gulimall-gateway under dev

5) Create a "bootstrap.properties" file, add the following configuration, specify the address of the configuration center and the namespace to which it belongs

6) Create an "application.properties" file, specify the service name and registry address

spring.application.name=gulimall-gateway
spring.cloud.nacos.discovery.server-addr=192.168.137.14:8848
server.port=88

7) Start "gulimall-gateway"

Start error:

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Because we have configured mybatis-related dependencies in the common module, spring will scan our database configuration by default.

In order to prevent the database configuration error from being found, we need to exclude the database configuration

Solution: Exclude configurations related to data sources in GulimallGatewayApplication 

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

Restart the gateway microservice:

Visit Nacos and see that the service has been registered in Nacos


Records of problems encountered during the process: 

Solution: increase dependency

<dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>


It is found that the gateway service is registered to nacos, which is in the public namespace, not the dev we hoped for

Solution:


2. Case, test the routing function of the gateway:

Configure routing rules

Modify level:

test:

http://localhost:88/?uri=baidu

 

http://localhost:88/?uri=qq 

reference:

gulimall-learning/gulimall-learning-distributed foundation.md at master OYCodeSite/gulimall-learning GitHub

spring-cloud-gateway official website: https://spring.io/projects/spring-cloud-gateway

spring-cloud-gateway documentation: https://docs.spring.io/spring-cloud-

gateway/docs/current/reference/html/

spring-cloud Chinese website: https://www.springcloud.cc/Original

link: https://blog.csdn.net/qq_51998352/article/details/121916920

https://blog.csdn.net/qq_52476654/category_11866407.html

Guess you like

Origin blog.csdn.net/ZHOU_VIP/article/details/129714768