Spring Cloud-超详细讲解Eureka服务注册中心

表示是否向 Eureka 注册中心注册自己(这个模块本身是服务器,所以不需要)

register-with-eureka: false

fetch-registry如果为false,则表示自己为注册中心,客户端的化为 ture

fetch-registry: false

Eureka监控页面~

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/

#相当于:http://localhost:7001/eureka/

(4)编写Spring Boot启动器

注意:要在主启动器上方添加 @EnableEurekaServer表示 服务端的启动类,可以接受别人注册进来

package com.yixin.springcloud;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

@SpringBootApplication

@EnableEurekaServer

public class EurekaServer_7001 {

public static void main(String[] args) {

SpringApplication.run(EurekaServer_7001.class,args);

}

}

(5)启动并访问http://localhost:7001/

页面出现如下,表示成功!

4.3  配置springcloud-provider-blog-8001


4.3.1 配置

(1)建立以下目录

(2)编写pom.xml

org.springframework.cloud

spring-cloud-starter-eureka

1.4.6.RELEASE

org.springframework.boot

spring-boot-test

2.4.5

org.springframework.boot

spring-boot-starter-web

2.4.5

org.springframework.boot

spring-boot-devtools

(3)编写 application.yml

server:

port: 8001

spring:

application:

name: springcloud-provider-blog

Eureka配置:配置服务注册中心地址

eureka:

client:

service-url:

defaultZone: http://localhost:7001/eureka/

(4)编写启动类

注意:要为主启动类添加@EnableEurekaClient注解

package com.yixin.springcloud;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableEurekaClient

@EnableDiscoveryClient

public class BlogProvider_8001 {

public static void main(String[] args) {

SpringApplication.run(BlogProvider_8001.class,args);

}

}

(5)启动测试

依次启动:

服务端:springcloud-eureka-7001------>客户端:springcloud-provider-blog-8001

访问监控页:http://localhost:7001/

可以发现,eureka已经成功将我们的客户端注册进去了。

4.3.2 关于Status

可以发现如果我们不对其描述信息进行描述,Eureka会采用默认的描述方式

(1)修改默认描述信息

修改springcloud-provider-blog-8001下的application.yml

Eureka配置:配置服务注册中心地址

eureka:

client:

service-url:

defaultZone: http://localhost:7001/eureka/

instance:

instance-id: springcloud-provider-dept-8001 #修改Eureka上的默认描述信息

重新启动springcloud-provider-blog-8001

可以发现,成功修改为我们自定义的描述信息了!

(2)配置服务加载的监控信息

但我们点击这个描述链接时,会跳到一个页面

现在我们希望在这个页面添加一些我们自己的描述信息。

修改springcloud-provider-blog-8001

a、pom.xml中添加依赖

org.springframework.boot

spring-boot-starter-actuator

b、修改配置文件application.yml

配置info:

Eureka配置:配置服务注册中心地址

eureka:

client:

service-url:

defaultZone: http://localhost:7001/eureka/

instance:

instance-id: springcloud-provider-dept-8001 #修改Eureka上的默认描述信息

info配置

info:

app.name: yixin-springcloud # 项目的名称

company.name: yixin # 公司的名称

c、重新启动springcloud-provider-blog-8001,进入描述信息链接

结果:

(3)客户端宕机

如果在运行过程中,我们的客户端springcloud-provider-blog-8001突然停止运行了,那么等30s后 监控会开启保护机制,即不会立即清理该实例,依旧会对该微服务的信息进行保存!

4.3.3 Eureka自我保护机制

一句话总结就是:好死不如赖活着,某时刻某一个微服务不可用,eureka不会立即清理,依旧会对该微服务的信息进行保存!

(1)保护机制出发原因

默认情况下eurekaclient会定时向eurekaserver端发送心跳(默认是30S),如果eurekaserver在一定时间内没有收到eurekaclient发送的心跳,便会把该实例从注册服务列表中剔除(默认是90S),但是短时间内丢失大量的实例心跳的话,这时候eureka server会开启自我保护机制,所有的注册服务实例都将被保护起来,不被剔除。

(2)保护机制的目的

