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

启动服务

测试路由转发

相关推荐
MrSYJ2 小时前
Redis 做分布式 Session
后端·spring cloud·微服务
岁岁种桃花儿4 小时前
注册中心宕机后,RPC调用还能成功吗?主流框架实测级分析
zookeeper·eureka·rpc
研究司马懿5 小时前
【云原生】Gateway API高级功能
云原生·go·gateway·k8s·gateway api
瑶山5 小时前
Spring Cloud微服务搭建五、集成负载均衡,远程调用,熔断降级
spring cloud·微服务·负载均衡·远程调用·熔断降级
金牌归来发现妻女流落街头18 小时前
【从SpringBoot到SpringCloud】
java·spring boot·spring cloud
7哥♡ۣۖᝰꫛꫀꪝۣℋ20 小时前
Spring-cloud\Eureka
java·spring·微服务·eureka
MonkeyKing_sunyuhua1 天前
docker compose up -d --build 完全使用新代码打包的方法
docker·容器·eureka
Java后端的Ai之路1 天前
【Spring全家桶】-一文弄懂Spring Cloud Gateway
java·后端·spring cloud·gateway
vx_Biye_Design2 天前
【关注可免费领取源码】房屋出租系统的设计与实现--毕设附源码40805
java·spring boot·spring·spring cloud·servlet·eclipse·课程设计