Teng's blog Teng's blog
首页
Java
H5前端
GitHub (opens new window)
首页
Java
H5前端
GitHub (opens new window)
  • 01.项目介绍
  • 02.后台系统-搭建项目
  • 03.后台系统-医院设置模块
  • 04.后台系统-统一异常处理
  • 05.后台系统-统一日志处理
  • 06.后台系统-搭建管理后台前端
  • 07.后台系统-医院设置前端
  • 08.后台系统-数据字典
  • 09.SpringCache+Redis缓存数据
  • 10.集成与配置Nginx
  • 11.启动医院接口模拟系统
  • 12.后台系统-上传医院信息
  • 13.后台系统-上传科室信息
  • 14.后台系统-上传排班信息
  • 15.搭建服务注册中心Nacos
  • 16.后台系统-医院管理
  • 17.后台系统-排班管理
  • 18.搭建服务网关Gateway
    • Gateway入门
    • 创建service-gateway项目
    • 配置pom文件
    • 修改 application.properties文件
    • 创建启动类
    • 全局跨域问题
    • 测试
  • 19.前台系统-搭建前端环境
  • 20.前台系统-首页
  • 21.前台系统-医院详情页
  • 22.前台系统-用户登录
  • 23.后台系统-短信服务
  • 24.用户认证与网关整合
  • 25.前台系统-微信登录
  • 26.前台系统-实名认证
  • 27.前台系统-就诊人管理
  • 28.后台系统-平台用户管理
  • 29.前台系统-预约挂号详情
  • 30.前台系统-预约确认
  • 31.前台系统-预约下单
  • 32.前台系统-订单管理
  • 33.后台系统-订单管理
  • 34.前台系统-微信支付
  • 35.前台系统-取消预约
  • 36.前台系统-就医提醒
  • 37.后台系统-预约统计
  • 38.小结
  • 附录:医院接口模拟系统说明
  • 附录:在线预约挂号API接口文档
  • Project-尚医通
Shetengteng
2021-12-08

18.搭建服务网关Gateway

# Gateway入门

提示

下面对Spring Cloud Gateway 进行快速入门,如果已经掌握可以略过

快速访问 🚀 Spring Cloud Gateway

是spring官方基于Spring 5.0、Spring Boot2.0和Project Reactor等技术开发的网关

- name: 快速访问 🚀 Spring Cloud Gateway
  desc: 是spring官方基于Spring 5.0、Spring Boot2.0和Project Reactor等技术开发的网关
  link: /pages/87366e/
  bgColor: '#DFEEE7'
  textColor: '#2A3344'
1
2
3
4
5

# 创建service-gateway项目

点击yygh_parent右键,New->Module... 选择 Maven->Next 如下,创建service-gatewaty项目

# 配置pom文件

修改service-gateway 配置如下

<?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>yygh_parent</artifactId>
        <groupId>com.stt.yygh</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <version>1.0</version>
    <packaging>jar</packaging>
    <name>service-gateway</name>
    <description>service-gateway</description>
    <artifactId>service-gateway</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.stt.yygh</groupId>
            <artifactId>common-util</artifactId>
            <version>0.0.1</version>
        </dependency>

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

        <!-- 服务注册 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

# 修改 application.properties文件

配置端口号 8080

配置服务名称 service-gateway

配置注册中心url

设置 service-hosp的路由

  • id设置为路由的服务名称
  • predicates 中设置的Path中的 一个 * 表示匹配任意一个路径,** 表示匹配任意多个路径

设置 service-cmn的路由

# 服务端口
server.port=8080
# 服务名
spring.application.name=service-gateway

# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

#使用服务发现路由
spring.cloud.gateway.discovery.locator.enabled=true

#设置路由id
spring.cloud.gateway.routes[0].id=service-hosp
#设置路由的uri
spring.cloud.gateway.routes[0].uri=lb://service-hosp
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[0].predicates=Path=/*/hosp/**

#设置路由id
spring.cloud.gateway.routes[1].id=service-cmn
#设置路由的uri
spring.cloud.gateway.routes[1].uri=lb://service-cmn
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[1].predicates=Path=/*/cmn/**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 创建启动类

创建相应的包以及ServiceGatewayApplication类

package com.stt.yygh;

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

@SpringBootApplication
public class ServiceGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceGatewayApplication.class, args);
    }
}
1
2
3
4
5
6
7
8
9
10
11

# 全局跨域问题

由于ip,port,以及协议的不同,会产生ajax调用的跨域问题,之前在对应服务的controller上添加了@CrossOrigin注解解决,局限性在于每个controller类都需要添加,比较繁琐,同时增加了配置的耦合

有了gateway,所有的请求通过gateway进行转发,那么只要解决了gateway中的跨域问题,那么其他服务的controller就不需要配置跨域注解,具体操作如下

在service-gateway中添加相应的包与配置类 CorsConfig

package com.stt.yygh.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*"); // 允许任何方法都可以跨域访问
        config.addAllowedOrigin("*"); 
        config.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

注意:添加配置类之后,其他所有的controller需要去除 @CrossOrigin 注解

service-hosp

  • 去除 DepartmentController 的 @CrossOrigin 注解
  • 去除 HospitalController 的 @CrossOrigin 注解
  • 去除 HospitalSetController 的 @CrossOrigin 注解
  • 去除 ScheduleController 的 @CrossOrigin 注解

service-cmn

  • 去除 DictController 的 @CrossOrigin 注解

# 测试

修改管理系统前端url访问地址,看各项功能是否正常

.env.development文件

# base api
VUE_APP_BASE_API = 'http://localhost:8080'
1
2
Last Updated: 2022/01/16, 11:29:51
17.后台系统-排班管理
19.前台系统-搭建前端环境

← 17.后台系统-排班管理 19.前台系统-搭建前端环境→

Theme by Vdoing | Copyright © 2021-2022 Shetengteng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式