目的是为了避免网络连接故障,在发生网络故障时,微服务和注册中心之间无法正常通信,但服务本身是健康的,不应该注销该服务。

如果eureka因网络故障而把微服务误删了,那即使网络恢复了,该微服务也不会重新注册到eureka server了,因为只有在微服务启动的时候才会发起注册请求,后面只会发送心跳和服务列表请求,这样的话,该实例虽然是运行着,但永远不会被其它服务所感知。

所以,eureka server在短时间内丢失过多的客户端心跳时,会进入自我保护模式,该模式下,eureka会保护注册表中的信息,不在注销任何微服务,当网络故障恢复后,eureka会自动退出保护模式。自我保护模式可以让集群更加健壮。

(3)设置自我保护机制开关

使用场景:

我们在开发测试阶段,需要频繁地重启发布,如果触发了保护机制,则旧的服务实例没有被删除,这时请求有可能跑到旧的实例中,而该实例已经关闭了,这就导致请求错误,影响开发测试。所以,在开发测试阶段,我们可以把自我保护模式关闭,只需在eureka server配置文件中加上如下配置即

eureka.server.enable-self-preservation=false

4.3.4 获取注册进来的微服务信息

(1)编写BlogController

package com.yixin.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.cloud.client.ServiceInstance;

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

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

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

import java.util.List;

@RestController

public class BlogController {

//获取端口号

@Value(“${server.port}”)

private Integer port;

/**

  • DiscoveryClient 可以用来获取一些配置的信息,得到具体的微服务!

*/

@Autowired

private DiscoveryClient client;

@GetMapping(“/getPort”)

public Integer getTodayFinishOrderNum(){

return port;

}

//注册进来的微服务,获取一些消息

@GetMapping(“/blog/discovery”)

public Object discovery(){

//获取微服务列表清单

List services = client.getServices();

System.out.println("discovery => services: "+services);

//得到一个具体的微服务信息

List instances = client.getInstances(“SPRINGCLOUD-PROVIDER-BLOG”);

for (ServiceInstance instance : instances) {

System.out.println(instance.getHost() + “\t” +

instance.getPort() + “\t” +

instance.getUri()+ “\t” +

instance.getServiceId());

}

return this.client;

}

}

(2)启动类添加@EnableDiscoveryClient 注解

@SpringBootApplication

@EnableEurekaClient

@EnableDiscoveryClient

public class BlogProvider_8001 {

public static void main(String[] args) {

SpringApplication.run(BlogProvider_8001.class,args);

}

}

(3)重新启动springcloud-provider-blog-8001

**访问:**http://localhost:8001/blog/discovery

访问成功!

4.4 配置springcloud-consumer-blog-80


(1)创建以下目录

(2)编写pom.xml

org.springframework.cloud

spring-cloud-starter-eureka

1.4.6.RELEASE

org.springframework.boot

spring-boot-test

2.4.5

org.springframework.boot

spring-boot-starter-web

2.4.5

org.springframework.boot

spring-boot-devtools

(3)编写application.yml

server:

port: 8000

spring:

application:

name: springcloud-consumer-blog

Eureka配置:配置服务注册中心地址

eureka:

client:

service-url:

defaultZone: http://localhost:7001/eureka/

instance:

instance-id: springcloud-consumer-blog8000

(4)编写ConfigBean

RestTemplate并没有注入到Spring中,所以才需要我们自己编写配置类注册到Spring中

package com.yixin.springcloud.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.client.RestTemplate;

@Configuration

public class ConfigBean {

@Bean

public RestTemplate getRestTemplate(){

return new RestTemplate();

}

}

(5)编写BlogConsumerController

package com.yixin.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

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

import org.springframework.web.client.RestTemplate;

@RestController

public class BlogConsumerController {

@Autowired

private RestTemplate restTemplate;

private static final String REST_URL_PREFIX=“http://localhost:8001”;

@GetMapping(“/consumer/blog”)

public String get(){

return “消费端:”+restTemplate.getForObject(REST_URL_PREFIX +“/blog/discovery”, String.class);

}

}

(6)编写主启动类

注意:要在主启动类添加@EnableEurekaClient注解。

