一.启动nacos
1.查看linux的nacos是否启动
docker ps

2.查看是否安装了nacos
前面是你的版本,后面的names是你自己的,我们下面要启动的就是这里的名字。
docker ps -a

3.启动nacos并查看是否启动成功

二.创建网关项目
1.创建idea的maven项目


2.向pom.xml文件里添加依赖
XML
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<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.6.13</spring-boot.version>
<jwt.version>0.7.0</jwt.version>
<fastjson.version>1.2.60</fastjson.version>
<spring-cloud-alibaba.version>2.0.3.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</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>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
3.创建启动类

java
package com.jr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class,args);
}
}

如果导入别人的项目,依赖是有传递性的这样你的项目里依赖的中间件就会很多,可以通过exclude属性排除掉。这样其他的中间件就不会启动了。
@SpringBootApplication(exclude = {XXXX.class, YYYY.class})
网关是一个非常干净的工程,不会添加其它中间件的配置,所以网关工程里只有一个Application的启动类和配置文件。启动类的注解@SpringBootApplication注解中可能开启的其它中间件的@EnableXXX,可以把其它中间件的注解排除掉,保证网关只启动自己的应用,只完成网关功能。
4.创建bootstrap.yml配置文件
java
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.130.39:8848
config:
server-addr: 192.168.130.39:8848
inetutils:
preferred-networks: 192.168.116

下面这个看我另一个博客在idea使用nacos微服务-CSDN博客

5.创建application.properties配置文件

##设置项目的注册名
spring.application.name=gateway
##设置项目的端口号 以上两个设置放在同一个配置文件里。
server.port=300
6.在resources创建一个index.html页面(一定要是index.html界面哈)
还有得先建立一个static或者public包,把index.html放里面

html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index.html页面</h1>
</body>
</html>

7.启动idea项目

启动结果:


三.配置网关
1.向pom.xml里添加部分依赖
先把下面代码的依赖删了,网关不需要前端了,所以要去除web依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
否则会出现下面的错误

记得别放<dependencyManagement></dependencyManagement>这个标签去了
XML
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2.在resources中创建application.yml文件
这注意要在resources右键建立,别在文件里创建application.yml,之前创建的resources下的两个配置文件也是这样,一定在resoures外面。

这里我把public文件夹收起来了(如果为来有多个文件夹,全部收起来),但是外面的三个配置文件还在外面,他三个必须在外面。

这里拿百度举例吧
- id: baidu_route(路由id唯一,是一个数组,可以配置多个路由)
uri: https://www.baidu.com(路由到的地址)
predicates:(断言)
-
Query=url, baidu(如果存在url和baidu,那么跳转到uri地址)
spring:
cloud:
gateway:
routes:
- id: baidu_route
uri: https://www.baidu.com
predicates:
- Query=baidu
3.启动idea项目
自己浏览器地址栏输入下面

跳转成功,http://localhost:300/baidu不会跳转到百度,http://localhost:300/baidu?baidu才会跳转到百度,是根据参数做断言。

四.我的项目结构
这里就结束了,这是我的项目结构,希望能帮到大家。
