• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SpringCloudAlibaba学习 03整合GateWay网关

武飞扬头像
杜小舟
帮助1

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



这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggcbbb
系列文章
更多 icon
同类精品
更多 icon
继续加载