(二) 标题springboot 2.6.6 整合 springCloud 、nacos,加入网关的配置
前言:
第一篇文章开发案例的springboot的版本是2.4.4开发的,所以从第二篇开始做下改动,由springboot2.4.4的版本改为springboot2.6.6的版本(改动的主要原因是,第二篇的案例是重新写的,由于第一篇的案例因为电脑的重置已经没有了,jdk也由8变成了17,因此重写了案例),项目目录和第一篇案例一样,只是项目的maven依赖做了小改动。改动如下:
1、改动前的项目案例springboot-nacos-demo父工程中依赖如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot-nacos-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springboot-nacos-serivce</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<!-- spring cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.0</version> # 注意版本号,我项目中sprinboot版本为2.4.4,因此springcloud版本为2020.0.0
<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>
</project>
2、改动后的项目案例springboot-nacos-demo父工程中依赖如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot-nacos-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springboot-nacos-service</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
</parent>
<dependencyManagement>
<dependencies>
<!-- springcloud版本 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring-cloud-alibaba 的版本控制-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
3、 改动前的项目案例 springboot-nacos-serivce 子父模块依赖如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-nacos-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>spring-boot-producer</module>
<module>spring-boot-customer</module>
</modules>
<artifactId>springboot-nacos-serivce</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- nacos -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 熔断器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
</dependencies>
</project>
4、 改动前的项目案例 springboot-nacos-serivce 子父模块依赖如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-nacos-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-nacos-service</artifactId>
<packaging>pom</packaging>
<modules>
<module>spring-boot-producer</module>
<module>spring-boot-customer</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 服务调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- 熔断器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
</dependencies>
</project>
注:由于版本的不同,有的依赖版本做了轻微的改动,应该是由于springboot版本升级了,对应的springclout 和 spring-cloud-alibaba-dependencies 版本对应的也升级了,修正了之前版本的缺陷,因为在新的版本中spring-cloud-starter-alibaba-nacos-discovery依赖的改动如下:
springboot2.4.4 版本的 springboot-nacos-service 的pom文件如下:
<!-- nacos -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
springboot2.6.6 版本的 springboot-nacos-service 的pom文件如下,在2.6.6的版本中,不再需要移除 spring-cloud-starter-netflix-ribbon 了
<!-- nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
正文——整合gateway网关
一、在子父模块 springboot-nacos-service 下新建项目,项目名为:springboot-nacos-gateway,目录如下
二、在 springboot-nacos-gateway 项目的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-nacos-service</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-nacos-gateway</artifactId>
<description>微服务网关</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<!--
说明:
在微服务网点的依赖中,需要如下依赖:
spring-cloud-starter-alibaba-nacos-discovery(注册中心),
spring-cloud-starter-openfeign(Feign服务调用)
-->
<dependencies>
<!-- gateway网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
</project>
说明:因为网关的依赖 spring-cloud-starter-gateway 和 spring-boot-starter-web 的依赖冲突,因此,在子父模块中的pom文件依赖做下改动,移除 springboot-nacos-service 工程下的 spring-boot-starter-web 依赖,分别在子模块 spring-boot-producer 和 spring-boot-customer 的工程中加入 spring-boot-starter-web 依赖
三、spring-cloud-starter-gateway 项目的启动类
package com.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 网关服务启动类
* @Author WEN
* @Date 2022/4/14 15:28
**/
@EnableDiscoveryClient // 开启注册中心,把网关注册到注册中心去
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
四、配置 spring-cloud-starter-gateway 项目的 application.yml 文件
# 网关服务端口
server:
port: 7300
spring:
application:
name: nacos-gateway
cloud:
nacos: # nacos 服务地址
discovery:
server-addr: 127.0.0.1:8848
gateway:
discovery:
locator:
enabled: true # 使用服务发现路由
lower-case-service-id: true # 服务路由名小写
routes:
# 设置路由的ID,理论上是可以随便写,但是一般建议写服务的名称
- id: nacos-producer
# 设置路由的uri,固定写法:lb:// + nacos-producer(此为在nacos中注册的服务名,注:要注意大写小,否则会出现503错误)
uri: lb://nacos-producer
# 设置路由断言,代理 serviceId为NACOS-PRODUCER的 /producer/ 路径
predicates:
- Path=/producer/** # 路径匹配
- id: nacos-customer
uri: lb://nacos-customer
predicates:
- Path=/customer/**
# - id: nacos-customer
# uri: lb://nacos-customer
# predicates:
# 此路径匹配的意思表示:在controller类中,在类上使用注解@RequestMapper("/abc/acl"),此时 /**/acl/** 表示匹配所有符合/acl/的所有路径
# - Path=/**/acl/**
五、启动项目
启动 spring-cloud-starter-gateway、spring-boot-producer、spring-boot-customer 项目,在浏览器访问:
1、先访问 spring-boot-customer 项目,看是否正常访问,访问正常,
浏览器输入:http://localhost:7200/customer/testCustomer/959595959,输出结果如下:
2、访问 spring-cloud-starter-gateway 项目,浏览器输入:http://localhost:7300/customer/testCustomer/我通过网关访问服务提供者 输出结果如下
至此,整合网关项目完成