4

SpringBoot3.1.5对应新版本SpringCloud开发(1)-Eureka注册中心 - 高同学,你好

 4 weeks ago
source link: https://www.cnblogs.com/nhgtx/p/18171301
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

服务的提供者和消费者

服务之间可以通过Spring提供的RestTemplate来进行http请求去请求另一个Springboot的项目,这就叫做服务间的远程调用。当一个服务通过远程调用去调用另一个服务时,被调用的服务就叫做服务的提供者,调用服务的服务就叫做服务的消费者。一个服务可以既是服务的提供者也是服务的消费者。

1、服务调用关系

  • 服务提供者:暴露接口给其它微服务调用
  • 服务消费者:调用其它微服务提供的接口
  • 提供者和消费者的角色其实是相对的

Eureka注册中心

  • 远程调用的问题

  • eureka原理

  • 搭建eurekaServer

eureka的作用

  • 消费者该如何获取服务提供者的具体信息
    1. 服务提供者启动时向eureka注册自己的信息
    2. eureka保存这些信息
    3. 消费者根据服务名称向eureka拉取提供者信息
  • 如果有多个服务提供者,消费者该如何选择
    1. 服务消费者利用负载均衡算法,从服务列表中挑选一个
  • 消费者如何感知服务提供者健康状态
    1. 服务提供者会每隔30s向EurekaServer发送心跳请求,报告健康状态
    2. eureka会更新记录服务列表信息,心跳不正常会被剔除
    3. 消费者就可以拉取到最新的信息

在eureka架构中,微服务角色有两类:

  • EurekaServer:服务端、注册中心
    1. 记录服务信息
  • EurekaClient:客户端
    1. provider:服务提供者
      1. 注册自己的信息到EurekaServer中
      2. 每隔30S向EurekaServer发送心跳
    2. consumer:服务消费者
      1. 根据服务名称从EurekaServer拉取服务列表
      2. 基于服务列表做负载均衡,选中一个微服务后发起远程调用

EurekaServer服务注册

  • 创建eureka服务端

父工程引入对应版本的springcloud依赖

springboot 3.1.5对应

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2022.0.4</version> <type>pom</type> <scope>import</scope></dependency>

引入eureka服务端依赖

<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency></dependencies>

启动eureka服务,在启动类上添加@EnableEurekaServer注解

@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }

配置文件中配置服务名称-地址-服务端口

spring: application: name: eureka-server #微服务名称server: port: 10086 #服务端口eureka: client: service-url: #eureka地址信息 eureka自己也是一个微服务,也会被注册到eureka中 为了做eureka集群 defaultZone: http://127.0.0.1:10086/eureka

eureka服务端自己也会被注册到eureka服务中

然后就可以访问http://127.0.0.1:10086进入eureka注册中心了

确保父工程成功引入cloud依赖后引入eureka服务端依赖

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>

在配置文件中配置eureka服务名称和地址即可

server: port: 8081spring: application: name: cloud-usereureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka

给RestTemplate添加上@LoadBalanced注解,实现同一服务多个地址的负载均衡调用

//RestTemplate spring提供的远程调用模块 允许发送http请求@Bean@LoadBalancedpublic RestTemplate restTemplate(){ return new RestTemplate();}

使用RestTemplate不通过具体的ip地址而是通过服务名称远程调用实现查询

@AutowiredRestTemplate restTemplate;@GetMapping("/order/all/{id}")public ordersUser getAllById(@PathVariable("id") Integer id){ String str1 = "http://cloud-order/order/"+id; orders od = restTemplate.getForObject(str1, orders.class); String str = "http://cloud-user/user/"+od.getOrderuserid(); user forObject = restTemplate.getForObject(str, user.class); ordersUser ordersUser = new ordersUser(); ordersUser.setOrderid(od.getOrderid()); ordersUser.setUser(forObject); ordersUser.setOrdername(od.getOrdername()); ordersUser.setOrderaddress(od.getOrderaddress()); ordersUser.setOrderprice(od.getOrderprice()); ordersUser.setOrdernumber(od.getOrdernumber()); return ordersUser;}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK