SpringCloud 详细网关gateway 与 eureka注册中心 以及如何结合使用的案例讲解

分类: best 365官网体育投注 时间: 2025-10-31 10:59:36 作者: admin 阅读: 7758
SpringCloud 详细网关gateway 与 eureka注册中心 以及如何结合使用的案例讲解

1. 什么是gateway,什么是eureka注册中心以及为什么需要注册

首先博主经过一周的到处乱撞,各种奇葩案例,走过的坑就希望大伙不去踩坑。所以下面的案例就直击重点,采用最简单的案例来为大家讲解,你一定会明白的!!!

1.1什么是eureka Service注册中心

首先要明白为什么需要注册,这里要用到SpringCloud的独立模块的概念,当我们一个大型项目有很多模块时(例如 一个购物网站 有购物车模块 用户管理模块 商品模块等),如果需要某个模块的接口去访问资源时,

我们就得知道它的具体ip +端口+接口 。这时如果有一个统一的管理购物网站的东西,我输入

统一管理地址+user----》访问用户模块

统一管理地址+cart-----》访问购物车模块

这时有同学就会疑惑 我一个项目的ip不是同一个吗,还需要一个统一管理地址不是多此一举。如果你的项目的不同模块处于不同服务器上,甚至所处区域都不一样时,ip就会衍生出非常之多(大型公司 淘宝等为了减轻服务器压力,在不同的地区放置了应对不同功能的服务器,整合成一个淘宝),这样是不是就突出了统一管理地址的作用。

而注册中心就是把各个模块统一注册到eureka 注册中心中,只需要用网关+模块的应用名称(可以理解为模块的名字)就能访问到该模块的相关数据。

1.2什么是gateway

gateway通俗的简单理解就是网络协议转换器,下面的案例中gateway暂时仅充当公司项目中的断言-》转发的目的。结合eureka注册中心,gateway网关要想通过别名发现注册到eureka中的服务,他自己也得注册进去。下面上实例。

2. eureka Service模块注册流程

2.1新建一个SpringBoot 项目(具体新建就不说了,我取名为eureka-demo)

主项目下只留pom,也就是上图的文件布局,为什么只留pom 是因为后面的模块才是各个具体功能模块,而主项目的pom是为了给模块的pom添加继承,统一各个模块的版本,方便管理。

eureka-demo的配置文件(pom.xml)

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">

4.0.0

org.example

eureka-demo

pom

1.0-SNAPSHOT

eureka-service

eureka-server02

server-provider

server-consumer

spring-boot-starter-parent

org.springframework.boot

2.3.6.RELEASE

Hoxton.SR1

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

2.2 新增模块eureka-server

2.3 新建resources资源文件夹,编写该模块的pom文件

我这里时新建过了,所以是取消mark标记为资源文件,你们新建时就显示标记就行。

pom文件如下(之后如果启动模块失败就三处test 整个文件目录)

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.example

eureka-service

1.0-SNAPSHOT

org.example

eureka-demo

1.0-SNAPSHOT

#====继承父版本,达到版本统一。

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-netflix-eureka-server

org.springframework.cloud

spring-cloud-starter

2.4 编写application.yml文件(得在本模块resources下新建)

server:

port: 8761

spring:

application:

name: eureka-service

eureka:

instance:

hostname: eureka01

prefer-ip-address: true #是否使用ip注册

instance-id: ${spring.cloud.client.ip-address}:${server.port} #ip:port

client:

register-with-eureka: true #是否将自己注册到注册中心

fetch-registry: true #是否从注册中心服务注册信息

service-url: #注册中心对外暴露的注册地址

defaultZone: http://localhost:8761/eureka/

# http://${eureka.instance.hostname}:${server.port}/eureka/

删除新建本模块下的App启动类(就是想改个名字方便启动时辨认)

package org.example;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

import org.springframework.web.bind.annotation.GetMapping;

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

