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

简介
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

代码持续更新…

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FCAT.TOP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值