學習springcloud的Eureka。記錄其中碰見的問題(參考純潔的微笑)

1.idea建立Eureka的服務項目html

 

選擇maven project 而後next下一步  java

選擇Eureka服務,建立spring

2.引用對應的pomapache

<?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">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>democloud</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>democloud</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
   </properties>

   <dependencies>
      <!--Eureka Server-->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter</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-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>

這裏須要注意的是,我參考的純潔的微笑,他pom裏面的app

<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka-server</artifactId>
	</dependency>

這個已經不在使用了,如今使用的是maven

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

請注意ide

3.在Application啓動文件裏面增長開啓Eureka服務註解spring-boot

@EnableEurekaServer

4.增長application.properties的配置參數測試

spring.application.name=spring-cloud-eureka

server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

啓動項目,訪問。成功fetch

服務中心,這種重要,若是單節點,掛了,後面很嚴重,如今嘗試雙節點

5.application.properties增長多配置啓動參數

#多配置文件
spring.profiles.active=peer1

建立對應的application-peer1.properties   與 application-peer2properties

spring.application.name=spring-cloud-eureka

server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#多配置文件
spring.profiles.active=peer1

eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
spring.application.name=spring-cloud-eureka
server.port=8001
eureka.instance.hostname=peer2

eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/

這裏,須要配置host轉換

在hosts文件中加入以下配置

127.0.0.1 peer1  
127.0.0.1 peer2

hosts位置:C:\Windows\System32\drivers\etc下

https://jingyan.baidu.com/article/f0e83a258928d122e491017a.html     hosts 相關介紹

使用maven打jar包

java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1

啓動成功

 

下面,我去測試eureka集羣了

相關文章
相關標籤/搜索