当前位置:网站首页>Eureka Registry
Eureka Registry
2022-07-30 03:57: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请求,实现远程调用
// Deserialize the result of the response to User对象
String url = "http://localhost:8081/user/" + order.getUserId();
User user = restTemplate.getForObject(url, User.class);
// 3.填充
order.setUser(user);
// 4.返回
return order;
}
}
Eureka注册中心
Every time the service starts, it will be thereEurekaRegistration center for registration information.
Consumer调用Provider的时候,It will be pulled from the registry,Provider的注册信息.
Every service every other30s都会向EurekaSend a heartbeat request,报告健康状态,If you don't jump,EurekaIts registration information will be deleted.
搭建Eureka注册中心
新建一个maven模块,添加依赖
<!-- eureka服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<!-- The version numbers need to matchspring-cloud-dependenciesin dependency managementnetflix版本 -->
<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: # eurekaown address information (eurekaYou will also register your own information)
defaultZone: http://localhost:10086/eureka
启动该模块,You can see this information
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服务拉取
Pull based on service name
public Order queryOrderById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId);
// String url = "http://localhost:8081/user/" + order.getUserId();
// 将IPThe address and port number are changed to the service name
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();
}
实现原理:
After the request is made, it will be receivedLoadBalancerInterceptorIntercepted by load balancing interceptors,Extract the service name to handRibbonLoadBalancerClient处理.
RibbonLoadBalancerClientBy service name fromDynamicServerListLoadBalancer中获取真实的IPList of address port numbers,再依据IRule
负载均衡策略,to choose a final one.
调整负载均衡策略:
第一种方式:作用是全局的
@Bean
public IRule randomRule(){
return new RandomRule();
}
第二种方式:针对某个服务
userService:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Ribbon默认采用懒加载,It is only created on the first visitLoadBalanceClient,The request time will be longer.
饥饿加载会在项目启动时创建,降低第一次访问的耗时
ribbon:
eager-load:
enabled: true
clients: userService # 针对的服务
边栏推荐
- Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Work (2) Mini Program Function
- ospf 导图
- 小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告
- How to solve the error "no such file or directory" when EasyCVR starts?
- Roperties类配置文件&DOS查看主机网络情况
- 逆向理论知识3【UI修改篇】
- sublime text 3 settings
- 【转】Swift 中的面向协议编程:引言
- LoadBalancer load balancing
- Sentinel Traffic Guard
猜你喜欢

小程序毕设作品之微信积分商城小程序毕业设计成品(4)开题报告

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (7) Interim Inspection Report

小程序毕设作品之微信二手交易小程序毕业设计成品(3)后台功能

How to solve the error "no such file or directory" when EasyCVR starts?

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

OpenFeign实现负载均衡

LoadBalancer load balancing

Rpc 和 gRpc 简介汇总

Let's learn the layout components of flutter together

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (3) Background Functions
随机推荐
redis分布式锁的原子保证
[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?
数组和结构体
Anti-shake and throttling
小程序毕设作品之微信积分商城小程序毕业设计成品(2)小程序功能
Nacos namespace
运行时间监控:如何确保网络设备运行时间
星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!
When the EasyNVR platform is cascaded to the EasyCVR, why can't the video be played after a while?
Boutique: Taobao/Tmall Get Order Details API for Purchased Products
(redistribute, special comprehensive experiment ospf area)
Starlight does not ask passers-by!The young lady on the Wuhan campus successfully switched to software testing in three months and received a salary of 9k+13!
国内首家沉浸式高逼真元宇宙,希元宇宙正式上线
STM32 SPI+WM8978语音回环
Flutter records and learns different animations (1)
小程序毕设作品之微信积分商城小程序毕业设计成品(3)后台功能
TCP congestion control technology and acceleration principle of BBR
sublime text 3 settings
New interface - API interface for "Taote" keyword search
STM32 SPI+WM8978 voice loopback