当前位置:网站首页>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 # 针对的服务
边栏推荐
- Hystrix 服务熔断
- Forum management system
- 数组和结构体
- spicy(二)unit hooks
- Resampling a uniformly sampled signal
- Nacos实现高可用
- Flutter record learning different animation (2)
- CMake的安装和测试
- Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (6) Question Opening Reply PPT
- 小程序毕设作品之微信二手交易小程序毕业设计成品(3)后台功能
猜你喜欢

小程序毕设作品之微信二手交易小程序毕业设计成品(1)开发概要

Transformation of traditional projects

What is the difference between mission, vision and values?

小程序毕设作品之微信积分商城小程序毕业设计成品(7)中期检查报告

sql中 exists的用法

LoadBalancer 负载均衡

EasyNVR平台级联到EasyCVR,视频播放一会就无法播放是什么原因?

vscode debugging and remote

Nacos installation and deployment

sqlmap使用教程大全命令大全(图文)
随机推荐
护网行动基本介绍
MySQ deadlock
Sentinel 流量防卫兵
Basic introduction to protect the network operations
进程优先级 nice
2022-07-29 第四小组 修身课 学习笔记(every day)
[Switch] Protocol-Oriented Programming in Swift: Introduction
2022-07-29 Group 4 Self-cultivation class study notes (every day)
Redis server启动后会做哪些操作?
cv2.polylines
ospf map
Tcp编程
微服务CAP原则
使命、愿景、价值观到底有什么区别
flutter 记录学习不一样的动画(二)
SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
高并发框架 Disruptor
Gateway 路由网关
Introduction to management for technical people 1: What is management
Nacos Configuration Center