Preface: GateWay integration with Nacos needs to pay attention to the place, I think the first is the dependency, because many dependencies are conflicting or missing things, so the integration of dependency must pay attention to the version of the agreement or version selection, version selection is not good, the pit will not be less, then I put the dependency out, for reference only
1. GateWay&Nacos project integration
1: Inject dependencies – The choice of dependencies
Trust me: there are no holes in this version<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3. RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<dependencies>
<! --gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.1.3. RELEASE</version>
</dependency>
<! --nacos dicovery-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2. RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Copy the code
2: Bootstrapt file is configured
Note: Note the file locator. Enabled =true and locator: The first is to enable service registration and discovery, which can be configured with Nacos to use LB for url forwarding and later load balancing. The other is because the registry will uppercase the path, which is lowercase by standard
Note: the yML file needs to be configured in the bootstrapt file, because this file is loaded before the application file for SpringBoot
spring:
application:
name: feiyu-gatway
cloud:
nacos:
discovery:
server-addr: 192.16831.137.: 8848
namespace: ae087f8a-9b9c-4698-9a95-160a1461fc1f
config:
server-addr: 192.16831.137.: 8848
file-extension: yml
namespace: ae087f8a-9b9c-4698-9a95-160a1461fc1f
gateway:
discovery:
locator:
enabled: true The spring Cloud Gateway automatically creates a router for each service based on service discovery. This router forwards the request path starting with the service name to the corresponding service.
lowerCaseServiceId: true For example, a request path with /service-hi/* is routed to a service named service-hi. For example, a request path with /service-hi/* is routed to a service named service-hi.
filters:
- StripPrefix=1
routes:
- id: feiyu-web
uri: lb://feiyu-web
predicates:
- Path=/web/**
filters:
- StripPrefix=1
profiles:
active: dev
Copy the code
3. Create a startup file. Note: @enableDiscoveryClient is required to enable discovery
package com.feiyu.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class GatWayApplication { public static void main(String[] args) { SpringApplication.run(GatWayApplication.class); }}Copy the code
How to select a SpringBoot client
1: Inject dependencies – The choice of dependencies
Note: These two dependencies are required: they are directly supported by SpringBoot and are easy to use
<! <dependency> <groupId>com.alibaba.cloud</groupId> < artifactId > spring - the cloud - starter - alibaba - nacos - discovery < / artifactId > < version > 2.2.1. RELEASE < / version > < / dependency > <! <dependency> <groupId>com.alibaba.cloud</groupId> < artifactId > spring - the cloud - starter - alibaba - nacos - config < / artifactId > < version > 2.2.1. RELEASE < / version > < / dependency >Copy the code
2: Bootstrapt file is configured
The Nacos service configuration is configured to allow the registry to discover the namespace. Namespace ID Server-addr: IP address of nacos installation File-extension: file type of the YML configuration file
Spring: Application: name: feiyu- Web cloud: nacos: Discovery: server-addr: 192.168.31.137:8848 namespace: Ae087f8a-9b9c-4688-9a95-160a1461fc1f config: server-addr: 192.168.31.137:8848 file-extension: Yml namespace: ae087f8a-9b9c-4698-9a95-160a1461fc1f profiles: active: devCopy the code
3: indicates the startup class
Same as above: @enableDiscoveryClient To enable service registration and discovery
package com.feiyu.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GatWayApplication {
public static void main(String[] args) { SpringApplication.run(GatWayApplication.class); }}Copy the code
3. Rationale why the gateway needs to integrate with the registry for load balancing (default 1: 1 switch), the reason is that I think using Nacos for the registry, when the SpringBoot client starts the project, the project will be registered in the registry, the GateWay gets the record of the registered existence, that is, the project name, and the address of the project name, when we access through the GateWay, Then the gateway will first route through the project name, that is, application.name. At the same time, load balancing can be carried out. The priority value needs to be set (in Nacos)