【SpringCloud】(四):建立Eureka Server例項
本篇文章我們來實現一個Eureka Server例項,在實現之前,首先將程式碼重構。將專案結構修改為maven的聚合工程。
一.修改專案結構
服務提供者:microservice-provider-user和服務消費者:microservice-comsumer-movie。
這兩個工程,在第一篇部落格的技術上主要修改了 POM.xml中的依賴。
1.修改parent依賴,為建立的parent的工程
2.專案依賴的版本繼承parent
3.刪除自身依賴的<groupId>和<version>,繼承自parent.
4.刪除了單元測試的依賴和單元測試的類檔案。
服務提供者 microservice-discovery-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">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-provider-user</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.dynamic.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
服務消費者 microservice-comsumer-movie的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">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-comsumer-movie</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.dynamic.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
建立parent工程:microservice-parent
主要是聚合子工程和統一依賴的版本。
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dynamic.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>microservice-provider-user</module>
<module>microservice-comsumer-movie</module>
<module>microservice-discovery-eureka</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- 依賴版本的繼承,2中方式 -->
<!-- 方案:1: 父類中,每個依賴都加入相應的版本。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</dependencyManagement> -->
<!-- 方式2:加入SpringCloud的依賴 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR1</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>
第二種方案:解決子工程依賴的版本問題:http://projects.spring.io/spring-cloud/
二.建立Eureka Server
建立Eureka工程: microservice-discovery-eureka
該工程建立Eureka Server例項。
POM.xml
<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>
<artifactId>microservice-discovery-eureka</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.dynamic.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
加入的依賴為:spring-cloud-starter-eureka-server
啟動類 EurekaApplication
package com.dynamic.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer// 宣告一個server:@EnableEurekaServer
public class EurekaApplication
{
public static void main( String[] args )
{
SpringApplication.run(EurekaApplication.class, args);
}
}
配置檔案:application.yml
server:
port: 8761 #Eurake預設埠
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka #把Eureka註冊到那個Eureka上面
啟動Eureka成功
相關文章
- 【SpringCloud】(五):服務註冊到Eureka ServerSpringGCCloudServer
- JS 建立例項物件的四種模式JS物件模式
- 如何建立一個新的SQL Server例項SQLServer
- SpringCloud之EurekaSpringGCCloud
- SpringCloud (一) EurekaSpringGCCloud
- SQL Server 2000 如何建立一個新例項 ?SQLServer
- 如何在jboss啟動時就建立該server的例項Server
- SpringCloud(一)——Eureka架構SpringGCCloud架構
- springcloud使用eureka叢集SpringGCCloud
- 【SpringCloud】(十):高可用 EurekaSpringGCCloud
- Eureka 原始碼分析之 Eureka Server原始碼Server
- SpringCloud——Feign例項及原理SpringGCCloud
- Jersey—建立restfulwebservice—例項RESTWeb
- Oracle JOB 建立例項Oracle
- springcloud 微服務 之 Eureka 配置SpringGCCloud微服務
- 微服務之springcloud eureka(一)微服務SpringGCCloud
- springcloud(二):註冊中心EurekaSpringGCCloud
- SpringCloud-Eureka叢集搭建SpringGCCloud
- springcloud學習(一)之EurekaSpringGCCloud
- SpringCloud如何配置Eureka授權SpringGCCloud
- SpringCloud(1)-Eureka相關配置SpringGCCloud
- Oracle 10g R2建立ASM例項Step By Step(四)Oracle 10gASM
- java反射方式建立例項Java反射
- js建立字典物件例項JS物件
- 手動建立Oracle例項Oracle
- ASM之建立ASM例項ASM
- 【SpringCloud】微服務Eureka高可用配置SpringGCCloud微服務
- (8)SpringCloud-Spring Cloud EurekaSpringGCCloud
- SpringCloud Eureka自我保護機制SpringGCCloud
- SpringCloud學習筆記(1)——EurekaSpringGCCloud筆記
- 資料庫例項 (SQL Server)資料庫SQLServer
- [AlwaysOn] 建立SQL Server高可用性組T-SQL語法:例項SQLServer
- 註冊中心 Eureka 原始碼解析 —— 應用例項註冊發現 (四)之自我保護機制原始碼
- 2.4.6 Step 5: (Windows)建立例項Windows
- Laravel 2.2 建立 Application 例項LaravelAPP
- OpenStack建立Win10例項Win10
- C#利用反射建立例項C#反射
- SpringCloud基礎教程(三)-Eureka進階SpringGCCloud