Spring Cloud学习(1)——单节点Eureka Server

阅读的书籍为《Spring Cloud 微服务实战》。

基本概念

服务治理

服务治理可以说是微服务架构中最为核心和基础的模块,主要用来实现各个微服务实例的自动化注册与发现。

早期微服务系统中,服务不多,可以通过静态配置来完成服务的调用。但是随着业务的发展,系统功能越来越复杂,相应的微服务应用也不断增加,静态配置就越来越难以维护,故需要实现微服务实例的自动化注册与发现

服务注册

在服务治理框架中,通常都会构建一个注册中心,每个服务单元都向注册中心登记自己提供的服务,将主机与端口号、版本号、通信协议等一些附加信息告知注册中心,注册中心按服务名分类组织服务清单。
服务注册中心还需要以心跳的方式去监测清单中的服务是否可用,若不可用,从服务清单中剔除,达到排除故障服务的效果。

服务发现

在服务治理框架下运作,服务间的调用不再通过指定具体的实例地址来实现,而是通过向服务名发起请求调用实现。

Eureka服务端

即为服务注册中心。
如果以集群模式部署,当集群中有分片出现故障时,Eureka就会转入自我保护模式。它允许在分片故障期间继续提供服务的发现与注册,当故障分片恢复运行的时候,集群中的其他分片会把它们的状态再次同步回来。
不同可用区域的服务注册中心通过异步模式互相复制各自的状态,这意味着任意给定的时间点,每个实例关于所有服务的状态是有细微差别的

Eureka客户端

主要处理服务的注册与发现,在应用程序运行时,客户端向注册中心注册自身提供的服务并周期性地发送心跳来更新它的服务租约。
同时它也能从服务端查询当前注册的服务信息,并把它们缓存到本地并周期性地刷新服务状态。

搭建服务注册中心

  • 使用IDEA搭建一个基础的Spring Boot工程,在pom.xml中引入Eureka必要的依赖内容。
  • 通过@EnableEurekaServer注解启动一个服务注册中心。如下:
@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • 在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,需要禁用它的客户端注册行为。在application.properties中增加如下配置:
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#关闭保护机制
eureka.server.enable-self-preservation=false

eureka.client.register-with-eureka设置为false代表不向注册中心注册自己。
eureka.client.fetch-registry设置为false,代表不需要去检索服务。
eureka.server.enable-self-preservation=false,代表关闭保护机制。

Eureka会因为15分钟内超过85%的实例丢失心跳而触发保护机制,触发保护机制后,Eureka Server会将当前实例注册信息保护起来,让这些实例不会过期。但在保护期内,可能实例出现了问题,那么客户端就很容易拿到实际已经不存在的实例,出现调用失败的情况。生产环境,需要客户端有容错机制,比如请求重试、断路器等。在此,本地调试时关闭保护机制,确保注册中心可以将不可用的实例正确剔除。

这里写图片描述
其中Instances currently registered with Eureka栏为空,说明该注册中心还没有注册任何服务。

注册服务提供者

  • 使用IDEA重新搭建一个基础的Spring Boot工程,在pom.xml中引入Eureka必要的依赖内容。
  • 主类中加上@EnableDiscoveryClient注解,激活Eureka中的DiscoveryClient实现。
package com.example.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class HelloworldApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
    }
}
  • 在该工程的主类同级目录下创建一个RESTful API实现代码。通过注入DiscoveryClient对象,在日志中打印出服务的相关内容。
package com.example.helloworld.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.apache.log4j.Logger;
import java.util.List;
@RestController
public class HelloworldController {
    private final Logger log = Logger.getLogger(getClass());
    @Autowired
    private DiscoveryClient discoveryClient;
    @RequestMapping(value = "/helloworld", method = RequestMethod.GET)
    public String helloworld() {
        System.out.println("discoveryClient.getServices().size() = " + discoveryClient.getServices().size());
        for (String s : discoveryClient.getServices()) {
            System.out.println("services " + s);
            List<ServiceInstance> serviceInstances = discoveryClient.getInstances(s);
            for (ServiceInstance si : serviceInstances) {
                System.out.println("    services:" + s + ":getHost()=" + si.getHost());
                System.out.println("    services:" + s + ":getPort()=" + si.getPort());
                System.out.println("    services:" + s + ":getServiceId()=" + si.getServiceId());
                System.out.println("    services:" + s + ":getUri()=" + si.getUri());
                System.out.println("    services:" + s + ":getMetadata()=" + si.getMetadata());
            }
        }
        return "hello world";
    }
}
  • 在application.properties中增加如下配置:
spring.application.name=hello-world-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
  • 启动服务

可以在该服务的控制台看到如下输出:
这里写图片描述
可以在服务注册中心的控制台看到如下输出:
这里写图片描述
访问Eureka的信息面板
这里写图片描述

