网站首页 > 技术文章 正文
什么是Eureka?
Eureka是一个服务治理组件,主要用于实现分布式系统中的服务注册与发现。
搭建Eureka服务端
我们可以通过在一个基础的springboot工程中引入eureka的依赖实现一个eureka服务端。
pom依赖:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
3.1.3
在启动类上添加注解
Application.java
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
增加配置
application.yml
server:
port: 8761
eureka:
client:
#是否将自己注册到Eureka Server 默认为true:注册
register-with-eureka: false
#是否从Eureka Server获取注册的服务信息 默认为true:拉取
fetch-registry: false
service-url:
#访问eureka时使用:http://localhost:9000 ,不要使用:http://localhost:9000/eureka/
defaultZone: http://localhost:8761/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enable-self-preservation: false
renwal-percent-threshold: 0.60
eviction-interval-timer-in-ms: 10000
peer-node-read-timeout-ms: 30000
peer-node-connect-timeout-ms: 10000
到这里我们开发的eureka服务端就完成了,然后启动看看。
搭建eureka客户端
和服务端相似,在一个基础的springboot工程中引入eureka-client依赖。
pom依赖:
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
3.1.3
在启动类上添加注解
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
添加配置
application.yml
spring:
application:
name: eureka-client
server:
port: 9010
eureka:
client:
#是否将自己注册到Eureka Server 默认为true:注册
register-with-eureka: true
#是否从Eureka Server获取注册的服务信息 默认为true:拉取
fetch-registry: true
registryFetchIntervalSeconds: 10
service-url:
#访问eureka时使用:http://localhost:9000 ,不要使用:http://localhost:9000/eureka/
defaultZone: http://localhost:8761/eureka/
instance:
lease-expiration-duration-in-seconds: 10
lease-renewal-interval-in-seconds: 3
eureka客户端就完成了,然后启动看看。
Feign调用
Feign调用是指在微服务架构中,使用Feign这个声明式的Web服务客户端来简化HTTP API调用的过程。
简单的说就是把原生的http调用的代码包装成一个方法,在需要请求的地方直接调用这个方法就可以发起http请求。
下面展示一下,首先准备一个eureka,两个微服务,一个postman。
贴一下eureka-client的feign接口代码
@Component
@FeignClient(value = "eureka-client2")
public interface TestFeignClient {
@GetMapping("test/1")
String getOne();
}
贴一下eureka-client2的接口代码
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/1")
public String getOne(){
return "123456";
}
}
接下来我们会把eureka-client作为请求方,eureka-client2作为服务方,然后使用postman请求eureka-client,之后eureka-client 会请求eureka-client2。结果如下,调用成功。
工作原理
eureka服务端将注册上来的服务统计在注册表(ConcurrentHashMap)中,当发生注册和下线动作时都会刷新注册表并同步到readWriteCacheMap中,同时readWriteCacheMap每180秒也会同步一次。每隔30秒服务列表同步到readOnlyCacheMap中,而eureka-client就是从readOnlyCacheMap中获取服务列表。
在源码中可以看到readWriteCacheMap过期时间是180秒,当readWriteCacheMap过期之后会从注册表同步服务列表信息。
猜你喜欢
- 2025-03-10 mysql 修改密码 报错 ERROR 1396 (HY000): 解决方案
- 2025-03-10 教你部署在线客服 - 宝塔面板篇 5:配置服务器主程序
- 2025-03-10 手把手教你搭建免费个人博客网站
- 2025-03-10 10分钟学会VS NuGet包私有化部署
- 2025-03-10 Hexo建站全攻略
- 2025-03-10 1.8K Star!Cool-Admin-Vue:AI编码+流程编排,重新定义后台开发
- 2025-03-10 监控和扩展:为应用程序添加系统监控,AdminServer组件
- 2025-03-10 后台权限管理系统,开源免费,模块化、插件化、AI极速开发CRUD
- 2025-03-10 Spring Cloud Gateway实战之五:内置filter
- 2025-03-10 Farm 火了!比 Rspack/Vite 更快的打包方案!
- 03-12我实现了Java多返回值
- 03-12Python 函数中的 4 种参数类型
- 03-12「洞唐Coder」自学Python编程的第0021天:自定义函数(下)
- 03-12Python函数的10个级别,你达到了第几级?
- 03-12Python中的元组和列表的相同点和不同点是啥呢么?
- 03-12Python有序对象元组处理技术
- 03-12Python 中的函数和模块
- 03-12震惊!Python中定义函数竟然这样用,你知道吗?
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)