Gateway is used in combination with Nacos
Welcome here, today I will introduce how to combine Spring Cloud Gateway and Nacos to realize an efficient and stable service gateway! In the microservice architecture, the API gateway is an essential part, which provides functions such as routing requests, load balancing, security authentication, and current limiting. Spring Cloud Gateway is an API gateway based on technologies such as Spring Framework, Spring Boot, and Project Reactor. It provides a simple and effective way to route, filter, and control traffic for microservices. Nacos is a component for service registration and discovery, which provides a stable, easy-to-use, lock-free service discovery solution. So, let’s see how to combine them to achieve a powerful service gateway!
?
- Gateway is used in combination with Nacos
-
- 1. Use Gateway alone
- 2. The combination of Gateway and Nacos
- 3. Summary
1. Use Gateway alone
First, let’s take a look at how to use Spring Cloud Gateway alone. Just add dependencies and configure Gateway.
- Add dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
- Configure Gateway?
Then, declare the route to be forwarded in the configuration file (application.yml or application.properties).
server: port: 7003 spring: application: name: gateway-server cloud: gateway: routes: - id: user-server # ID of the current route, unique requirement uri: "http://localhost:7001" # The address to forward the request to order: 1 # The priority of the route, the smaller the number, the higher the level predicates: # Assertion (that is, the conditions to be met by routing and forwarding) - Path=/user-server/** # Only when the request path satisfies the rules specified by Path, will the routing be forwarded filters: # filter, the request can be modified through the filter during the transmission process - StripPrefix=1 # Remove layer 1 path before forwarding
A route is declared in the above configuration to forward the request with the path /user-server/** to the corresponding service.
Next, let’s take a look at how to use Spring Cloud Gateway and Nacos together to achieve more powerful functions.
2. The combination of Gateway and Nacos
We can combine Spring Cloud Gateway and Nacos to provide a more stable and efficient service gateway solution for the microservice architecture. Next, I will teach you how to combine them step by step.
- Add dependencies
Add the following dependencies to the pom.xml file.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.cloud spring-cloud-starter-loadbalancer
These are some Spring Cloud-related dependencies, including Spring Cloud Gateway, Spring Cloud Alibaba Nacos Discovery and Spring Cloud LoadBalancer.
When Gateway uses Nacos as a service discovery and registration center, using the loadbalancer dependency allows Gateway to automatically obtain a list of available service instances from the Nacos registration center, and forward requests to different instances according to routing rules to achieve load balancing and high availability .
Therefore, adding the loadbalancer dependency can make the combination of Gateway and Nacos more perfect, and provide a more stable and efficient service gateway solution for the microservice architecture.
- Add @EnableDiscoveryClient annotation (optional)?
@SpringBootApplication @EnableDiscoveryClient public class GateWayApplication {<!-- --> public static void main(String[] args) {<!-- --> SpringApplication.run(GateWayApplication.class, args); System.out.println("run"); } }
- Configure nacos registration center and gateway?
This is a configuration file based on Spring Cloud Gateway, which configures the routing rules of the gateway.
server: port: 7003 spring: application: name: gateway-server cloud: nacos: discovery: server-addr: 192.168.157.129:8848 gateway: routes: - id: user-server # ID of the current route, unique requirement uri: lb://user-server # lb refers to obtaining microservices by name from nacos, and follows negative order: 1 # The priority of the route, the smaller the number, the higher the level predicates: # Assertion (that is, the conditions to be met by routing and forwarding) - Path=/user-server/** # When the request path satisfies the rules specified by Path, the route forwarding is performed filters: # filter, the request can be modified through the filter during the transmission process - StripPrefix=1 # Remove layer 1 path before forwarding
- server.port: Specify the port number of the gateway application, which is set to 7003 here.
- spring.application.name: Specify the name of the gateway application, which is set to gateway-server here.
- spring.cloud.nacos.discovery.server-addr: Specifies the address of the Nacos service registry, which is set to 192.168.157.129:8848.
Under gateway.routes, a routing rule is configured:
When the request path meets the rules specified by Path (/user-server/**), it will be forwarded to the microservice named user-server.
The priority (order) of the route is specified, and the StripPrefix filter is used to remove one path in the request path and forward the remaining part to the next-level microservice.
Concise version: Remove the routing configuration
?
server: port: 7003 spring: application: name: gateway-server cloud: nacos: discovery: server-addr: 192.168.157.129:8848 gateway: discovery: locator: enabled: true # Let the gateway discover microservices in nacos
This is a configuration file based on Spring Cloud Gateway, which adds the configuration of the discovery service.
Set it to true in the gateway.discovery.locator.enabled property, indicating that the routing function based on service discovery is enabled. In this way, when routing, the corresponding service instance will be obtained from the Nacos service registry according to the name of the microservice, thus realizing automatic service discovery
and load balancing
function.
- Run the Spring Boot application to register with the nacos service center
Register a microservice named user-server in the nacos service center and start at least two examples.
Both examples are the same except for the port number and the data returned by the interface
- test application
Requests can now be sent to Spring Cloud Gateway and forwarded to the appropriate service. As long as you follow the Gateway address/microservice/interface format
to access, you can get a successful response.
http://localhost:7003/user-server/user/get1
Gateway address/microservice name/interface format
Obtain the corresponding service instance from the Nacos service registry according to the name of the microservice, thus realizing the functions of automatic service discovery
and load balancing
.
3. Summary
The above is an example of using Spring Cloud Gateway for gateway configuration and Nacos for service discovery and load balancing. The combination of Spring Cloud Gateway and Nacos can provide a more stable and efficient service gateway solution for the microservice architecture.