查看原文
其他

(三)SpringBoot+SpringCloud —— 高可用的Eureka注册中心

点击上方 "程序员小乐"关注, 星标或置顶一起成长

每天凌晨00点00分, 第一时间与你相约


每日英文

Not easily cut open to others, because others are hilarious, and the pain is yourself.

不要轻易把伤口揭开给别人看,因为别人看的是热闹,而痛的却是自己。


每日掏心

人之所以有一张嘴,而有两只耳朵,原因是听的要比说的多一倍。

来自:XuePeng77 | 责编:乐乐

链接:my.oschina.net/u/2450666/blog/1162821

程序员小乐(ID:study_tech)第 824 次推文   图片来自百度


往日回顾:阿里的Bug智能定位神器!直接定位线上Bug,超给力超牛掰!建议收藏!


     

   正文   


手把手教你SpringBoot+SpringCloud系列共8篇:
1、(一)手把手教你 SpringBoot + SpringCloud 开发环境搭建2、(二)手把手教你 SpringBoot + SpringCloud —— 使用Eureka实现服务注册与发现!

一、配置高可用的Eureka注册中心集群

之前使用的是独立模式的注册中心,为了防止注册中心崩溃使整个应用系统瘫痪,现采用集群的方式部署高可用的注册中心。

创建两个属性文件,命名为application-server1.properties和application-server2.properties:

#application-server1.properties
spring.application.name=eureka-server-01
server.port=9001
eureka.instance.hostname=server1
eureka.client.serviceUrl.defaultZone=http://server2:9002/eureka/
#application-server2.properties
spring.application.name=eureka-server-02
server.port=9002
eureka.instance.hostname=server2
eureka.client.serviceUrl.defaultZone=http://server1:9001/eureka/

这么做的目的是要开启两个应用,一个端口为9001,一个为9002,相互注册,即是服务端又是客户端。

需要注意的是hostname,需要在hostname文件中,加入映射,window系统在C:\Windows\System32\drivers\etc目录下,linux系统在/etc/hosts:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

127.0.0.1 server1
127.0.0.1 server2

通过maven install打包jar包,然后打开两个cmd,通过命令行执行:

  • java -jar tbp-eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=server1

  • java -jar tbp-eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=server2

随后访问localhost:9001与localhost:9002,可以看到注册中心已经互相注册了:

二、将服务注册到Eureka注册中心集群

修改owl-bookstore-service-user工程的application.yml配置文件,将defaultZone设置为集群连接:

eureka:
client:
serviceUrl:
defaultZone: http://localhost:9001/eureka/,http://localhost:9002/eureka/
server:
port: 7001
spring:
application:
name: SERVICE-USER

重新启动两个user服务,打开两个cmd,输入:

  • java -jar owl-bookstore-service-user-1.0-SNAPSHOT.jar --server.port=7001

  • java -jar owl-bookstore-service-user-1.0-SNAPSHOT.jar --server.port=7002

查看注册中心,确认服务注册成功:

三、将消费端注册到Eureka注册中心集群

同服务的配置文件,这里也将owl-bookstore-web-console工程的application.yml注册到集群:

spring:
application:
name: owl-bookstore-web-console
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9001/eureka/,http://localhost:9002/eureka/
server:
port: 8000

测试如上图。

到此,Eureka Server的集群就已经搭建完毕了。

获取项目源码:在公众号后台回复“STS”即可获得。

欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,学习能力的提升上有新的认识,欢迎转发分享给更多人。

欢迎各位读者加入订阅号程序员小乐技术群,在后台回复“加群”或者“学习”即可。

猜你还想看


阿里、腾讯、百度、华为、京东最新面试题汇集

如何更好的使用Java异常,看这篇就对了!

互联网后端基础设施,看了都说好!

(二)手把手教你 SpringBoot + SpringCloud —— 使用Eureka实现服务注册与发现!

关注订阅号「程序员小乐」,收看更多精彩内容
嘿,你在看吗?
: . Video Mini Program Like ,轻点两下取消赞 Wow ,轻点两下取消在看

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存