Nacos:SpringCloud Nacos-CSDN博客
*****************************************************
什么是网关?
顾明思议,网关就是网络的关口。数据在网络间传输,从一个网络传输到另一网络时就需要经过网关来做数据的路由和转发以及数据安全的校验。
更通俗的来讲,网关就像是以前园区传达室的大爷。
外面的人要想进入园区,必须经过大爷的认可,如果你是不怀好意的人,肯定被直接拦截。
外面的人要传话或送信,要找大爷。大爷帮你带给目标人。
现在,微服务网关就起到同样的作用。前端请求不能直接访问微服务,而是要请求网关:
网关可以做安全控制,也就是登录身份校验,校验通过才放行
通过认证后,网关再根据请求判断应该访问哪个微服务,将请求转发过去
新建一个springboot项目,导入如下依赖
把src删掉后,目录结构是这样的
将pom.xml里面的一些不必要的东西去掉(build dependencies),这个pom文件将作为parent来管理子模块的依赖
- "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.0modelVersion>
- <groupId>com.examplegroupId>
- <artifactId>demo-gateway02artifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>demo-gateway02name>
- <description>demo-gateway02description>
- <properties>
- <java.version>17java.version>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
- <spring-boot.version>3.0.2spring-boot.version>
- <spring-cloud-alibaba.version>2022.0.0.0-RC2spring-cloud-alibaba.version>
- <spring-cloud.version>2022.0.0-RC2spring-cloud.version>
- properties>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-dependenciesartifactId>
- <version>${spring-cloud.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-dependenciesartifactId>
- <version>${spring-boot.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-alibaba-dependenciesartifactId>
- <version>${spring-cloud-alibaba.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- dependencies>
- dependencyManagement>
-
- <repositories>
- <repository>
- <id>spring-milestonesid>
- <name>Spring Milestonesname>
- <url>https://repo.spring.io/milestoneurl>
- <snapshots>
- <enabled>falseenabled>
- snapshots>
- repository>
- repositories>
-
- project>
新建一个user-service模块,导入需要的依赖
修改pom文件 (去掉properties,dependence management 引入parent)
- "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.0modelVersion>
- <groupId>com.examplegroupId>
- <artifactId>user-serviceartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>user-servicename>
- <description>user-servicedescription>
-
- <parent>
- <groupId>com.examplegroupId>
- <artifactId>demo-gateway02artifactId>
- <version>0.0.1-SNAPSHOTversion>
- parent>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.1version>
- <configuration>
- <source>17source>
- <target>17target>
- <encoding>UTF-8encoding>
- configuration>
- plugin>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <version>${spring-boot.version}version>
- <configuration>
- <mainClass>com.example.UserServiceApplicationmainClass>
- <skip>trueskip>
- configuration>
- <executions>
- <execution>
- <id>repackageid>
- <goals>
- <goal>repackagegoal>
- goals>
- execution>
- executions>
- plugin>
- plugins>
- build>
-
- project>
在parent的pom文件下修改打包方式为pom 并标记modules
注册nacos服务
user-service的端口号为8090
- # 应用服务 WEB 访问端口
- server.port=8090
-
- # Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html
- spring.application.name=user-service
- # Nacos认证信息
- spring.cloud.nacos.discovery.username=nacos
- spring.cloud.nacos.discovery.password=nacos
- # Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
- #虚拟机的ip为192.168.168.168
- spring.cloud.nacos.discovery.server-addr=192.168.168.168:8848
- # 注册到 nacos 的指定 namespace,默认为 public
- spring.cloud.nacos.discovery.namespace=public
写一个控制器,用于等下测试
- package com.example.controller;
-
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
-
- @RestController
- @RequestMapping("/home")
- public class UserController {
- @GetMapping("hello")
- public String hello() {
- return "this is user-service";
- }
- }
order-service模块的创建如法炮制
parent的pom也要知道它的存在
注册nacos服务 ,服务的端口号为8091
控制器
做完这些工作后,项目的结构如下
试着启动order-service 和 user-service 在nacos可以看到注册的两个服务
新建一个gateway-service ,导入如下依赖
不能导入web的起步依赖,因为web里面默认使用tomcat服务器,gateway自带了netty服务器,两者会冲突
需要额外导入loadbalancer 用于负载均衡
gateway的pom文件如下
- "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.0modelVersion>
- <groupId>com.examplegroupId>
- <artifactId>gateway-serviceartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>gateway-servicename>
- <description>gateway-servicedescription>
- <parent>
- <groupId>com.examplegroupId>
- <artifactId>demo-gateway02artifactId>
- <version>0.0.1-SNAPSHOTversion>
- parent>
- <dependencies>
-
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-loadbalancerartifactId>
- dependency>
-
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-gatewayartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.1version>
- <configuration>
- <source>17source>
- <target>17target>
- <encoding>UTF-8encoding>
- configuration>
- plugin>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <version>${spring-boot.version}version>
- <configuration>
- <mainClass>com.example.GatewayServiceApplicationmainClass>
- <skip>trueskip>
- configuration>
- <executions>
- <execution>
- <id>repackageid>
- <goals>
- <goal>repackagegoal>
- goals>
- execution>
- executions>
- plugin>
- plugins>
- build>
- <repositories>
- <repository>
- <id>spring-milestonesid>
- <name>Spring Milestonesname>
- <url>https://repo.spring.io/milestoneurl>
- <snapshots>
- <enabled>falseenabled>
- snapshots>
- repository>
- repositories>
-
- project>
parent的pom文件也要知道它的存在
在application.properties中注册nacos服务,编写gateway转发规则
- # Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html
- spring.application.name=gateway-service
-
- # Nacos认证信息
- spring.cloud.nacos.discovery.username=nacos
- spring.cloud.nacos.discovery.password=nacos
- # Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
- spring.cloud.nacos.discovery.server-addr=192.168.168.168:8848
- # 注册到 nacos 的指定 namespace,默认为 public
- spring.cloud.nacos.discovery.namespace=public
-
- # gateway 请求配置
-
- # 允许跨域
- spring.cloud.gateway.globalcors.cors-configurations.[/**].allowed-origin-patterns=*
-
- spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedHeaders=*
- spring.cloud.gateway.globalcors.cors-configurations.[/**].allowCredentials=true
-
- # 允许以下请求方式的请求
- spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedMethods[0]=GET
- spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedMethods[1]=POST
- spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedMethods[2]=PUT
- spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedMethods[3]=DELETE
- spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedMethods[4]=OPTIONS
- spring.cloud.gateway.discovery.locator.enabled=true
-
-
- # 配置gateway的转发规则
-
-
- # user-service
- # id 需要跟服务注册中心一致
- spring.cloud.gateway.routes[0].id=user-service
- # uri值需要跟服务注册中心一致
- spring.cloud.gateway.routes[0].uri=lb://user-service
- # 路径里面带user就需要转发到user-service
- spring.cloud.gateway.routes[0].predicates[0]=Path=/user/**
- # 移除前缀
- spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1
-
- # order-service
- # id 需要跟服务注册中心一致
- spring.cloud.gateway.routes[1].id=order-service
- # uri值需要跟服务注册中心一致
- spring.cloud.gateway.routes[1].uri=lb://order-service
- # 路径里面带order就需要转发到order-service
- spring.cloud.gateway.routes[1].predicates[0]=Path=/order/**
- # 移除前缀
- spring.cloud.gateway.routes[1].filters[0]=StripPrefix=1
最终的目录结构如下
现在启动 user-service order-service gateway-service,在nacos的服务列表中可以看到我们注册的3个服务
现在访问一下user-service和order-service的控制器,检查是否能够正常访问(注意看端口)
现在我们通过网关访问一下这两个模块,可以看到成功访问了
由此案例,我们可以感受到gateway带给我们的方便。有了gateway,前端就只需要请求8080端口,以模块进行路径的书写,就能访问所有的后端接口了,同时gateway也解决了跨域的问题。