Spring Cloud Eureka 是spring Cloud Netflix微服务套件中的一部分,它基于Netflix kafka做了二次封装,主要负责完成微服务架构中的服务治理功能。服务治理可以说是微服务框架中最为核心和基础的服务,他主要用来实现各个微服务实例的自动化注册和发现。
- 服务注册:服务治理框架中,都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机、端口号、版本号、通信协议等一些附加信息告知注册中心。
- 服务发现:服务治理框架下运作,服务间的调用不再通过制定具体的实例地址来实现,而是通过向服务名发起请求调用实现。所以服务调用方在调用服务提供方接口的时候,并不知道具体的服务实例位置。
Eurek服务端:即服务注册中心,支持高可用配置。依托强一致性提供良好的服务实例可用性,可以应多种不同的故障场景。
Eurek客户端:主要处理服务的注册与发现。客户端服务通过注解和参数配置方式嵌入在客户端应用程序代码中,在应用程序运行时,Eurek客户端向注册中心注册自身提供的服务,并周期性的发送心跳来更新它服务租约。同时也能从服务端查询当前注册的服务信息并把它们缓存到本地并周期性的刷新服务状态。
下面以实例说明,服务框架为
parent pomxml
<modules>
<module>provider-user</module>
<module>consumer-order</module>
<module>eureka-server</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.21.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M9</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
eurek-server pom.xml
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>com.man</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.man</groupId>
<artifactId>eureka-server</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
eurek-server application.yml
server:
port: 9999
eureka:
instance:
hostname: localhost
#服务名称
appname: eureka
client:
service-url:
#注册中心的地址
defaultZone: http://user:123@${eureka.instance.hostname}:9999/eureka/,http://user:123@${eureka.instance.hostname}:9998/eureka/
#是否向注册中心注册自己,默认为true,
#由于只是单节点(之后会分享高可用的Eureka集群的搭建),所以这里选择false
register-with-eureka: true
#是否检索服务,由于注册中心的职责是维护服务示例,默认为true
#并不需要检索服务,所以这里选择false
fetch-registry: false
security:
basic:
enabled: true #开启安全配置,如果不需要设置密码设置为false即可,这个参数必须放在application.yml中,不允许放在bootstrap.yml
user:
name: user
password: 123
eureka-server2 application.yml
server:
port: 9998
eureka:
instance:
hostname: localhost
#服务名称
appname: eureka
client:
service-url:
#注册中心的地址
defaultZone: http://user:123@${eureka.instance.hostname}:9999/eureka/,http://user:123@${eureka.instance.hostname}:9998/eureka/
#是否向注册中心注册自己,默认为true,
#由于只是单节点(之后会分享高可用的Eureka集群的搭建),所以这里选择false
register-with-eureka: true
#是否检索服务,由于注册中心的职责是维护服务示例,默认为true
#并不需要检索服务,所以这里选择false
fetch-registry: false
security:
basic:
enabled: true #开启安全配置,如果不需要设置密码设置为false即可,这个参数必须放在application.yml中,不允许放在bootstrap.yml
user:
name: user
password: 123
启动主类(两个注册中心的启动类类似)
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication
{
public static void main( String[] args )
{
SpringApplication.run(EurekaApplication.class,args);
}
}
启动两个主类
启动成功
添加服务提供者provider-user模块
pom.xml
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>com.man</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.man</groupId>
<artifactId>provider-user</artifactId>
<name>provider-user</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<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-netflix-eureka-client</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
添加:spring-cloud-starter-netflix-eureka-client 和 spring-boot-starter-actuator 依赖
配置application.yml
server:
port: 7900
context-path: /provider-user
spring:
application:
name: provider-user
eureka:
client:
service-url:
#注册中心的地址
defaultZone: http://user:123@localhost:9999/eureka/,http://user:123@localhost:9998/eureka/
security:
user:
name: user
password: 123
启动主类添加@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient //启用eureka 客户端
public class ProviderUserApplication
{
public static void main( String[] args )
{
SpringApplication.run(ProviderUserApplication.class,args);
}
}
启动主类可以将provider-server注册到 Eurek中。
注册成功。