可以在注册服务的控制台查看到它从服务注册中心获取到的服务相关信息。
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring boot+spring cloud视频教学下载全套。( ├1 公开课.avi ├10 4.6 Ribbon-3使用配置文件自定义Ribbon Client.avi ├11 4.7 Ribbon-4 Ribbon脱离Eureka使用.avi ├12 4.8 Feign-1 Feign的简介及基础使用.avi ├13 4.9 Feign-2覆写Feign的默认配置.avi ├14 4.10 Fegion-3覆写Fegion的默认配置及Fegion的日志.avi ├15 4.11 Fegion-4解决Fegion第一次请求timeout的问题.avi ├16 4.12 Eureka深入理解.avi ├17 4.13 Eureka常用配置详解.avi ├18 4.14 Eurek Ribbon Feign常见问题及解决.avi ├19 5.1超时机制,断路器模式简介.avi ├2 1.1 微服务架构概述.avi ├20 5.2 Hystrix简介及简代码示例.avi ├20 5.2Hystrix简介及简代码事例.avi ├21 Hystrix Health Indicator及Metrics Stream.avi ├22 5.4 Hystrix Health Indicator及Metrics Stream支持.avi ├23 5.5 Fegion的Hystrix支持.avi ├24 5.6如何禁用个FegionClient的Hystrix的支持.avi ├25 5.7 Feign使用fallbackFactory属性打印fallback异常.avi ├26 5.8 Hystrix Dashboard的使用与常见问题总结.avi ├27 5.9 Turbine-上.avi ├28 5.9 Turbine-下.avi ├29 6.1 API Gateway简介.avi ├3.开始使用Spring Cloud实战微服务.avi ├30 6.2 Zuul简介及代码示例.avi ├31 6.3 Zuul指定path+serviceid.avi ├32 6.4 Zuul指定Path+url以及指定可用的服务节点时如何负载均衡.avi ├33 6.5 Zuul使用正则表达式指定路由规则.avi ├34 6.6 Zuul路由的strip-prefix与order.avi ├35 6.7 Zuul的各种姿势.avi ├36 6.8通过Zuul上传文件,禁用Zuul的Filters.avi ├37 6.9 Zuul的回退.avi ├38 6.10 使用Sidecar支持异构平台的微服务.avi ├39 6.10 Sidecar补充.avi ├4 服务提供者与服务消费者.avi ├4 服务提供者与服务消费者new.avi ├40 6.11-1 Zuul过滤器.avi ├41 6.11-2禁用Zuul的过滤器.avi ├42 7.1 Spring Cloud Config简介.avi ├43 7.2 编写Config Server.avi ├44 7.3 编写Config Client.avi ├45 7.4 Git仓库配置详解.avi ├46 7.5配置属性加解密之对称加密.avi ├47 7.6配置属性加解密之非对称加密.avi ├48 7.7 Spring Cloud Config与Eureka.avi ├49 7.8 Spring Cloud Config 与Eureka配合使用.avi ├5 4.1服务发现与服务注册.avi ├50 7.9 Spring Cloud Config配置属性刷新之手动刷新.avi ├51 7.10 Spring Cloud Config配置属性刷新之自动刷新.avi ├52 7.11 Spring Cloud Config配置属性刷新之自动刷新补充.avi ├53 7.12 Config Server的高可用.avi ├6 4.2Eureka简介与Eureka Server上.avi ├7 4.3将微服务注册到Eureka Server上.avi ├8 4.4 Ribbon-1 Ribbon的基本使用.avi ├9 4.5 Ribbon-2通过代码自定义配置ribbon.avi )
动力节点(PowerNode)是一个培训机构,其提供了关于 Spring Cloud学习课程。通过参加动力节点Spring Cloud 培训课程,你可以深入学习和实践 Spring Cloud 框架的各个组件和功能。 在动力节点Spring Cloud 培训课程中,你可以学习以下内容: 1. Spring Cloud 的基本概念和架构:学习 Spring Cloud 的核心原理、微服务架构的设计思想,以及 Spring Cloud 的整体架构。 2. 服务注册与发现:学习如何使用 Spring Cloud Netflix Eureka 或 Consul 实现微服务的注册与发现功能,以及如何配置和管理服务的注册中心。 3. 服务间通信:学习如何使用 Spring Cloud Feign 或 Ribbon 实现微服务之间的通信,包括声明式的 REST 调用和负载均衡等功能。 4. 服务网关:学习如何使用 Spring Cloud Gateway 或 Zuul 构建和管理微服务的统一访问入口,实现路由、过滤和认证等功能。 5. 分布式配置管理:学习如何使用 Spring Cloud Config 实现分布式配置管理,通过集中管理配置文件来实现配置的动态更新和管理。 6. 熔断器和容错机制:学习如何使用 Spring Cloud Hystrix 实现微服务的熔断、降级和容错等机制,确保系统的可靠性和稳定性。 7. 消息总线和服务追踪:学习如何使用 Spring Cloud Bus 和 Sleuth 实现消息总线和分布式跟踪功能,方便系统的监控和调试。 通过动力节点的培训课程,你可以系统地学习和实践 Spring Cloud 的各个组件和功能,掌握构建分布式系统的技术和最佳实践。建议你访问动力节点的官方网站或咨询他们的客服了解更多关于 Spring Cloud 培训课程的详细信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值