Java之 Spring Cloud 微服务 Eureka (第一个阶段)【二】【SpringBoot项目实现商品服务器端是调用】(1)

配置订阅:服务提供者和服务调用者订阅微服务相关的配置

配置下发:主动将配置推送给服务提供者和服务调用者

  1. 服务健康检测

检测服务提供者的健康情况

(2) 常见的注册中心

Zookeeper

zookeeper它是一个分布式服务框架,是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等。简单来说zookeeper=文件系统+监听通知机制。

Eureka

Eureka是在Java语言上,基于Restful Api开发的服务注册与发现组件,Springcloud Netflix中的重要组件

Consul

Consul是由HashiCorp基于Go语言开发的支持多数据中心分布式高可用的服务发布和注册服务软件,采用Raft算法保证服务的一致性,且支持健康检查。

Nacos

Nacos是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。

简单来说 Nacos 就是注册中心 + 配置中心的组合,提供简单易用的特性集,帮助我们解决微服务开发必会涉及到的服务注册与发现,服务配置,服务管理等问题。

Nacos 还是 Spring Cloud Alibaba 组件之一,负责服务注册与发现。

最后我们通过一张表格大致了解Eureka、Consul、Zookeeper的异同点。选择什么类型的服务注册与发现组件可以根据自身项目要求决定。

在这里插入图片描述

2、Eureka的概述


(1) Eureka的基础知识

Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能。

在这里插入图片描述

上图简要描述了Eureka的基本架构,由3个角色组成:

1、Eureka Server

  • 提供服务注册和发现

2、Service Provider

  • 服务提供方

  • 将自身服务注册到Eureka,从而使服务消费方能够找到

3、Service Consumer

  • 服务消费方

  • 从Eureka获取注册服务列表,从而能够消费服务

(2) Eureka的交互流程与原理

在这里插入图片描述

图是来自Eureka官方的架构图,大致描述了Eureka集群的工作过程。图中包含的组件非常多,可能比较难以理解,我们用通俗易懂的语言解释一下:

  • Application Service 相当于本书中的服务提供者,Application Client相当于服务消费者;

  • Make Remote Call,可以简单理解为调用RESTful API;

  • us-east-1c、us-east-1d等都是zone,它们都属于us-east-1这个region;

由图可知,Eureka包含两个组件:Eureka Server 和 Eureka Client,它们的作用如下:

  • Eureka Client是一个Java客户端,用于简化与Eureka Server的交互;

  • Eureka Server提供服务发现的能力,各个微服务启动时,会通过Eureka Client向Eureka Server进行注册自己的信息(例如网络信息),Eureka Server会存储该服务的信息;

  • 微服务启动后,会周期性地向Eureka Server发送心跳(默认周期为30秒)以续约自己的信息。如果Eureka Server在一定时间内没有接收到某个微服务节点的心跳,Eureka Server将会注销该微服务节点(默认90秒);

  • 每个Eureka Server同时也是Eureka Client,多个Eureka Server之间通过复制的方式完成服务注册表的同步;

  • Eureka Client会缓存Eureka Server中的信息。即使所有的Eureka Server节点都宕掉,服务消费者依然可以使用缓存中的信息找到服务提供者。

综上,Eureka通过心跳检测、健康检查和客户端缓存等机制,提高了系统的灵活性、可伸缩性和可用性。

3、搭建Eureka注册中心


(1)搭建Eureka服务中心

1)创建子模块
01)工程搭建

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

02)添加依赖

在这里插入图片描述

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

03)编写配置文件

在这里插入图片描述

server:

port: 9000 #端口

配置eureka server

eureke:

instance:

hostname: localhost

client:

register-with-eureka: false # 是否将自己注册到注册中心

fetch-registry: false # 是否从eureka中获取注册信息

配置暴露给Eureka Client 的 请求地址、

service-url:

defaultZone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/

04)创建对应的启动类

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

package cn.itbluebox.eureke;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication

//激活eurekaserver

@EnableEurekaServer

public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class,args);

}

}

05)运行测试

在这里插入图片描述

在这里插入图片描述

访问:http://localhost:9000/

在这里插入图片描述

(2)将服务提供者注册到eurekaServer上

1)引入EurekaClient的坐标

修改product_service的pom.xml

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

2)修改application.yml添加EurekaServer的信息

在这里插入图片描述

server:

port: 9001 #端口

spring:

application:

name: service-product #服务名称

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8

username: root

password: root

jpa:

database: MySQL

show-sql: true

open-in-view: true

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

3)修改启动类,添加服务发现的支持(可选)

在这里插入图片描述

package cn.itbluebox.product;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.domain.EntityScan;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication

@EntityScan(“cn.itbluebox.product.entity”)

//激活Eureka

//@EnableEurekaClient

@EnableDiscoveryClient //写不写都一样

public class ProductApplication {

public static void main(String[] args) {

SpringApplication.run(ProductApplication.class,args);

}

}

4)运行测试,启动商品微服务

在这里插入图片描述

在这里插入图片描述

访问测试:http://localhost:9000/

在这里插入图片描述

多了

