SpringCloud之网关获取注册中心的服务实例

Spring Cloud 获取注册中心所有服务以及服务下的所有实例

待用

/**
 * import org.springframework.cloud.client.ServiceInstance;
 * import org.springframework.cloud.client.discovery.DiscoveryClient;
 *
 * 获取每一个服务下面实例
 * 测试时,可以使用多个端口启动服务实例
 */

//感觉 DiscoveryClient 挺关键的保存了服务实例的详细信息
@Autowired
private DiscoveryClient discoveryClient;

@GetMapping("/serviceurl")
public Map<String, List<ServiceInstance>> serviceUrl() {
    Map<String, List<ServiceInstance>> msl = new HashMap<>();
    List<String> services = discoveryClient.getServices();
    for (String service : services) {
        List<ServiceInstance> sis = discoveryClient.getInstances(service);
        msl.put(service, sis);
    }
    return msl;
}

研究

DiscoveryClient.java

package org.springframework.cloud.client.discovery;

import java.util.List;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.core.Ordered;

public interface DiscoveryClient extends Ordered {
    int DEFAULT_ORDER = 0;

    String description();

    //获取服务的详细信息
    List<ServiceInstance> getInstances(String serviceId);

    //获取服务的名称,即 serviceId
    List<String> getServices();

    default int getOrder() {
        return 0;
    }
}

通过继承关系图发现有 NacosDiscoveryClient, 表明对 Nacos 注册中心有支持

266

ServiceInstance.java

public interface ServiceInstance {
    default String getInstanceId() {
        return null;
    }

    String getServiceId();

    String getHost();

    int getPort();

    boolean isSecure();

    URI getUri();

    Map<String, String> getMetadata();

    default String getScheme() {
        return null;
    }
}

结果

{
    "code": 200,
    "count": null,
    "data": {
        "platform-sso": [
            {
                "host": "x.x.x.x",
                "instanceId": null,
                //注册中心的信息
                "metadata": {
                    "nacos.instanceId": "10.x.x.144#8777#DEFAULT#DEFAULT_GROUP@@platform-sso",
                    "nacos.weight": "1.0",
                    "nacos.cluster": "DEFAULT",
                    "nacos.healthy": "true",
                    "preserved.register.source": "SPRING_CLOUD"
                },
                "port": 8777,
                "scheme": null,
                "secure": false,
                "serviceId": "platform-sso",
                "uri": "http://10.x.x.144:8777"
            }
        ],
        "platform-gateway": [],
        "platform-apihub": []
    },
    "message": "操作成功"
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud 是一个用于构建分布式系统的开源框架,它基于 Spring Boot 提供了一套完整的分布式系统解决方案。 下面是一个简单的 Spring Cloud 实例,演示了如何使用 Spring Cloud 构建微服务架构: 1. 创建服务注册中心:使用 Eureka 或 Consul 等服务注册中心,用于管理服务的注册与发现。可以创建一个名为 "eureka-server" 的 Spring Boot 应用,添加相应的依赖并进行配置,使其成为一个服务注册中心。 2. 创建服务提供者:创建一个或多个服务提供者,用于提供具体的业务功能。可以创建一个名为 "product-service" 的 Spring Boot 应用,添加相应的依赖并进行配置,使其成为一个服务提供者。在该应用中,需要将服务注册到服务注册中心,并提供相应的接口供其他服务调用。 3. 创建服务消费者:创建一个或多个服务消费者,用于调用服务提供者提供的接口。可以创建一个名为 "order-service" 的 Spring Boot 应用,添加相应的依赖并进行配置,使其成为一个服务消费者。在该应用中,需要从服务注册中心获取服务提供者的地址,并通过调用相应的接口实现业务逻辑。 4. 创建网关服务:创建一个网关服务,用于统一管理和转发请求。可以创建一个名为 "gateway-service" 的 Spring Boot 应用,添加相应的依赖并进行配置,使其成为一个网关服务。在该应用中,可以配置路由规则、过滤器等,实现请求的转发和处理。 以上是一个简单的 Spring Cloud 实例,演示了如何构建基于微服务架构的分布式系统。通过使用 Spring Cloud 提供的各种组件和功能,可以方便地实现服务注册与发现、服务调用、负载均衡、熔断降级、分布式配置等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值