首先创建一个module——kiba-getway,如下图:
我们新建的项目缺少resources文件夹,打开project structure——moudules,右键java,创建文件夹,如下图:
然后选择resources,再点击【mark a directory as a resources root】,设置resource root,如下图:
然后修改pom里的组件版本,文件代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kibaframework</groupId>
<artifactId>kiba-getway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kiba-getway</name>
<description>kiba-getway</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.boot.version>2.3.12.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR11</spring.cloud.version>
<spring.cloud.alibaba.version>2.2.10-RC1</spring.cloud.alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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.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-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<configuration>
<mainClass>com.example.kibagetway.KibaGetwayApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
然后在resources下添加bootstrap.yml文件,编写内容如下:
spring:
application:
name: kiba-gateway
cloud:
nacos:
discovery:
server-addr: 10.1.0.145:8848
username: nacos
password: nacos
auto-register: true
reactive:
enabled: false #关闭响应式发现服务,不需要返回消息,增加注册速度,简化复杂度
gateway:
discovery:
locator:
enabled: true #网关能够通过特定的机制发现和使用其他服务。该配置会将其他服务自动根据服务名进行映射
httpclient:
max-header-size: 104857600
loadbalancer:
retry:
enabled: true #启动负载均衡重试
上文设置了gateway.discovery.locator.enabled=true。所以会将其他服务自动根据服务名进行映射。
项目结构很简单,如下图:
然后启动项目,将网关注册进nacos,如下。
我的nacos里已经有一个服务了。正常请求地址——localhost:8588/user/getFiegn,如下图:
请求网关地址——localhost:9010/app-kiba-feign/user/getFiegn,如下图:
网关成功转发了请求。
注:getway项目一定要最后启动,他是在启动时进行一次服务扫描,然后将服务信息自动配置进去的。如果先启动getway,那么后面启动的服务,将无法被注册进网关。
注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!