前言:本文個人整理筆記記錄,難免有錯誤,歡迎大家留言指正
Eureka注冊中心在叢集中的位置是處于核心位置,所有的微服務通過Eureka進行管理發現。Eureka注冊中心中儲存有各個微服務的服務名稱以及服務連接配接位址。便于微服務的叢集操作。 建立所有微服務的父項目springCloud,指令如下:
mvn archetype:generate -DgroupId=com.ultrapower -DartifactId=springCloud -Dversion=0.0.1-SNAPSHOT -DarchetypeArtifactId=maven-archetype-site-simple -DarchetypeGroupId=org.apache.maven.archetypes -DinteractiveMode=false -DarchetypeCatalog=local |
生成的父項目的目錄結構中,删除掉所有的檔案夾,隻保留pom.xml,在pom中添加如下配置:
<properties> <jdk.version>1.8</jdk.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR4</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> |
1.Eureka注冊中心服務端配置 步驟1:在父項目下建立【springCloud-Eureka】
mvn archetype:generate -DgroupId=com.ultrapower -DartifactId=springCloud-Eureka -Dversion=1.0.0-SNAPSHOT -Dpackage=com.ultrapower -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeGroupId=org.apache.maven.archetypes -DinteractiveMode=false -DarchetypeCatalog=local |
步驟2:修改【springCloud-Eureka】項目的pom.xml添加如下依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> |
步驟3:修改【springCloud-Eureka】項目的application.yml檔案
server: port: 8000 eureka: client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://127.0.0.1:${server.port}/eureka/ |
步驟4:修改【springCloud-Eureka】項目的啟動類
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } } |
2.Eureka注冊中心用戶端配置 步驟1:建立微服務【springCloud-UltraService-8800】建立指令參考Eureka服務端指令,後面不再說明。修改pom.xml檔案,添加如下依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> |
步驟2:修改【springCloud-UltraService-8800】的application.yml檔案,添加如下内容:
spring: application: name: springCloud-UltraService server: port: 8800 eureka: client: serviceUrl: defaultZone: http://127.0.0.1:8000/eureka/ |
步驟3:修改【springCloud-UltraService-8800】的啟動類
@SpringBootApplication @EnableEurekaClient public class UtralServiceApplication { public static void main(String[] args) { SpringApplication.run(UtralServiceApplication.class, args); } } |
步驟4:啟動Eureka服務端和用戶端,進行測試。通路http://localhost:8000/erueka,看用戶端是否注冊到注冊中心中去了。 3.Eureka的叢集部署 首先說明一下,Eureka的叢集注冊有兩種方式,例如三個節點A,B,C 方式一:A注冊B,B注冊C,C注冊A 方式二:A注冊BC,B注冊AC,C注冊AB 我們采用方式二進行注冊 步驟1:複制之前建立好的【springCloud-Eureka】兩份分别起名為【springCloud-Eureka-8001】,【springCloud-Eureka-8002】,【springCloud-Eureka-8003】分别修改三個項目中的application.yml配置檔案中的對應啟動端口和注冊位址, 注意,應用名稱不需要修改
server: port: 8001 eureka: client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://127.0.0.1:8002/eureka/,http://127.0.0.1:8003/eureka/ |
步驟2:用戶端隻需要修改application.yml
eureka: client: serviceUrl: defaultZone: http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/,http://127.0.0.1:8003/eureka/ |
步驟3:啟動驗證通路三個Eureka位址,都可以看到其他的兩個Eureka服務以及用戶端服務: http://127.0.0.1:8001 http://127.0.0.1:8002 http://127.0.0.1:8003 4.Eureka的安全認證 在spirngCloud架構中,無論是注冊中心,配置中心,路由還是到對應的微服務,都需要設定安全認證機制,詳細的設定步驟參照“ 系列2-springCloud微服務-Security安全認證子產品”,