本文主要是介绍【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: falsefetch-registry: falseservice-url:defaultZone: http://localhost:8761/eureka #把Eureka注册到那个Eureka上面
启动Eureka成功
这篇关于【SpringCloud】(四):创建Eureka Server实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!