在这里插入图片描述

(3)服务消费者通过注册中心获取服务列表,并调用

Eureka中的元数据:服务的主机名,ip,等信息.可以通过eurekaserver进行获取,用于服务之间的调用,

1)引入EurekaClient的坐标

在这里插入图片描述

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

2)修改application.yml添加EurekaServer的信息

在这里插入图片描述

server:

port: 9002 #端口

spring:

application:

name: service-order #服务名称

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8

username: root

password: root

jpa:

database: MySQL

show-sql: true

open-in-view: true

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

3)完善order_service当中的 OrderController

在这里插入图片描述

package cn.itbluebox.order.controller;

import cn.itbluebox.order.entity.Product;

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.*;

import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController

@RequestMapping(“/order”)

public class OrderController {

//注入RestTemplate对象

@Autowired

private RestTemplate restTemplate;

/*

注入DiscoveryClient:

SpringCloud 提供的获取原数组的工具类

调用方法获取服务的元数据信息

*/

@Autowired

private DiscoveryClient discoveryClient;

/*

参数:商品ID

通过订单系统,调用商品服务

1、需要配置商品对象

2、需要调用商品服务

3、entity.Product

使用java 当中的urlconnection,httpclient,okhttp

*/

@RequestMapping(value = “/buy/{id}”, method = RequestMethod.GET)

public Product findById(@PathVariable Long id) {

//调用discoveryClient方法

//已调用服务名称获取所有的元数据

List instances = discoveryClient.getInstances(“service-product”);

//获取唯一的一个元数据

ServiceInstance instance = instances.get(0);

//根据元数据中的主机地址和端口号拼接请求微服务的URL

Product product = null;

//如何调用商品服务?

product = restTemplate.getForObject(“http://”+instance.getHost()+“:”+instance.getPort()+“/product/”+id,Product.class);

return product;

}

@RequestMapping(method = RequestMethod.POST)

public String save(@RequestBody Product product) {

//调用discoveryClient方法

//已调用服务名称获取所有的元数据

List instances = discoveryClient.getInstances(“service-product”);

//获取唯一的一个元数据

ServiceInstance instance = instances.get(0);

restTemplate.postForObject(“http://”+instance.getHost()+“:”+instance.getPort()+“/product/”,product,Product.class);

return “保存成功”;

}

@RequestMapping(method = RequestMethod.PUT)

public String update(@RequestBody Product product) {

//调用discoveryClient方法

//已调用服务名称获取所有的元数据

List instances = discoveryClient.getInstances(“service-product”);

//获取唯一的一个元数据

ServiceInstance instance = instances.get(0);

restTemplate.put(“http://”+instance.getHost()+“:”+instance.getPort()+“/product/”,product);

return “更新成功”;

}

@RequestMapping(value = “/{id}”, method = RequestMethod.DELETE)

public String delete(@PathVariable Long id) {

//调用discoveryClient方法

//已调用服务名称获取所有的元数据

List instances = discoveryClient.getInstances(“service-product”);

//获取唯一的一个元数据

ServiceInstance instance = instances.get(0);

restTemplate.delete(“http://”+instance.getHost()+“:”+instance.getPort()+“/product/”+id);

return “删除成功”;

}

}

4)运行测试

在这里插入图片描述

访问http://localhost:9002/order/buy/1

在这里插入图片描述

二、服务注册Eureka高级

=========================================================================

1、Eureka Server 高可用集群


在上一个章节,实现了单节点的Eureka Server的服务注册与服务发现功能。

Eureka Client会定时连接Eureka Server,获取注册表中的信息并缓存到本地。

微服务在消费远程API时总是使用本地缓存中的数据。

因此一般来说,即使Eureka Server发生宕机,也不会影响到服务之间的调用。但如果Eureka Server宕机时,某些微服务也出现了不可用的情况,Eureka Server中的缓存若不被刷新,就可能会影响到微服务的调用,甚至影响到整个应用系统的高可用。

因此,在生成环境中,通常会部署一个高可用的Eureka Server集群。

Eureka Server可以通过运行多个实例并相互注册的方式实现高可用部署,Eureka Server实例会彼此增量地同步信息,从而确保所有节点数据一致。

事实上,节点之间相互注册是Eureka Server的默认行为。

在这里插入图片描述

(1)搭建 Eureka Server高可用集群

1 准备2个EurekaServer,需要相互注册

1号server : 9000

2号server : 8000I

2需要将微服务注册到两个EurekaServer上

1)修改eureka_server当中的application.yml

在这里插入图片描述

#模拟两个EurekaServer

#端口9000 , 8000

#两个server需要相互注册

spring:

application:

name: eureka-server

server:

port: 9000 #端口

#配置eureka server

eureka:

client:

#register-with-eureka: false #是否将自己注册到注册中心

#fetch-registry: false #是否从eureka中获取注册信息

service-url: #配置暴露给Eureka Client的请求地址

defaultZone: http://127.0.0.1:8000/eureka/

运行测试

在这里插入图片描述

打开Run Dashboard

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 修改application.yml

在这里插入图片描述

#模拟两个EurekaServer

#端口9000 , 8000

#两个server需要相互注册

spring:

application:

name: eureka-server

server:

port: 8000 #端口

#配置eureka server

eureka:

client:

#register-with-eureka: false #是否将自己注册到注册中心

#fetch-registry: false #是否从eureka中获取注册信息

service-url: #配置暴露给Eureka Client的请求地址

defaultZone: http://127.0.0.1:9000/eureka/

启动多个eureka_server

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

实现eureka的相互注册

在这里插入图片描述

访问地址:

http://localhost:9000/

在这里插入图片描述

http://localhost:8000/

在这里插入图片描述

2)启动product_service

将product_service注册到http://localhost:8000/eureka/

修改product_service当中的application.yml

在这里插入图片描述

server:

port: 9001 #端口

spring:

application:

name: service-product #服务名称

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8

username: root

password: root

jpa:

database: MySQL

show-sql: true

open-in-view: true

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

