Spring Cloud eureka

一、使用场景

Eureka 是 Netflix 开源的服务注册与发现组件,解决的是微服务架构中服务之间如何找到彼此的问题。

简单举个例子:当工程A需要远程调用工程B的服务时,我们可以使用 RestTemplate 来实现远程调用(可以看上一篇RestTemplate的文章),当更换机器,或者新增机器时,这个URL就需要跟着变更,就需要去通知所有的相关服务去修改。随之而来的就是各个项目的配置⽂件反复更新

java 复制代码
package com.linzhixin.order.service;

import com.linzhixin.order.mapper.OrderMapper;
import com.linzhixin.order.model.OrderInfo;
import com.linzhixin.order.model.ProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    public OrderInfo selectOrderById(Integer orderId) {
        OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
        String url = "http://127.0.0.1:9090/product/" + orderInfo.getProductId();
        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
        orderInfo.setProductInfo(productInfo);
        return orderInfo;
    }

}

此时注册中心的作用就体现出来了,eureka 就是一个常见的注册中心。服务提供者通过在注册中心注册服务信息,服务消费者从注册中心查询服务提供者的地址,并通过该地址调用服务提供者的接口。

二、使用方法

1、搭建 Eureka Server(Eureka-server是一个独立的子模块)

1、搭建 Eureka-server 子模块

2、需要引入 euraka-server 依赖

XML 复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

3、项目构建插件

html 复制代码
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

4、完善启动类

java 复制代码
package com.linzhixin.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

5、编写配置文件

html 复制代码
# Eureka相关配置
# Eureka 服务
server:
  port: 10010
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false # 表示是否从Eureka Server获取注册信息,默认为true.因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这里设置为false
    register-with-eureka: false # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
    service-url:
      # 设置与Eureka Server的地址,查询服务和注册服务都需要依赖这个地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

6、启动服务

访问注册中心:http://127.0.0.1:10010/

可以观察到 eureka-server 已经启动成功了

2、服务注册

接下来把 服务提供方注册到 eureka-server 中

1、在服务提供方(product-service)的 pom 文件中引入 eureka-client 依赖

html 复制代码
<!-- eureka-client-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2、完善服务提供方的配置文件

添加服务名称和eureka地址

html 复制代码
spring:
 application:
 name: product-service
eureka:
 client:
 service-url:
 defaultZone: http://127.0.0.1:10010/eureka

3、启动服务

刷新注册中心:http://127.0.0.1:10010/

可以看到 product-service 已经注册到 eureka 上了

3、服务发现

接下来我们修改服务消费者(order-service),在远程调用时,从 eureka-server 拉取服务提供者的服务信息,实现服务发现

1、服务消费者引入依赖

服务注册和服务发现都封装在eureka-client依赖中,所以服务发现时,也是引入eureka-client依赖

html 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2、完善配置文件

通过配置文件得到 eureka 地址,从而拉取服务提供者的服务信息

html 复制代码
spring:
 application:
 name: product-service
eureka:
 client:
 service-url:
 defaultZone: http://127.0.0.1:10010/eureka

3、远程调用

通过 DiscoveryClient 从注册中心获取服务实例信息

远程调用时,我们需要从eureka-server中获取product-service的列表(可能存在多个服务),并选择其中 一个进行调用

java 复制代码
import com.bite.order.mapper.OrderMapper;
import com.bite.order.model.OrderInfo;
import com.bite.order.model.ProductInfo;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EurekaServiceInstance;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;

@Slf4j
@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    
    @Resource
    private DiscoveryClient discoveryClient;
   
    @Autowired
    private RestTemplate restTemplate;
 
    public OrderInfo selectOrderById(Integer orderId) {
        OrderInfo orderInfo = orderMapper.selectOrderById(orderId);

        //String url = "http://127.0.0.1:9090/product/"+ orderInfo.getProductId();

        //根据应⽤名称获取服务列表 
        List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
         
        //服务可能有多个, 获取第⼀个 

        EurekaServiceInstance instance = (EurekaServiceInstance)instances.get(0);
 
        log.info(instance.getInstanceId());
 
        //拼接url 
        String url = instance.getUri()+"/product/"+ orderInfo.getProductId();
        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
        orderInfo.setProductInfo(productInfo);
        return orderInfo;
 }
}

4、启动服务

刷新注册中心:http://127.0.0.1:10010/

可以看到 order-service 已经注册到 eureka 上了

访问接口:http://127.0.0.1:8080/order/1

可以看到,远程调用也成功了

相关推荐
2601_9620652535 分钟前
【图文详解】什么是微服务?什么是SpringCloud?
spring cloud·微服务·架构
2601_962071575 小时前
SpringCloud篇(服务网关 - GateWay)
spring boot·spring cloud·gateway
文慧的科技江湖6 小时前
慧知开源虚拟电厂管理平台(VPP) Spring Cloud实现“资源管理 → 事件申报 → 调度分配 → 执行监测 → 效果评估 → 结算分账“全流程闭环。
spring cloud·开源·vpp·虚拟电厂·开源虚拟电厂平台·vpp虚拟电厂平台
卓怡学长6 小时前
w175基于springboot校园二手物品交易平台
java·spring boot·spring·maven·intellij-idea
互联网中的一颗神经元7 小时前
09. mheap:全局堆管理器
java·spring·dubbo
凤山老林7 小时前
Spring Boot 3.x 集成 Spring Cloud Kubernetes:原生服务发现、配置注入与优雅下线
spring boot·spring cloud·kubernetes
2601_962174177 小时前
Spring 核心技术解析【纯干货版】- XI:Spring 数据访问模块 Spring-Oxm 模块精讲
数据库·python·spring
2601_962189608 小时前
【SpringCloud】Gateway
java·spring cloud·gateway
2601_962122978 小时前
Spring Cloud——路由网关Zuul
后端·spring·spring cloud
亚历克斯神17 小时前
低代码与 AI 的融合趋势——从可视化搭建到自然语言生成应用
java·spring·微服务