【SpringCloudAlibaba学习 03】整合GateWay网关

172 阅读4分钟

@[TOC]

1、创建GateWay子工程项目

我们在父工程项目下创建gateway-config的子工程项目 在这里插入图片描述


2、nacos-config子项目配置pom

<dependencies>
        <!-- Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- Nacos Config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <!-- GateWay-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
    </dependencies>

3、创建bootstrap.yml配置文件

目录结构

在这里插入图片描述

bootstrap.yml配置文件代码

url:
  nacos: localhost:8848 # Nacos地址
spring:
  application:
    name: gateway-config # 当前服务名称
  main:
    # 是因为GateWay组件中的spring-boot-starter-webflux依赖与Springboot中的spring-boot-starter-web依赖出现冲突, 加这个配置就是解决这个冲突的
    web-application-type: reactive
  # 命名空间
  profiles:
    active: test
  # cloud 配置
  cloud:
    nacos:
      discovery:
        # 命名空间
        namespace: ${spring.profiles.active}
        # 注册中心地址
        server-addr: ${url.nacos}
      config:
        namespace: ${spring.profiles.active}
        file-extension: yaml
        # 配置中心地址
        server-addr: ${url.nacos}

4、在Nacos的test空间下添加gateway-config.yaml配置文件

在这里插入图片描述

server:
  port: 9002 # 服务端口

5、创建启动类

目录结构

在这里插入图片描述

GateWayApplication启动类代码

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GateWayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GateWayApplication.class, args);
    }

}

6、启动GateWayApplication启动类

启动程序后,进入Nacos管理页面 Nacos地址:http://localhost:8848/nacos/index.html 账号:nacos 密码:nacos 在Nacos页面中点击服务管理下的服务列表就能看到我们刚刚启动的gateway-config服务了

在这里插入图片描述


7、使用GateWay配置路由、断言,统一管理服务入口

GateWay网关主要的功能是路由、断言和过滤器,将服务入口统一起来,方便对各个服务进行访问、控制,下面我们就创建两个服务,使用一下网关做为入口。


7.1、创建service-impl子工程

后面我再创建服务的时候就只贴一个总的目录结构和代码,接下来,我们创建一个service-impl子工程,这个service-impl子工程下放我们的业务实现服务

目录结构

在这里插入图片描述

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>SpringCloudAlibabaDemo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-impl</artifactId>
    <name>service-impl</name>
    <version>${parent.version}</version>

</project>

7.2、创建user-service-impl服务

目录结构

在这里插入图片描述

pom配置

    <dependencies>
        <!-- Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- Nacos Config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>

bootstrap.yml配置文件

url:
  nacos: localhost:8848 # Nacos地址
spring:
  application:
    name: user-service-impl # 当前服务名称
  # 命名空间
  profiles:
    active: test
  # cloud 配置
  cloud:
    nacos:
      discovery:
        # 命名空间
        namespace: ${spring.profiles.active}
        # 注册中心地址
        server-addr: ${url.nacos}
      config:
        namespace: ${spring.profiles.active}
        file-extension: yaml
        # 配置中心地址
        server-addr: ${url.nacos}

UserServiceApplication启动类代码

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UserServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }

}

UserController代码

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/getUserInfo")
    public String getUserInfo() {
        return "调用了/user/getUserInfo接口";
    }

}

在Nacos管理页面中test空间下配置user-service-impl.yaml

在这里插入图片描述

server:
  port: 9003 # 服务端口

7.2、创建order-service-impl服务

目录结构

在这里插入图片描述

pom配置

    <dependencies>
        <!-- Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- Nacos Config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>

bootstrap.yml配置文件

url:
  nacos: localhost:8848 # Nacos地址
spring:
  application:
    name: order-service-impl # 当前服务名称
  # 命名空间
  profiles:
    active: test
  # cloud 配置
  cloud:
    nacos:
      discovery:
        # 命名空间
        namespace: ${spring.profiles.active}
        # 注册中心地址
        server-addr: ${url.nacos}
      config:
        namespace: ${spring.profiles.active}
        file-extension: yaml
        # 配置中心地址
        server-addr: ${url.nacos}

OrderServiceApplication启动类代码

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OrderServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }

}

OrderController代码

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @RequestMapping("/getOrderInfo")
    public String getOrderInfo() {
        return "调用了/order/getOrderInfo接口";
    }

}

在Nacos管理页面中test空间下配置order-service-impl.yaml

在这里插入图片描述

server:
  port: 9004 # 服务端口

7.3、在Nacos管理页面中修改gateway-config.yaml配置

在这里插入图片描述

在这里插入图片描述

server:
  port: 9002 # 服务端口
spring:
  cloud:
    gateway:
      routes:
        - id: user-service-impl # 路由ID
          uri: lb://user-service-impl # 匹配的服务名称
          predicates:
            - Path=/user/** # 断言,路径相匹配的进行路由
        - id: order-service-impl # 路由ID
          uri: lb://order-service-impl # 匹配的服务名称
          predicates:
            - Path=/order/** # 断言,路径相匹配的进行路由

7.4、启动、测试

启动user-service-impl、order-service-impl和gateway-config服务,这三个服务启动完成后,我们使用gateway的端口调用刚刚在user-service-impl服务下写的接口 user-service-impl接口地址:localhost:9002/user/getUserInfo

在这里插入图片描述

然后,我们再试一下通过gateway服务的端口,调用order-service-impl服务下的接口 order-service-impl接口地址:localhost:9002/order/getOrderInfo

在这里插入图片描述

成功!




End