@EnableEurekaServer

@SpringBootApplication

@RestController

public class EurekaServiceApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServiceApplication.class);

}

@GetMapping("/user")

public String getUser(){

return "gateway ok";

}---------后续为了gateway来测试它,本来是要写在controller中的,这里只是为了方便测试。

}

这里面写了一个简单的测试接口,方便gateway 拦截,并用网关+Application-name属性去访问该模块。暂时写道这里只需要启动该模块,访问http://localhost:8761 进入如下界面说明注册成功!!!

看到这里,总结以下:所谓的eureka注册就是通过配置yml 和pom依赖并通过eureka可视化平台看到自己的注册模块,没有难度吧。下面配置gateway

3. 配置gateway项目文件

3.1新建项目。

常规的新建Springboot文件,pom依赖下面有,所以不勾选依赖看下面的pom注释也能明白有哪些依赖。

3.2 pom编写

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

gateway-study

0.0.1-SNAPSHOT

gateway-study

Demo project for Spring Boot

1.8

UTF-8

UTF-8

2.3.7.RELEASE

Hoxton.SR9

org.springframework.cloud

spring-cloud-starter-gateway

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-dependencies

${spring-boot.version}

pom

import

org.apache.maven.plugins

maven-compiler-plugin

3.8.1

1.8

1.8

UTF-8

org.springframework.boot

spring-boot-maven-plugin

2.3.7.RELEASE

com.example.gatewaystudy.GatewayStudyApplication

repackage

repackage

3.3 yml配置以及讲解

server: port: 9527spring: application: name: cloud-gateway cloud: gateway: discovery: locator: enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名进行路由" routes: - id: eureka-service #payment_routh #路由的ID,没有固定规则,但要求唯一,建议配合服务名 #uri: http://localhost:8761 #匹配后提供服务的路由地址 没有进行负载均衡 uri: lb://eureka-service #匹配后提供服务的路由地址-------这里用的动态路由格式统一为 lb://注册进eureka服务中心的名字 predicates: - Path=/server01/** #断言,路径相匹配的进行路由--------断言也就是匹配方式,当识别到/servero1/**时就会跳转上面的uri filters: #这个必须写 - StripPrefix=1 # 请求/name/bar/foo,去除掉前面两个前缀之后,最后转发到目标服务的路径为/foo-----这里写的是只去掉一个,多了自然会导致路径错误,不同的访问url配置也不同eureka: instance: hostname: cloud-gateway-service client: # 服务提供者provider注册金eureka服务列表内 service-url: register-with-eureka: true fetch-registry: true defaultZone: http://localhost:8761/eureka/

gateway启动类如下

package com.example.gatewaystudy;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.gateway.route.RouteLocator;

import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.context.annotation.Bean;

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

import org.springframework.web.reactive.result.view.Rendering;

@EnableEurekaClient

@SpringBootApplication

public class GatewayStudyApplication {

public static void main(String[] args) {

SpringApplication.run(GatewayStudyApplication.class, args);

}

}

前面说过,gateway想要通过Application-name发现注册到eureka的服务模块,自己就也得注册进去,多的部分也就是gateway配置部分。(总结就是匹配server01 然后转发到上面的uri)

3.4 启动gateway,进行跨项目访问

输入 localhost:9527/server01/user 得到下面的结果就全部完成了

需要注意一点:gateway项目引入的依赖是:

spring-cloud-starter-netflix-eureka-client而模块服务引入的依赖是

spring-cloud-netflix-eureka-server

总结上面的例子,我只举例了一个服务中心的注册。因为对于简单的理解gateway+eureka足够了,而使用yml的配置方式也是公司常用的使用方式。希望能帮助大家入门。少走弯路!!!!

相关文章

阮林的意思
dnf多久能转职
包含【眼】的成语有哪些
win11电脑全屏后为什么会弹出桌面?如何避免这种情况?