近來本地搭建eureka的集羣環境遇到諸多問題,爲了鞏固學的東西,在此記錄下這些解決方案,話很少說,上代碼。html
1.首先是pom.xml文件,主要是 spring-boot-starter-web ,spring-cloud-starter-netflix-eureka-serverjava
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.gongdafang</groupId> <artifactId>serverdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>serverdemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <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-server</artifactId> </dependency> <!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.我使用了yaml文件,由於它結構清晰易懂,配置內容字符量比properties文件少,主要仍是由於能夠一個文件多個環境配置web
#集羣配置 spring: profiles: active: peer3 --- server: port: 8081 spring: profiles: peer1 application: name: z1-eureka eureka: client: serviceUrl: defaultZone: http://peer2:8082/eureka/,http://peer3:8081/eureka/ instance: hostname: peer1 --- server: port: 8082 spring: profiles: peer2 application: name: z2-eureka eureka: client: serviceUrl: defaultZone: http://peer1:8081/eureka/,http://peer3:8083/eureka/ instance: hostname: peer2 --- server: port: 8083 spring: profiles: peer3 application: name: z3-eureka eureka: client: serviceUrl: defaultZone: http://peer1:8081/eureka/,http://peer2:8082/eureka/ instance: hostname: peer3
3.運行main spring
4.上面的三步驟已經將本地集羣設置基本完成了,可是光寫完代碼,運行有問題那也是很頭疼的一件事啦,嘿嘿,如今就總結下我遇到過的問題和解決方案吧apache
1. WARN 2364 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_Z2-EUREKA/windows10.microdone.cn:z2-eureka:8082 - registration failed Cannot execute request on any known server編程
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known serverwindows
2. There was a problem with the instance info replicatorapi
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known serverapp
① 由於本地設置了多個server端(如peer1,peer2),而instance.hostname是惟一標識 ,此時須要配一下本地的host文件。Windows的host在/etc/host,mac的在/private/etceclipse
我是用windows爲例。cmd裏敲notepad "%systemroot%/system32/drivers/etc/hosts" 回車
②在idea中屢次啓動同一個項目,啓動設置修改
③對於idea運行時須要設定指定參數,如圖,當運行不一樣節點時,須要指定對應節點 --spring.profiles.active=peer1
5.以上是我用idea出現的問題,注意若是運行非最後一個server端時,服務會啓動成功可是仍是報錯(以下出錯),是由於他註冊的某個服務還未啓動致使,能夠不用管,等全部服務都註冊成功後,服務會自動註冊成功的,貼上運行成功的頁面吧
ERROR 5796 --- [tbeatExecutor-0] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://peer2:8082/eureka/} com.sun.jersey.api.client.ClientHandlerException: org.apache.http.NoHttpResponseException: peer2:8082 failed to respond
6.除了使用編程工具運行項目外,還可採用控制檯命令的形式
① 切換目錄到你的項目根目錄而後運行 mvn install生產jar包
也能夠用 idea打包
控制檯切換到你的jar 包 位置 java -jar 你的jar包 .jar 針對集羣環境命令 java -jar 你的jar包 .jar --spring.profiles.active=xxx
②第二種方法,能夠直接 切換目錄到你的項目根目錄,而後運行 mvn spring-boot:run
以上是我搭建環境時遇到的問題,百度搭建環境的相關步驟,沒有一個完整的步驟,有丟丟費腦子,哈哈哈。不過仍是有學習收穫的。除了使用idea,使用eclipse的同窗在運行時,是須要配置運行服務端設置的,能夠借鑑文檔https://www.cnblogs.com/syuf/p/9561798.html,寫的仍是挺具體的.