您的位置:首页 > 编程语言 > Java开发

【Spring Cloud】Eureka服务注册中心搭建

2017-11-25 00:44 856 查看
简介

Eureka服务器用作服务注册服务器。

Eureka客户端是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。

Eureka特点

servlet 应用 Jersey 框架实现自身的 RESTful HTTP接口

服务的注册通过 HTTP 协议实现 通过 JDK

自带的 Timer 实现定时任务:心跳、定时清理过期服务、节点同步

使用Google的guava包实现内存缓存

Eureka客户端

服务启动时,向注册中心注册服务,同时从一个服务注册服务中查询所有可用服务实例的库,并缓存到本地;

用来简化与服务器的交互、作为轮询负载均衡器,并提供故障切换支持;

内置使用轮询负载均衡算法;默认发送心跳的周期是30s;当一个服务器不可用,需要3个心跳才能让服务器和客户端的元数据相同。

eureka.instance.leaseRenewalIntervalInSeconds

eureka.instance.leaseExpirationDurationInSeconds

Eureka服务端:

服务注册中心;

为服务实例注册管理和查询可用实例提供了REST API;

架构图



配置maven父项目的pom

<groupId>com.xfdmao</groupId>
<artifactId>fcat-springcloud</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>fcat-center</module>
<module>fcat-user</module>
</modules>

<name>fcat-springcloud</name>
<description>fcat-spring cloud project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.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>Edgware.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>


Eureka服务注册中心——服务端

新建项目模块:fcat-center

pom引用jar包

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>


创建注册中心启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class CenterApplication {

public static void main(String[] args) {
SpringApplication.run(CenterApplication.class,args);
}
}


application.yml配置文件中配置

spring:
application:
name: fcat-center

server:
port: 8760

eureka:
client:
register-with-eureka: false # 实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true
fetch-registry: false   # 此客户端是否获取eureka服务器注册表上的注册信息,默认为true
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/


Eureka客户端

新建项目模块:fcat-user

pom引用相应jar包

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>


新建用户模块启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}


新建controller

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

@RequestMapping(value = "",method = RequestMethod.GET)
public String home(){
return "FCat User";
}
}


application.yml 配置文件

spring:
application:
name: fcat-user

server:
port: 8761

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8760/eureka/


启动

main方法一起启动:CenterApplication、UserApplication

访问

服务注册中心: http://localhost:8960

用户模块: http://localhost:8961

源码地址:https://gitee.com/xfdm_admin/spring-cloud/tree/master

更多相关内容请查看:

angular、spring cloud 开源实战项目源码:https://gitee.com/xfdm/FCat

QQ群:549141844

代码持续更新…
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring-cloud