Getway實現nacos註冊及服務轉發
首先建立一個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,那麼後面啟動的服務,將無法被註冊進閘道器。
注:此文章為原創,任何形式的轉載都請聯絡作者獲得授權並註明出處!
若您覺得這篇文章還不錯,請點選下方的【推薦】,非常感謝!
https://www.cnblogs.com/kiba/p/18160147