当前位置:网站首页>Eureka注册中心
Eureka注册中心
2022-07-30 03:51:00 【Cold Snowflakes】
基于RestTemplate发起的http请求实现远程调用
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RestTemplate restTemplate;
public Order queryOrderById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId);
// 2.使用RestTemplate发起Http请求,实现远程调用
// 将响应的结果反序列化为User对象
String url = "http://localhost:8081/user/" + order.getUserId();
User user = restTemplate.getForObject(url, User.class);
// 3.填充
order.setUser(user);
// 4.返回
return order;
}
}
Eureka注册中心
每一个服务启动的时候都会在Eureka注册中心进行注册信息。
Consumer调用Provider的时候,会在注册中心拉取,Provider的注册信息。
每一个服务每隔30s都会向Eureka发一次心跳请求,报告健康状态,如果不跳了,Eureka就会删除其注册信息。
搭建Eureka注册中心
新建一个maven模块,添加依赖
<!-- eureka服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<!-- 版本号需要匹配spring-cloud-dependencies中依赖管理的netflix版本 -->
<version>3.0.3</version>
</dependency>
启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
配置文件
server:
port: 10086
spring:
application: # 服务名称
name: eureka_server
eureka:
client:
service-url: # eureka自己的地址信息 (eureka也会把自己的信息注册进去)
defaultZone: http://localhost:10086/eureka
启动该模块,就能看到这样的信息
Eureka服务注册
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.0.3</version>
</dependency>
spring:
application:
name: orderService # 服务名称不能使用下划线
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
Eureka服务拉取
基于服务名称来进行拉取
public Order queryOrderById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId);
// String url = "http://localhost:8081/user/" + order.getUserId();
// 将IP地址和端口号改为服务名称
String url = "http://userService/user/" + order.getUserId();
User user = restTemplate.getForObject(url, User.class);
// 3.填充
order.setUser(user);
// 4.返回
return order;
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
实现原理:
发起请求后会被LoadBalancerInterceptor负载均衡拦截器所拦截,将服务名称提取交给RibbonLoadBalancerClient处理。
RibbonLoadBalancerClient依据服务名称从DynamicServerListLoadBalancer中获取真实的IP地址端口号列表,再依据IRule
负载均衡策略,去选择一个最终的。
调整负载均衡策略:
第一种方式:作用是全局的
@Bean
public IRule randomRule(){
return new RandomRule();
}
第二种方式:针对某个服务
userService:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Ribbon默认采用懒加载,第一次访问时才会创建LoadBalanceClient,请求时间会较长。
饥饿加载会在项目启动时创建,降低第一次访问的耗时
ribbon:
eager-load:
enabled: true
clients: userService # 针对的服务
边栏推荐
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (3) Background Functions
- 基于全志D1-H和XR806的名贵植物监控装置
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (2) Mini Program Function
- Nacos配置中心
- Nacos Configuration Center
- Gateway routing gateway
- Nacos achieves high availability
- Solve the problem of compiling and installing gdb-10.1 unistd.h:663:3: error: #error “Please include config.h first.”
- CMake installation and testing
- Nacos服务注册与发现
猜你喜欢

传输层详解

Hystrix service circuit breaker

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (5) Task Book

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (6) Question Opening Reply PPT

监控页面部署

How does the AI intelligent security video platform EasyCVR configure the simultaneous transmission of audio and video?

基于全志D1-H和XR806的名贵植物监控装置

OpenFeign实现降级

Nacos cluster partition

第51篇-知乎请求头参数分析【2022-07-28】
随机推荐
Nacos配置中心
ospf 导图
SQL Server数据类型转换函数cast()和convert()详解
运行时间监控:如何确保网络设备运行时间
What is the difference between mission, vision and values?
一起来学习flutter 的布局组件
小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告
护网行动基本介绍
Nacos集群分区
mysql 结构、索引详解
小程序毕设作品之微信积分商城小程序毕业设计成品(2)小程序功能
小程序毕设作品之微信积分商城小程序毕业设计成品(3)后台功能
2022-07-29 第四小组 修身课 学习笔记(every day)
ospf map
新接口——“淘特”关键词搜索的API接口
Has been empty, a straightforward, continue to copy the top off!
The difference between BGP room and ordinary room in Beijing
TCP congestion control technology and acceleration principle of BBR
Hystrix service circuit breaker
小程序毕设作品之微信二手交易小程序毕业设计成品(1)开发概要