Eureka
服务治理组件,包含服务的注册与发现
一 单服务模式
1.1创建Eureka服务
创建一个springboot工程(jdk1.8)
pom文件,这里使用boot2.0.3与cloud的F版本
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hh</groupId>
<artifactId>eureka-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-service</name>
<description>Demo project for Spring Boot</description>
<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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<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>
boot与cloud的版本对应(cloud版本名都是伦敦地铁站名)
1.2修改配置
在启动类上加注解@EnableEurekaServer表示为eureka服务端
修改application.yml
spring:
application:
name: eureka-server
server:
port: 8080
eureka:
instance:
hostname: localhost
client:
##是否将自己这个服务注册到EurekaServer中 默认True 此服务为Server 所以为False
register-with-eureka: false
#是否从EurekaServer获取注册信息 默认True 单点ES不存在其他的EurkaServer 所以为False
fetch-registry: false
serviceUrl:
#与其他ES交互的地址,单点ES写自己的链接
defaultZone: http://localhost:8080/eureka
1.3运行程序
运行程序访问http://localhost:8080/,页面如下,eureka服务已开启
二 多节点模式
实际开发部署中,服务很有可能或宕机,然后就会导致所有注册的生产项目都无法使用,全面崩盘。所有一般采用多节点分布式部署服务实现高可用,其中一个服务宕机还有其他服务再继续服务,不会多生成消费产生影响。
再创建一个如上一样的eureka服务端项目模拟多节点部署。
修改配置
启动类上添加@EnableDiscoveryClient注解表示为eureka客户端,
eureka-service > application.yml
spring:
application:
name: eureka-server
server:
port: 8080
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8081/eureka
eureka-service2 > application.yml
spring:
application:
name: eureka-server
server:
port: 8081
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka
上面两个配置文件,服务名都是一样的,都是eureka-service,而且defaultZone的值都是对方的链接,意思是彼此注册,如有多个节点就是把自己注册到其他所有服务,链接用逗号隔开即可
启动服务
分别访问 http://localhost:8081/。http://localhost:8080/
两个服务端都已注册在eureka-service服务中