• SpringCloud系列(14)--Eureka服务发现(Discovery)


    前言:在上一章节中我们说明了一些关于服务信息的配置,在本章节则介绍一些关于Discovery的知识点及其使用

    1、Discovery是什么,有什么用

    Discovery(服务发现)是eureka的功能和特性,有时候微服务可能需要对外提供一种功能,这个功能可以对外提供服务IP、服务名称、端口号等服务信息,而这时候Discovery就能实现这个功能,Discovery能对外暴露注册进Eureka里的服务信息

    2、启用服务发现

    在provider-payment8001模块的启动类里加上@EnableDiscoveryClinet这一注解来启用服务发现

    1. package com.ken.springcloud;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    6. @SpringBootApplication
    7. //标注为Eureka Client(服务注册中心)
    8. @EnableEurekaClient
    9. @EnableDiscoveryClient
    10. public class PaymentMain {
    11. public static void main(String[] args) {
    12. SpringApplication.run(PaymentMain.class, args);
    13. }
    14. }

    效果图:

    3、实现服务发现接口

    在provider-payment8001模块的PaymentController类里注入DiscoveryClient接口,然后利用DiscoveryClient接口的方法在PaymentController类里实现服务发现接口

    1. package com.ken.springcloud.controller;
    2. import com.ken.springcloud.entities.CommonResult;
    3. import com.ken.springcloud.entities.Payment;
    4. import com.ken.springcloud.service.PaymentService;
    5. import lombok.extern.slf4j.Slf4j;
    6. import org.springframework.beans.factory.annotation.Value;
    7. import org.springframework.cloud.client.ServiceInstance;
    8. import org.springframework.cloud.client.discovery.DiscoveryClient;
    9. import org.springframework.web.bind.annotation.*;
    10. import javax.annotation.Resource;
    11. import java.util.List;
    12. @RestController
    13. @Slf4j
    14. public class PaymentController {
    15. @Resource
    16. private PaymentService paymentService;
    17. @Value("${server.port}")
    18. private String serverPort;
    19. @Resource
    20. private DiscoveryClient discoveryClient;
    21. @PostMapping("/payment/insert")
    22. public CommonResult insert(@RequestBody Payment payment) {
    23. int result = paymentService.insert(payment);
    24. log.info("插入结果{}",result);
    25. if(result > 0) {
    26. return new CommonResult(200,"插入数据库成功,提供服务的端口号为" + serverPort,result);
    27. }else {
    28. return new CommonResult(500,"插入数据库失败",result);
    29. }
    30. }
    31. @GetMapping("/payment/get/{id}")
    32. public CommonResult insert(@PathVariable("id") Long id) {
    33. Payment payment = paymentService.getPaymentById(id);
    34. log.info("查询结果{}",payment);
    35. if(payment != null) {
    36. return new CommonResult(200,"查询成功,提供服务的端口号为" + serverPort,payment);
    37. }else {
    38. return new CommonResult(500,"没有对应的数据,查询失败,查询id" + id,payment);
    39. }
    40. }
    41. @GetMapping("/payment/discovery")
    42. public Object discovery() {
    43. //获取eureka内的服务
    44. List services = discoveryClient.getServices();
    45. for (String service : services) {
    46. log.info("***service:" + service);
    47. }
    48. //获取服务名为CLOUD-PAYMENT-SERVICE下的实例
    49. List instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
    50. for (ServiceInstance instance : instances) {
    51. log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
    52. }
    53. return this.discoveryClient;
    54. }
    55. }

    效果图:

    4、调用服务发现接口,查看接口效果

    在浏览器地址栏输入http://localhost:8001/payment/discovery然后按回车调用服务发现接口,查看接口返回的信息以及控制台里打印的日志信息,如果接口能成功拿到eureka里的服务信息,则证明服务发现接口实现成功

     效果图:

  • 相关阅读:
    这几本书看了之后在工作生活上都是有用的
    【Python零基础入门篇 · 27】:文件操作
    【leetcode】【初级算法】【链表5】回文链表
    MySQL--单表查询
    详解:程序部署在服务器上,localhost可以访问Tomcat,但是外网ip无法访问
    【Leetcode每日一题】2022-09-30 面试题 01.08 零矩阵 Java实现
    探索电子元器件商城:从原型到批量生产的选择
    【Mysql】使用binlog日志进行数据库迁移和数据恢复
    34【源码】数据可视化:基于 Echarts + Python 动态实时大屏 - 视频平台
    【C++】:模板进阶
  • 原文地址:https://blog.csdn.net/m0_64284147/article/details/132251046