Eureka
什么是Eureka
Eureka是Netflix微服务套件中的一部分,它是基于REST的服务。主要负责微服务架构中各个服务的自动注册与发现。Eureka包含了两部分:Eureka Server和Eureka Client。
Eureka Server
Eureka Server即服务注册中心,各服务启动后都需要在注册中心进行注册。
Eureka Client
Eureka Client是一个Java应用程序,它在启动的时候主动到注册中心去注册自己的服务,同时它也能从服务端查询当前注册服务信息并把它们缓存到本地并周期性地刷新服务状态。
Eureka Client又可以分为两种角色:Application Server和Application Client。一个是服务的提供者一个是服务的使用者,两者没有绝对的划分,当提供服务时就是Server,当调用其他服务时就是Client。
搭建Eureka服务
单节点
因为Spring Cloud是基于SpringBoot的,所有先创建一个SpringBoot项目,可以直接使用idea创建,或者在Spring Initializr中创建。
pom文件
<!-- SpringBoot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<!-- SpringCloud的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
配置文件
在resource下面创建配置文件,bootstrap.yml或者application.yml。前者在后者之前执行,这里我们用application.yml
# 自己取的应用名称,可以不要
spring:
application:
name: spring-cloud-eureka
# 服务端口
server:
port: 8761
eureka:
client:
# 是否将自己注册到Eureka server中,默认为true
registerWithEureka: false
# 是否从eureka Server中获取服务注册信息,默认为true
fetchRegistry: false
# 服务地址
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/
启动类
@EnableEurekaServer
注解表明这是一个Eureka Server注册中心。启动之后在浏览器访问localhost:8761
,就可以看到注册中心的可视化界面了。
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
双节点命令启动
首先在windows的hosts文件添加dns配置
127.0.0.1 peer1
127.0.0.1 peer2
配置文件
spring:
application:
name: eureka-cluster
spring:
profiles: peer1
server:
port: 8761
eureka:
instance:
hostname: peer1
server:
enable-self-preservation: false
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://peer2:8762/eureka
---
spring:
application:
name: eureka-cluster
spring:
profiles: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2
server:
enable-self-preservation: false
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://peer1:8761/eureka
pom文件
<!-- SpringBoot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<!-- SpringCloud的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动方式
mvn clean package
java -jar xx.jar --spring.profiles.active=peer1
java -jar xx.jar --spring.profiles.active=peer2
双节点idea启动
首先在windows的hosts文件添加dns配置
127.0.0.1 peer1
127.0.0.1 peer2
配置文件
application.yml
spring:
profiles:
active: peer1
application:
name: eureka-cluster
application-peer1.yml
server:
port: 8761
eureka:
instance:
hostname: peer1
server:
enable-self-preservation: false
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://peer2:8762/eureka
application-peer2.yml
server:
port: 8762
eureka:
instance:
hostname: peer2
server:
enable-self-preservation: false
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://peer1:8761/eureka
pom文件
<!-- SpringBoot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<!-- SpringCloud的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动方式
idea启动的地方,编辑启动类,选择可以多次启动,选激活peer1启动,再激活peer2启动。