SpringCloud2模块系列:Gateway(接口转发)

1 Gateway接口转发流程

在这里插入图片描述

图1 Gateway接口转发流程

2 Usage

新建Gateway单体项目.

2.1 pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<!-- <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.8.RELEASE</version>
		<relativePath/>
	</parent> -->
	<parent>
		<groupId>com.personal</groupId>
		<artifactId>microspersonal</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.personal</groupId>
	<artifactId>microspersonal-gateway</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>microspersonal-gateway</name>
	<description>Demo project for Gateway</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-gateway</artifactId>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-client</artifactId>
			<version>2.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		  </dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2.2 application.yml

配置文件中,开启locator,即可在网关中通过单体服务名称访问指定的单体服务,此时,需要在网关ip:port后添加单体服务名的大写形式访问单体服务,如gateway的服务为http://domain-name:8000,单体服务名称(application.name=micros-user),访问micors-user服务的形式为:http://domain-name:8000/MICROS-USER/URI,其中URI为micros-user的统一资源定位符.若使用单体服务名通过网关访问单体服务,如http://domain-name:8000/micros-user/URI,只需添加lowerCaseServiceId:true,开启小写服务id.

spring:
  devtools: 
    restart: 
      enabled: true
  profiles: dev
  application: 
    name: micros-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true# 开启以应用名代理服务,即通过应用名称访问接口
          lowerCaseServiceId: true# 微服务名称小写
      routes:
      - id: MICROS-USER # 微服务应用名大写
        uri: lb://MICROS-USER
        predicates:
          - Path=/MICROS-USER/**
      # routes:
      # - id: baidu # 代理URL测试
      #   uri: https://www.baidu.com
      #   predicates:
      #     - Path=/baidu/**
server:
  port: 8094
  servlet:
    session:
      timeout: PT10S
    # context-path: /user
eureka: 
  client: 
    service-url: 
      defaultZone: http://localhost:8090/eureka/eureka
logging: 
  level: 
    root: INFO

management: #开启SpringBoot Admin的监控
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

2.3 启动类

package com.personal.microspersonalgateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SpringBootApplication
@EnableEurekaClient
public class MicrospersonalGatewayApplication {
	static Logger logger = LoggerFactory.getLogger(MicrospersonalGatewayApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(MicrospersonalGatewayApplication.class, args);
		logger.info("Gateway服务启动");
	}
}

2.4 启动顺序

Eureka->Gateway->MICROS-USER
其中,MICROS-USER参见博客:SpringCloud2模块系列:Feign(服务间调用)第2.2节.

2.5 访问

  • 格式
http://gateway-ip:gateway-port/servicdId/serviceURI
  • 样例
    单体服务名大写形式:
http://localhost:8094/MICROS-USER/api/api/page/user/query

单体服务名小写形式:

http://localhost:8094/micros-user/api/api/page/user/query

3 注意事项

3.1 异常

org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean

  • 原因
    SpringCloud Gateway不兼容springboot-web启动
  • 方案
    若需要使用web方式启动,添加webflux依赖

3.2 单体服务名大小写

网关的默认代理模式为服务名大写形式,即MICROS-USER,若需要通过网关通过单体服务名称的小写形式访问单体服务,需要添加配置文件lowerCaseServiceId: true.


【参考文献】
[1]https://blog.csdn.net/weixin_42033269/article/details/89847988
[2]https://www.jb51.net/article/162143.htm
[3]https://blog.csdn.net/qq_41402200/article/details/94333830
[4]https://www.cnblogs.com/crazymakercircle/p/11704077.html
[5]https://www.cnblogs.com/babycomeon/p/11161073.html
[6]https://blog.csdn.net/Xin_101/article/details/106299514
[7]https://blog.csdn.net/Xin_101/article/details/106837895
[8]https://www.shangmayuan.com/a/d812e96f953649e2acf9a60f.html
[9]https://blog.csdn.net/weixin_30894389/article/details/98657087
[10]https://blog.csdn.net/SZMS1/article/details/84241196

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值