  • 启动product_service

在这里插入图片描述

http://localhost:9000/上查看

在这里插入图片描述

http://localhost:8000/

在这里插入图片描述

3)启动order_service

将order_service注册到http://localhost:8000/eureka/

修改order_service当中的application.yml

在这里插入图片描述

server:

port: 9002 #端口

spring:

application:

name: service-order #服务名称

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8

username: root

password: root

jpa:

database: MySQL

show-sql: true

open-in-view: true

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

在这里插入图片描述

访问:http://localhost:9000/

在这里插入图片描述

访问:http://localhost:8000/

在这里插入图片描述

4)测试

断开一台eurekaService

在这里插入图片描述

8000断开

在这里插入图片描述

测试数据是否可以正确获取:http://localhost:9002/order/buy/1

在这里插入图片描述

2、显示护IP


修改eureka_server当中的application.yml,修改回之前的配置方便后期测试

在这里插入图片描述

#模拟两个EurekaServer

#端口9000 , 8000

#两个server需要相互注册

spring:

application:

name: eureka-server

server:

port: 9000 #端口

#配置eureka server

eureka:

client:

register-with-eureka: false #是否将自己注册到注册中心

fetch-registry: false #是否从eureka中获取注册信息

service-url: #配置暴露给Eureka Client的请求地址

defaultZone: http://127.0.0.1:8000/eureka/

1)使product_service暴露对应的ip

修改product_service当中的application.yml

在这里插入图片描述

server:

port: 9001 #端口

spring:

application:

name: service-product #服务名称

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8

username: root

password: root

jpa:

database: MySQL

show-sql: true

open-in-view: true

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ipaddress:{server.port} #向组成中心注册服务ID

重新运行并测试

在这里插入图片描述

访问:http://localhost:9000/

在这里插入图片描述

3、服务续约时间设置


在服务的提供者,设置心跳间隔,设置续约到期时间

修改product_service当中的application.yml

在这里插入图片描述

server:

port: 9001 #端口

spring:

application:

name: service-product #服务名称

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8

username: root

password: root

jpa:

database: MySQL

show-sql: true

open-in-view: true

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ipaddress:{server.port} #向组成中心注册服务ID

lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id

lease-expiration-duration-in-seconds: 10 #续约到期的时间

重新启动

在这里插入图片描述
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

结尾

查漏补缺:Java岗 千+道面试题Java基础+全家桶+容器+反射+异常等

这不止是一份面试清单,更是一种”被期望的责任“,因为有无数个待面试者,希望从这篇文章中,找出通往期望公司的”钥匙“,所以上面每道选题都是结合我自身的经验于千万个面试题中经过艰辛的两周,一个题一个题筛选出来再次对好答案和格式做出来的,面试的答案也是再三斟酌,深怕误人子弟是小,影响他人仕途才是大过,也希望您能把这篇文章分享给更多的朋友,让他帮助更多的人,帮助他人,快乐自己,最后,感谢您的阅读。

由于细节内容实在太多啦,在这里我花了两周的时间把这些答案整理成一份文档了,在这里只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
img.cn/833df87d7ebd4d399d5bdbe6961f50f5.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6JOd55uS5a2QaXRibHVlYm94,size_13,color_FFFFFF,t_70,g_se,x_16)
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-OWyF7800-1713128044745)]

[外链图片转存中…(img-aN3K7hwq-1713128044745)]

[外链图片转存中…(img-PB12pmh7-1713128044745)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

结尾

[外链图片转存中…(img-JZf0CJak-1713128044746)]

这不止是一份面试清单,更是一种”被期望的责任“,因为有无数个待面试者,希望从这篇文章中,找出通往期望公司的”钥匙“,所以上面每道选题都是结合我自身的经验于千万个面试题中经过艰辛的两周,一个题一个题筛选出来再次对好答案和格式做出来的,面试的答案也是再三斟酌,深怕误人子弟是小,影响他人仕途才是大过,也希望您能把这篇文章分享给更多的朋友,让他帮助更多的人,帮助他人,快乐自己,最后,感谢您的阅读。

由于细节内容实在太多啦,在这里我花了两周的时间把这些答案整理成一份文档了,在这里只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 29
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值