2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The main content of this section is to create a gateway module, register the gateway to Nacos, and configure routing for testing.
Right click projectNew->Module
, create a new module, module name gulimall-gateway
。
Fill in various information.
Select the Gateway dependency.
Click Create to create the module.
<dependency>
<groupId>com.atguigu.gulimall</groupId>
<artifactId>gulimall-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Because the gateway service also needs to use the registration center and configuration center, it needs dependencies on related packages, which can be obtained by relying on the common module.
By adding annotations to the startup class, the service is registered with the registration center after startup.
@EnableDiscoveryClient
Create three new configuration files in the resources directory.
① application.properties
Configure the Nacos registration center related information.
server.port=80
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.application.name=gulimall-gateway
② bootstrap.properties
Configure the relevant information of Nacos configuration center. Note that you need to create a namespace named gateway on Nacos in advance. All configuration files of the gateway module are stored in this namespace.
spring.application.name=gulimall-gateway
spring.cloud.nacos.config.server-addr=localhost:8848
spring.cloud.nacos.config.namespace=405d2201-d62b-4203-80c8-800e9387ad40
③ application.yml
In this file, write the gateway routing configuration. See the next section for details.
In this section, we will take two actual requirements as examples to practice how to configure the gateway.
The role of the gateway is to forward the received requests to the corresponding services according to the rules configured by the gateway.
For the convenience of demonstration, assume that there is such a requirement. When the following URL is entered in the browser, the request will be forwarded to Baidu.
http://localhost/?url=baidu
When the following URL is entered in the browser, the request will be forwarded to Tencent.
http://localhost/?url=qq
How to write a configuration file?
Repository Spring Cloud GatewayOfficial Documentation, refer to its format.
Because we need to forward the query conditions on the URL, we find the Gateway assertion configuration document about Query.
Refer to this document for configuration.
application.yml
spring:
cloud:
gateway:
routes:
- id: baidu_test
uri: https://www.baidu.com
predicates:
- Query=url,baidu
- id: qq_test
uri: https://www.qq.com
predicates:
- Query=url,qq
Two routing rules are defined here to match the query parameters.url
The value of determines the target URI to which the request should be forwarded.
spring.cloud.gateway.routes
: This is the route configuration list of Spring Cloud Gateway.
Eachroutes
The item defines a routing rule.
id
: Each routing rule requires a unique ID to identify and manage the route.
uri
: When the routing rule matches, the request will be forwarded to this URI address.https://www.baidu.com
andhttps://www.qq.com
The URLs of the Baidu and Tencent websites are specified respectively.
predicates
: This is a list of assertions for the route rule that are used to determine whether this route rule is applied. Assertions are expressions that are evaluated based on the request's metadata.
Query=url,baidu
: This assertion states that if the request contains aurl
, and its value is equal tobaidu
, then this routing rule will be triggered and the request will be forwarded tohttps://www.baidu.com
。
Query=url,qq
: Similarly, if the query parameterurl
The value ofqq
, the request will be forwarded tohttps://www.qq.com
。
Then, when your application receives aurl=baidu
orurl=qq
When a request with query parameters is made, Spring Cloud Gateway will proxy the request to the corresponding website according to the above rules.
Access the following address in your browser.
http://localhost/?url=baidu
If the above interface appears, it means that the gateway service configuration and routing configuration are normal.
An error will be reported during the process of starting Gateway.
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-07-11 15:57:32.988 ERROR 27224 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
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
The reason is that the Gateway module depends on common, and the common module declares a dependency on mybatis. The mybatis package will look for database-related configurations at startup, but the Gateway project does not require a database, so there is no related configuration.
The solution is to tell the gateway module that it does not need to look for database-related configurations at startup by declaring the exclusion of related package dependencies in the startup class annotation.
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)