SpringCloud从入门到进阶(二)——注册中心Eureka
2018-10-24 10:18
1196 查看
内容
SpringBoot整合SpringCloud的Eureka、Zuul等组件,快速实现简单易懂且具有服务熔断、负载均衡的分布式架构1.0,体验微服务的魅力。
版本
IDE:IDEA 2017.2.2 x64
JDK:1.8.0_171
manve:3.3.3
SpringBoot:1.5.9.RELEASE
SpringCloud:Dalston.SR1
适合人群
Java开发人员
说明
转载请说明出处:SpringCloud从入门到进阶(二)——注册中心Eureka
参考
步骤
项目路径
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud</artifactId> <groupId>com.leo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-server-7001-7002</artifactId> <dependencies> <!--eureka server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <!-- Spring Boot的Maven插件,使用Maven插件的方式来启动Spring Boot工程 如果不添加该插件在使用mvn命令打包的jar有问题,执行时会报错:xxx.jar中没有主清单属性--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
yaml
spring: profiles: peer1 application: name: application-eurekaserver server: port: 7001 eureka: instance: hostname: eureka7001.com instance-id: springcloud-eurekaserver-7001 # prefer-ip-address: false client: #不将eureka server 注册进来,会提示unavailable-replicas #默认情况下,Eureka Server会向自己注册,这时需要配置eureka.client.registerWithEureka 和 eureka.client.fetchRegistry为false,防止自己注册自己。 register-with-eureka: true fetch-registry: true service-url: #defaultZone中填写的URL必须包括后缀/eureka,否则各eureka server之间不能通信 #defaultZone为默认的Zone,来源于AWS的概念。区域(Region)和可用区(Availability Zone,AZ)是AWS的另外两个概念。区域是指服务器所在的区域,
#比如北美洲、南美洲、欧洲和亚洲等,每个区域一般由多个可用区组成。 在本案例中defaultZone是指Eureka Server的注册地址。 defaultZone: http://eureka7002.com:7002/eureka ,http://eureka7003.com:7003/eureka #eureka集群的时候使用 #http://${eureka.instance.hostname}:${server.port}/eureka/ #eureka单机的时候使用。 # server: # enable-self-preservation: false #actuator management: port: 7101 #配置了Actuator对外暴露REST API接口的端口号,如果不指定,端口为应用程序的启动端口,这样做的目的是将程序端口和程序的监控端口分开。 security: enabled: false #Actuator 采取非安全验证方式,l.5x 版本默认开启了 Actuator 的安全验证。 --- spring: profiles: peer2 application: name: application-eurekaserver server: port: 7002 eureka: instance: hostname: eureka7002.com instance-id: springcloud-eurekaserver-7002 client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka ,http://eureka7003.com:7003/eureka #eureka集群的时候使用 #actuator management: port: 7102 #配置了Actuator对外暴露REST API接口的端口号,如果不指定,端口为应用程序的启动端口,这样做的目的是将程序端口和程序的监控端口分开。 security: enabled: false #Actuator 采取非安全验证方式,l.5x 版本默认开启了 Actuator 的安全验证。 --- spring: profiles: peer3 application: name: application-eurekaserver server: port: 7003 eureka: instance: hostname: eureka7003.com instance-id: springcloud-eurekaserver-7003 client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #eureka集群的时候使用 #actuator management: port: 7103 #配置了Actuator对外暴露REST API接口的端口号,如果不指定,端口为应用程序的启动端口,这样做的目的是将程序端口和程序的监控端口分开。 config: enabled: false #Actuator 采取非安全验证方式,l.5x 版本默认开启了 Actuator 的安全验证。
SpringBootApplication
package com.leo; @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication{ public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class,args); } }项目打包
在命令行工具中进入项目pom文件所在路径,然后执行mvn clean package打包。
F:\SpringCloud>cd eureka-server-7001-7002 #进入项目根目录 F:\SpringCloud\eureka-server-7001-7002>mvn clean package #执行maven打包语句 ... [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 29.597 s [INFO] Finished at: 2018-10-23T09:14:11+08:00 [INFO] Final Memory: 35M/293M ...拷贝
使用Bitvise SFTP或WinSCP等工具将jar包上传到有公网的服务器
然后使用scp命令将jar包拷贝到注册发现服务器普通用户的~/jars路径下。
[ServerA@iz8vbgliys1ni7szfx6t2bz jars]$ scp eureka-server-7001-7002-1.0-SNAPSHOT.jar jmsA8@172.26.125.118:/home/jmsA8/jars ... eureka-server-7001-7002-1.0-SNAPSHOT.jar 100% 38MB 38.4MB/s 00:00修改hosts文件
将内部域名eureka7001.com、eureka7002.com、eureka7003.com绑定到局域网IP 172.26.125.118。
修改/etc/hosts,添加hostname对应的ip地址
#IP 域名 别名 [jmsA8@iz8vb62snc6e5cage5yvzbz ~]$ sudo vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com
重启网卡
[jmsA8@iz8vb62snc6e5cage5yvzbz ~]$ sudo /etc/rc.d/init.d/network restart Restarting network (via systemctl): [ OK ]
进行ping测试
[jmsA8@iz8vb62snc6e5cage5yvzbz ~]$ ping eureka7001.com PING eureka7001.com (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.016 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.024 ms [jmsA8@iz8vb62snc6e5cage5yvzbz ~]$ ping eureka7002.com PING eureka7002.com (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.012 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.024 ms [jmsA8@iz8vb62snc6e5cage5yvzbz ~]$ ping eureka7003.com PING eureka7003.com (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.012 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.024 ms启动与测试
[ServerA@iz8vbgliys1ni7szfx6t2bz jars]$java -jar eureka-server-7001-7002-1.0-SNAPSHOT.jar --spring.profiles.active=peer1 & [ServerA@iz8vbgliys1ni7szfx6t2bz jars]$java -jar eureka-server-7001-7002-1.0-SNAPSHOT.jar --spring.profiles.active=peer2 & [ServerA@iz8vbgliys1ni7szfx6t2bz jars]$java -jar eureka-server-7001-7002-1.0-SNAPSHOT.jar --spring.profiles.active=peer3 &
确保本地能访问到eureka服务器,访问http://eureka7001:7001/
关闭
编写启动脚本批量运行jar包,关闭java程序请参考:Linux入门实践笔记(二)--Jar包运行与关闭
相关文章推荐
- spring cloud快速入门教程(二)服务注册中心Eureka
- Spring Cloud Eureka 入门 (一)服务注册中心详解
- Spring Cloud Eureka 入门 (一)服务注册中心详解 「Spring Cloud Eureka 入门系列」 Spring Cloud Eureka 入门 (一)服务注册中心详解 Spr
- Spring Cloud Eureka 入门 (一)服务注册中心详解
- 【Spring Cloud】Eureka服务注册中心搭建
- springcloud实战之3 高可用服务注册中心(eureka集群)
- SpringCloud中的服务注册中心Eureka
- 【Spring Cloud】Eureka服务注册中心搭建
- 【微服务架构】SpringCloud之Eureka(注册中心集群篇)(三)
- Spring cloud系列四 Eureka 之概述和服务注册中心集群
- spring-cloud-eureka-server 注册中心启动不显示页面而是xml问题
- spring-cloud 注册中心eureka环境搭建
- Spring Cloud 入门教程 - Eureka服务注册与发现
- 四、Spring Cloud - Netflix(Eureka注册中心集群构建代码层面)
- 微服务基础设施之服务注册中心: Spring Cloud Eureka
- Spring Cloud 详解(第一篇:Eureka注册中心)
- 【微服务架构】SpringCloud之Eureka(注册中心集群篇)(三)
- 【Spring Cloud】Eureka服务注册中心搭建
- springcloud(二):注册中心Eureka
- 【微服务架构】SpringCloud之Eureka(注册中心集群篇)(三)