SpringCloud Eureka基本使用

SpringCloud Eureka基本使用,第1张

目录

1.注册中心的对比

​2.Eureka 基础架构​

3.Eureka交互流程

4.搭建单例Eureka Server服务注册中心 

1.引入pom

1.1 lagou-parent中引入Spring Cloud 依赖环境

 1.2引入 -Eureka server依赖

 3.写yml

 4.主启动

 5.访问注册中心

 6.商品微服务和页面静态化微服务注册到Eureka

 6.1 pom文件中添加Eureka Client依赖

 6.2yml配置Eureka服务端信息

 6.3 修改启动类 


1.注册中心的对比

CAP定理又称CAP原则,指的是在一个分布式系统中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),最多只能同时三个特性中的两个,三者不可兼得

2.Eureka 基础架构
3.Eureka交互流程

 

1.首先Eureka包含两个组件,一个Eureka Server,一个Eureka Client

2.服务的提供者和消费者都是Eureka的Client,并且都要向注册中心进行注册

3.注册中心是集群的话,server自己也要注册进去,那么注册之后会进行集群之间的数据同步

4.微服务启动后并且注册后,每隔30s会周期性的向Eureka Server发送心跳,证明自己还活着,如果Eureka Server 90s还没检测到的话,就会将服务进行移除

5.Eureka Client会缓存注册中心的的服务信息,即使注册中心挂了,或者服务注册挂了,服务消费者依然可以使用缓存中的信息找到服务提供者(在一定的时间内)

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

4.搭建单例Eureka Server服务注册中心  1.引入pom 1.1 lagou-parent中引入Spring Cloud 依赖环境

Spring Cloud 是一个综合的项目,下面有很多子项目,比如eureka子项目




org.springframework.cloud
spring-cloud-dependencies
Greenwich.RELEASE
pom
import


 1.2引入 -Eureka server依赖

lagou-cloud-eureka工程pom.xml中引入依赖




org.springframework.cloud
spring-cloud-starter-netflix-eureka-server

 注意:在父工程的pom文件中手动引入jaxb的jar,因为Jdk9之后默认没有加载该模块,Eureka
Server使用到,所以需要手动导入,否则EurekaServer服务无法启动

父工程:



com.sun.xml.bind
jaxb-core
2.2.11


javax.xml.bind
jaxb-api


com.sun.xml.bind
jaxb-impl
2.2.11


org.glassfish.jaxb
jaxb-runtime
2.2.10-b140310.1920


javax.activation
activation
1.1.1


 3.写yml

在yml文件中配置Eureka server服务端口,服务名等信息

#Eureka server服务端口
server:
port: 9200
spring:
application:
name: lagou-cloud-eureka-server # 应用名称,会在Eureka中作为服务的id标识
(serviceId)
eureka:
instance:
hostname: localhost
client:
service-url: # 客户端与EurekaServer交互的地址,如果是集群,也需要写其它Server的地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
register-with-eureka: false # 自己就是服务不需要注册自己
fetch-registry: false #自己就是服务不需要从Eureka Server获取服务信息,默认为true,置
为false
 4.主启动

编写启动类,声明当前服务为Eureka注册中心

package com.lagou.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
 声明本项目是一个Eureka服务
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
 5.访问注册中心

访问http://127.0.0.1:9200,如果看到如下页面(Eureka注册中心后台),则表明EurekaServer
发布成功

 6.商品微服务和页面静态化微服务注册到Eureka  6.1 pom文件中添加Eureka Client依赖


org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
 6.2yml配置Eureka服务端信息
eureka:
client:
serviceUrl: # eureka server的路径
defaultZone: http://localhost:9200/eureka/
instance:
#使用ip注册,否则会使用主机名注册了(此处考虑到对老版本的兼容,新版本经过实验都是ip)
prefer-ip-address: true
#自定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address,早期版本是ipAddress
instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@
 6.3 修改启动类 
@SpringBootApplication
@EnableDiscoveryClient //@EnableEurekaClient
public class PageApplication {
public static void main(String[] args) {
SpringApplication.run(PageApplication.class,args);
} 
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}


 


 



 


 


 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/736101.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-28
下一篇2022-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存