3.springcloud_eureka服务发现(Finchley.SR2)

本文深入探讨SpringCloud中服务发现的原理与实现,通过实际案例展示如何利用DiscoveryClient获取服务列表及实例信息,为微服务间通信奠定基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是一个从零开始的springcloud的系列教程,如果你从中间开始看,可能会看不明白.进入我的博客查看其他文章

前言

在之前课程中,我们学会创建了多工程项目,spring-cloud-learn.

搭建的项目有:

  • eureka-server:服务治理中心,为微服务提供注册和管理
  • eureka-client: 在此工程学会了如何向服务治理中心注册
  • eureka-client2: 一个eureka微服务工程

我们学会了搭建微服务注册和管理中心,现在我们来学习微服务如何向服务治理中心请求其他微服务信息来达到服务发现的功能.

在开始前:

  • 启动eureka-server微服务

服务发现

在框架中,服务之间的相互调用不再通过传统的ip地址来寻找到对方.而是通过服务名称,然后向服务注册中心获取服务ip地址,然后再进行调用,达到动态化增加服务和自动化发现服务的目的.

传统方式调用:

服务A -> http请求(服务B的ip地址192.168.0.100:8001)->服务B收到

传统方式调用依赖静态配置,服务A有个配置文件,里面配置了服务B的ip地址

治理框架下调用:

服务A -> 向注册中心咨询服务B的地址或从缓存中获取 -> 得到地址后发起http请求(服务B集群状况下,是一组地址,根据轮询策略来决定使用哪个ip地址) -> 服务B收到

在使用服务治理框架来发现服务,使用者不再关心服务B的具体位置,只需要关心我想使用服务名称叫B的服务,哪怕服务B的ip变更也不影响服务A的调用.大大降低了维护成本.

发现服务

教程链接:eureka服务治理和注册教程

之前我们已经掌握了一个微服务如何将自己注册到服务治理中心(eureka-sevrer),将eureka-client2工程进行启动注册,服务名称为eureka-client2,端口为7772.

Spring-Cloud通过DiscoveryClient来进行发现服务的操作,修改eureka-client工程的EurekaClientApplication类

package com.jack.eureka_client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@SpringBootApplication
@RestController
//@EnableEurekaClient (Finchley.SR2已经不需要该配置了,你只需要在配置文件中含有eureka.client.enabled的配置即可)
public class EurekaClientApplication {
    

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }

    private DiscoveryClient discoveryClient;
	
    // 得到DiscoveryClient
    @Autowired
    public void setDiscoveryClient(DiscoveryClient discoveryClient) {
        this.discoveryClient = discoveryClient;
    }
	
    @RequestMapping(value = "/discovery", method = RequestMethod.GET)
    public Object discovery() {
        // 获取服务名称列表
        List<String> names = discoveryClient.getServices();
        System.out.println("*******************" + names);
        StringBuilder builder = new StringBuilder();
        for (String name : names) {
            // 根据服务名称获取服务实例列表,可能是多个实例.
            List<ServiceInstance> instances = discoveryClient.getInstances(name);
            for (ServiceInstance instance : instances) {
                String info = instance.getServiceId() + "," +
                        instance.getHost() + "," +
                        instance.getPort() + "," +
                        instance.getUri() + "<br/>";
                System.out.println(info);
                builder.append(info);
            }
        }

        return builder.toString();
    }
}

http请求localhost:7771/discovery,得到两个服务信息,EUREKA-CLIENT,EUREKA-CLIENT2.

image.png

通过服务的详细信息动态的获取服务的ip地址,然后发送http请求.如果一个服务名称有多个实例,需要根据轮询策略来决定请求哪个具体实例,为此需要写代码去做这方面的处理.幸运的是spring-cloud已准备了工具,不需要再进行编写了.


#总结

经过上面的讲解,已经了解服务发现的概念,以及如何用代码来发现服务.不过以上还不足以让我们方便的进行服务之间的调用.SpringCloud提供两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。接下来我们会对两种方法进行深入的讲解.

项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis二次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第二代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 版本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新版本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点二----------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值