【SpringCloud】(四):建立Eureka Server例項

00潤物無聲00發表於2017-08-13

  本篇文章我們來實現一個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成功






相關文章