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

启动服务

测试路由转发

相关推荐
小坏讲微服务3 小时前
Docker-compose搭建Docker Hub镜像仓库整合SpringBootCloud
运维·分布式·spring cloud·docker·云原生·容器·eureka
z***43843 小时前
当遇到 502 错误(Bad Gateway)怎么办
gateway
2501_941149504 小时前
区块链技术:从金融革命到行业变革的多维探索
eureka
斯普信专业组6 小时前
深入理解 Gateway API:设计原则、核心资源与数据流
运维·网关·gateway
百***68826 小时前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
漂流幻境6 小时前
Spring cloud gateway 跨域配置与碰到的问题
java·gateway·springcloud·跨域
q***78786 小时前
SpringGateway网关(Spring Gateway是Spring自己编写的,也是SpringCloud中的组件)
spring·spring cloud·gateway
q***21606 小时前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
2501_941403768 小时前
科技创新与可持续发展:如何让技术推动环保未来
eureka