springcloud-eureka与gateway简易搭建

目录

eureka

Spring Cloud Eureka主要负责实现微服务架构中的服务治理功能,简易搭建步骤为:

新建euereka-server项目

创建maven项目,在pom.xml中配置以下依赖信息:

复制代码
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>

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

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

新建服务启动类

复制代码
@EnableEurekaServer
@SpringBootApplication
public class StartApplication {
    public static void main(String args[]){
        SpringApplication.run(StartApplication.class,args);
    }
}

配置相关属性

在application.properties中增加如下配置:

复制代码
spring.application.name=eureka-server
server.port=8761
# 不向注册中心注册自己
eureka.client.register-with-eureka=false
# 不需要检索服务
eureka.client.fetch-registry=false
# 关闭保护机制,使服务正常退出,方便开发与调试
eureka.server.enableSelfPreservation=false
eureka.client.service-url.defaultZone=http://*.*.226.80:8762/eureka/

启动服务

启动服务后,可通过eureka提供的web控制台查看服务注册状态

编写微服务进行注册测试

新建maven项目,在pom.xml中增加如下依赖:

复制代码
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

在application.properties中增加如下配置:

复制代码
eureka.client.register-with-eureka=true
eureka.client.fetchRegistry=true
eureka.client.server.waitTimeInMsWhenSyncEmpty=0
eureka.client.service-url.defaultZone=http://10.18.226.209:5054/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=springCloud-test:5051

启动服务,可以看到已经成功注册到eureka上

至此eureka已经部署完成

SpringCloudGateway

SpringCloudGateway作为SpringCloud生态系中的网关,为微服务架构提供统一的路由管理,并且根据http请求进行相应的匹配、断言、过滤,其简易搭建方法如下:

新建gateway项目

新建maven项目,在pom.xml中增加如下依赖:

复制代码
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>

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

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

配置相关属性

在application.properties中增加如下配置:

复制代码
spring.application.name=springCloud-Gateway-test
server.port=5052
server.tomcat.uri-encoding=utf-8
###############################################################
#默认所有服务转发操作
spring.cloud.gateway.discovery.locator.enabled=true
#小写服务名
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

启动服务

测试路由转发

相关推荐
日取其半万世不竭6 小时前
PeerTube 部署指南:自建视频托管平台
云原生·eureka·音视频
budingxiaomoli6 小时前
多机部署,负载均衡-LoadBalancer
运维·spring cloud·负载均衡
暗夜猎手-大魔王6 小时前
转载--AI Agent 架构设计:Gateway 架构设计(OpenClaw、Claude Code、Hermes Agent 对比)
gateway
SarL EMEN8 小时前
Gateway Timeout504 网关超时的完美解决方法
gateway
空中海8 小时前
Docker入门到精通
java·docker·eureka
phltxy8 小时前
告别繁琐URL!Spring Cloud OpenFeign 优雅实现微服务远程调用
spring·spring cloud·微服务
2601_949194261 天前
Gateway Timeout504 网关超时的完美解决方法
gateway
budingxiaomoli1 天前
SpringCloud概述
java·spring cloud·微服务
空中海2 天前
第五篇:网关篇 — Spring Cloud Gateway
spring cloud
码点滴2 天前
私有 Gateway 接入企业 IM:从消息路由到多租户隔离——Hermes Agent 工程实战
人工智能·架构·gateway·prompt·智能体·hermes