Spring Cloud 实战二:Client的创建和高可用

栏目: Java · 发布时间: 7年前

内容简介:Hello大家好,我是初晨,本章我们学习SpringCloud Client的使用。大家有问题和意见可以发邮箱mr_beany@163.com创建步骤与

Hello大家好,我是初晨,本章我们学习SpringCloud Client的使用。大家有问题和意见可以发邮箱mr_beany@163.com

一:创建SpringCloud Client

创建步骤与 创建Spring Cloud Eureka 基本相同,不同的是选取服务时按照下图选择

Spring Cloud 实战二:Client的创建和高可用

选择Eureka Discovery,引入jar包

二:启动上一节中创建的eureka,这一点必不可少,以后启动其他服务时默认eureka处于开启状态!

三:配置client

1:启动类上加上 @EnableDiscoveryClient

表示该服务是一个client端

Spring Cloud 实战二:Client的创建和高可用

2:配置eureka的地址

添加如下配置来告诉client我们的eureka的地址

Spring Cloud 实战二:Client的创建和高可用 注意:这里我们将application修改为yml文件

3:启动项目

Spring Cloud 实战二:Client的创建和高可用 启动之后我们发现项目会自己停止运行。这里我们打开pom文件,添加如下依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>复制代码

4:再次启动,启动成功

Spring Cloud 实战二:Client的创建和高可用

5:打开eureka页面

http://localhost:8761/复制代码

Spring Cloud 实战二:Client的创建和高可用

我们发现client已经注册进eureka中

四:高可用

高可用的意思就是我可以为同一个服务注册多个端口运行,这样当其中一个服务挂掉时,就不会影响项目的正常运行

1:修改client默认端口

Spring Cloud 实战二:Client的创建和高可用

Spring Cloud 实战二:Client的创建和高可用

修改端口号为8081

2:添加client服务

Spring Cloud 实战二:Client的创建和高可用

选择复制

Spring Cloud 实战二:Client的创建和高可用

Spring Cloud 实战二:Client的创建和高可用

修改端口为8082

3:重启两个client

4:打开eureka启动页面

http://localhost:8761/复制代码

Spring Cloud 实战二:Client的创建和高可用

我们可以发现,有两个client注册成功,端口分别为刚配置的8081和8082

五:结尾

感谢大家支持,应用中心接下来陆续更新,感谢大家。

对于springBoot还不了解的朋友可以看我的SpringBoot系列教程


以上所述就是小编给大家介绍的《Spring Cloud 实战二:Client的创建和高可用》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Software Engineering for Internet Applications

Software Engineering for Internet Applications

Eve Andersson、Philip Greenspun、Andrew Grumet / The MIT Press / 2006-03-06 / USD 35.00

After completing this self-contained course on server-based Internet applications software, students who start with only the knowledge of how to write and debug a computer program will have learned ho......一起来看看 《Software Engineering for Internet Applications》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码