package com.yixin.springcloud;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableEurekaClient

public class BlogConsumer_80 {

public static void main(String[] args) {

SpringApplication.run(BlogConsumer_80.class,args);

}

}

(7)测试

依次启动以下微服务:

(1)springcloud-eureka-7001

(2)springcloud-provider-blog-8001:服务生产方

(3)springcloud-consumer-blog-80:服务消费方

**访问:**http://localhost:8000/consumer/blog

测试成功!

五、搭建集群Eureka Server

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

搭建集群Eureka Server的好处就是:当我们任意一个Eureka注册中心宕机了,由于有其它Eureka注册中心,就不会导致服务中断,其它的Eureka注册中心将会接替已经宕机的Eureka注册中心的工作,不至于一个注册中心奔溃而导致全部服务停止。

(1)新建springcloud-eureka-7002、springcloud-eureka-7003 模块

(2)编写pom.xml

springcloud-eureka-7002和springcloud-eureka-7003的pom.xml的配置和7001一样

org.springframework.cloud

spring-cloud-starter-eureka-server

1.4.6.RELEASE

org.springframework.boot

spring-boot-devtools

(3)编写自定义本机名

为了更加接近真实开发,配置一些自定义本机名字,例如:我们之前localhost==127.0.0.1

a、找到本机hosts文件并打开

b、在hosts文件最后加上,要访问的本机名称,默认是localhost

(3)编写配置类

a、springcloud-eureka-7001

在集群中使springcloud-eureka-7001关联springcloud-eureka-7002、springcloud-eureka-7003,其它两个配置类也一样

application.yml:

server:

port: 7001

Eureka配置

eureka:

instance:

Eureka服务端的实例名字

hostname: eureka7001.com

client:

表示是否向 Eureka 注册中心注册自己(这个模块本身是服务器,所以不需要)

register-with-eureka: false

fetch-registry如果为false,则表示自己为注册中心,客户端的化为 ture

fetch-registry: false

Eureka监控页面~

service-url:

defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

集群(关联):7001关联7002、7003

单机: 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/

b、springcloud-eureka-7002

application.yml:

server:

port: 7002

Eureka配置

eureka:

instance:

Eureka服务端的实例名字

hostname: eureka7002.com

client:

表示是否向 Eureka 注册中心注册自己(这个模块本身是服务器,所以不需要)

register-with-eureka: false

fetch-registry如果为false,则表示自己为注册中心,客户端的化为 ture

fetch-registry: false

Eureka监控页面~

service-url:

defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/

集群(关联):7002关联7001、7003

单机: 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/

c、springcloud-eureka-7003

application.yml:

server:

port: 7003

Eureka配置

eureka:

instance:

Eureka服务端的实例名字

hostname: eureka7003.com

client:

表示是否向 Eureka 注册中心注册自己(这个模块本身是服务器,所以不需要)

register-with-eureka: false

fetch-registry如果为false,则表示自己为注册中心,客户端的化为 ture

fetch-registry: false

Eureka监控页面~

service-url:

defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

集群(关联):7003关联7001、7002

单机: 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/

(4)编写主启动类与springcloud-eureka-7001类似

(5)修改springcloud-provider-blog-8001的配置文件

修改其defaultZone,让其关联三个Server

application.yml:

Eureka配置:配置服务注册中心地址

eureka:

client:

service-url:

defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

instance:

instance-id: springcloud-provider-dept-8001 #修改Eureka上的默认描述信息

最后

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

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

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

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

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

集群(关联):7003关联7001、7002

单机: 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/

(4)编写主启动类与springcloud-eureka-7001类似

(5)修改springcloud-provider-blog-8001的配置文件

修改其defaultZone,让其关联三个Server

application.yml:

Eureka配置:配置服务注册中心地址

eureka:

client:

service-url:

defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

instance:

instance-id: springcloud-provider-dept-8001 #修改Eureka上的默认描述信息

最后

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

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

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

[外链图片转存中…(img-J1xwL99T-1715060926409)]

[外链图片转存中…(img-2rMUhEAs-1715060926409)]

[外链图片转存中…(img-cjl4beCK-1715060926410)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值