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

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

相关推荐
空中湖16 小时前
Spring AI RAG 完整实战:从零搭建企业知识库问答系统
java·人工智能·spring
sun032221 小时前
【笔记】Spring MVC 相关介绍
java·spring·mvc
野蛮人6号1 天前
黑马天机学堂系列问题之选集8本地服务报错
java·spring cloud·黑马程序员·天机学堂·黑马天机学堂
XS0301061 天前
Spring AI 第二课-流式输出 & 运行时动态参数配置
java·人工智能·spring
额恩661 天前
阶段五:HttpOnly Cookie 登录持久化
java·spring
就是小王同学啊1 天前
Spring小技巧之设计模式
sql·spring·设计模式
weixin_436525071 天前
Spring + Dubbo + Zookeeper项目依赖注入
spring·dubbo·java-zookeeper
L-影1 天前
springboot启动流程
java·spring boot·spring
早川9191 天前
有关线程池知识
spring