0 课程地址
https://coding.imooc.com/lesson/380.html#mid=28550
1 自我提升
1.1 重点知识
a maven打包相关指令:
# 进行打包 mvn clean package -Dmaven.test.skip=true -U #bootstrap.yml生效的server java -jar coupon-eureka-1.0-SNAPSHOT.jar --spring.profiles.active=server1
1.2 踩过的坑
a 多个eureka server启动报错
报错内容:
com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.ConnectTimeoutException: Connect to server1:8001 timed out at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar!/:1.19.1] at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123) ~[jersey-client-1.19.1.jar!/:1.19.1] at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.9.8.jar!/:1.9.8]
解决方案:
修改的hosts文件位置不对,其路径应该为C:WindowsSystem32driversetc
b 多个eureka server启动报错
报错内容:
2021-01-10 08:18:27.909 ERROR 9312 --- [ main] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://server1:8001/eureka/} com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar!/:1.19.1] at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123) ~[jersey-client-1.19.1.jar!/:1.19.1] at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.9.8.jar!/:1.9.8] at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar!/:1.19.1]
解决方案:
通过对比错误的yml和正确的yml,发现是profiles制表位不对,应该往前提一位(认为yml配置文件不如properties文件的地方,书写格式有问题不容易排查)
spring:
application:
name: coupon-eureka
#这是正确的
profiles: server3
server:
port: 8003
eureka:
instance:
hostname: server3
prefer-ip-address: false
client:
service-url:
defaultZone: http://server1:8001/eureka,http://server2:8002/eureka
2 课程内容
2.1 多server启动配置:
a 注掉application.yml
b 添加bootstrap.yml
spring: application: name: coupon-eureka profiles: server1 server: port: 8001 eureka: instance: hostname: server1 prefer-ip-address: false client: service-url: defaultZone: http://server2:8002/eureka,http://server3:8003/eureka --- spring: application: name: coupon-eureka profiles: server2 server: port: 8002 eureka: instance: hostname: server2 prefer-ip-address: false client: service-url: defaultZone: http://server1:8001/eureka,http://server3:8003/eureka --- spring: application: name: coupon-eureka profiles: server3 server: port: 8003 eureka: instance: hostname: server3 prefer-ip-address: false client: service-url: defaultZone: http://server1:8001/eureka,http://server2:8002/eureka
c 配置hosts文件
windows系统,打开路径C:WindowsSystem32driversetc,修改配置文件添加:
127.0.0.1 server1 127.0.0.1 server2 127.0.0.1 server3
修改后,保存
d 执行maven命令打包
打开idea terminal窗口,切到跟路径:F:kewaiSpringCloudlaoqinimooc-coupon>
打包:
mvn clean package -Dmaven.test.skip=true -U
打包后在coupon-eureka模块下能看到相关jar包
e 分别(选择性的使用配置文件bootstrap.yml部分属性)运行jar包
前两个窗口报错正常(因为还没启动第三个server),第三个server不应该报错
e.1 第一个ideaterminal窗口,切到coupon-eureka的target路径下(jar包路径)
java -jar coupon-eureka-1.0-SNAPSHOT.jar --spring.profiles.active=server1
e.2 打开第二个ideaterminal窗口,切到coupon-eureka的target路径下(jar包路径)
java -jar coupon-eureka-1.0-SNAPSHOT.jar --spring.profiles.active=server2
e.3 打开第三个ideaterminal窗口,切到coupon-eureka的target路径下(jar包路径)
java -jar coupon-eureka-1.0-SNAPSHOT.jar --spring.profiles.active=server3
f 上边步骤完成后,eureka 已经起来了,浏览器分别输入:http://server1:8001, http://server2:8002, http://server3:8003访问即可
2.2 Eureka-Server提供浏览器界面的说明:
System Status:系统状态信息,包含启动时间,当前时间,数据中心,环境
Ds Replicas:父本,说明该服务器从哪里同步数据,如上图,从server2和server3同步数据
Instances currently registered with Eureka:当前实例注册到eureka的信息(如上图,有三个server)
General Info:通用信息,关于eureka的一些信息采集信息
instance info:当前实例信息
3 其他内容
3.1 mvn涉及指令:
mvn clean package #打包命令
-Dmaven.test.skip=true #不执行测试用例,也不编译测试用例类
-U #强制刷新本地仓库不存在release版和所有的snapshots版本 java -jar coupon-eureka-1.0-SNAPSHOT.jar #运行jar包
--spring.profiles.active=server1 #在执行java -jar
命令时可以通过--spring.profiles.active=test
的方式进行激活指定的profiles
列表。
4 引用博客
https://www.cnblogs.com/ceshi2016/p/6489477.html