1、什么是 Eureka
Eureka,这里是 Spring Cloud Eureka 的简称,是 Spring Cloud Netflix 组件之一。Spring Cloud Netflix 中核心的组件包括了服务治理(Eureka),服务容断(Hystrix),路由(Zuul)和客户端负载均衡(Ribbon)。
Spring Cloud Eureka,是基于 Netflix Eureka (Netflix 是 Java 实现的开源软件)。服务治理(Eureka)包括服务注册、服务发现和服务检测监控等。
Eureka,这里是 Spring Cloud Eureka 的简称,是 Spring Cloud Netflix 组件之一。Spring Cloud Netflix 中核心的组件包括了服务治理(Eureka),服务容断(Hystrix),路由(Zuul)和客户端负载均衡(Ribbon)。
Spring Cloud Eureka,是基于 Netflix Eureka (Netflix 是 Java 实现的开源软件)。服务治理(Eureka)包括服务注册、服务发现和服务检测监控等。
2、Eureka微服务工程结构:
├── pom.xml --
└── src
└── main
├── java
│ └── org
│ └── spring
│ └── springcloud
│ ├── CenterServiceApplication.java --服务启动类
└── resources
└── application.yml --配置文件(跟properties一样的作用,只是结构性的,更利于理解)
3、
<?
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.example
</
groupId
>
<
artifactId
>center-service
</
artifactId
>
<
version
>0.0.1-SNAPSHOT
</
version
>
<
packaging
>war
</
packaging
>
<
name
>center-service
</
name
>
<
description
>..
服务注册中心
</
description
>
<!-- Spring Boot 启动父依赖 -->
<
parent
>
<
groupId
>org.springframework.boot
</
groupId
>
<
artifactId
>spring-boot-starter-parent
</
artifactId
>
<
version
>1.5.10.RELEASE
</
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
>
<
spring-cloud.version
>Dalston.SR5
</
spring-cloud.version
>
</
properties
>
<
dependencies
>
<!-- Spring Cloud Netflix Eureka Server 依赖 -->
<
dependency
>
<
groupId
>org.springframework.cloud
</
groupId
>
<
artifactId
>spring-cloud-starter-eureka-server
</
artifactId
>
</
dependency
>
<!-- Spring Boot Test 依赖 -->
<
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
>
3、application.yml:
server: port: 8888 # 服务端口 eureka: instance: hostname: localhost # 设置主机名 client: registerWithEureka: false # 是否向 Eureka 注册服务。该应用为服务注册中心,不需要自注册,设置为 false fetchRegistry: false # 是否检索服务。该应用为服务注册中心,职责为注册和发现服务,无需检索服务,设置为 false
serviceUrl: defaultZone: http://localhost:${server.port}/eureka/ #注册中心的地址 server: waitTimeInMsWhenSyncEmpty: 0 # 设置同步为空时的等待时间。默认 5 * MINUTES
4、
CenterServiceApplication启动类
@SpringBootApplication -----SpringBoot的应用标志
@EnableEurekaServer -----此注解标注了此服务为EurekaServer,并会自动化读取相关配置组件。
public class CenterServiceApplication
extends SpringBootServletInitializer {
public static void main(String[] args) {
//启动内嵌的应用服务器(
Tomcat或jetty),并初始化Spring环境及Spring相关组建 SpringApplication.
run(CenterServiceApplication.
class, args); }
//
Spring Boot也支持将应用部署至已有的Tomcat容器, 或JBoss, WebLogic等传统Java EE应用服务器
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
{
return application.sources(CenterServiceApplication.class);
} }