• 微服务框架 SpringCloud微服务架构 3 Eureka 3.5 服务发现


    微服务框架

    【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】

    SpringCloud微服务架构

    3 Eureka

    3.5 服务发现
    3.5.1 在order-service完成服务拉取

    之前我们已经完成 了服务的注册

    在这里插入图片描述

    且eureka 管理界面可以查看到服务列表

    在这里插入图片描述

    现在就要做服务的发现【拉取】

    服务拉取是基于服务名称获取服务列表,然后在对服务列表做负载均衡

    ① 修改OrderService的代码,修改访问的url路径,用服务名代替ip、端口:

    package cn.itcast.order.service;
    
    import cn.itcast.order.mapper.OrderMapper;
    import cn.itcast.order.pojo.Order;
    import cn.itcast.order.pojo.User;
    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 Order queryOrderById(Long orderId) {
            // 1.查询订单
            Order order = orderMapper.findById(orderId);
            //2. 利用RestTemplate发起HTTP请求查询用户
            //2.1 URL路径
            String url = "http://userservice/user/" + order.getUserId();
            //2.2 发送HTTP请求,实现远程调用
            User user = restTemplate.getForObject(url, User.class);
            //3. 封装user到order
            order.setUser(user);
            // 4.返回
            return order;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在这里插入图片描述

    ② 在order-service项目的启动类OrderApplication中的RestTemplate添加负载均衡注解:

    package cn.itcast.order;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @MapperScan("cn.itcast.order.mapper")
    @SpringBootApplication
    public class OrderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(OrderApplication.class, args);
        }
    
        //创建RestTemplate并注入Spring容器
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在这里插入图片描述

    其实现在代码已经完成 了

    重启order 服务

    在这里插入图片描述

    现在直接调order 服务,看看它到底调的user 1 还是user 2

    在这里插入图片描述

    第一次,

    在这里插入图片描述

    第二次

    OK,查看日志

    user 1

    在这里插入图片描述

    user2

    在这里插入图片描述

    这就实现了负载均衡,而且我们这次根本没有直接写到用哪个服务,只是写了服务名称

    在这里插入图片描述

    OK,这就是服务的发现【拉取】

    3.5.2 总结
    1. 搭建EurekaServer

      • 引入eureka-server依赖
      • 添加@EnableEurekaServer注解
      • 在application.yml中配置eureka地址
    2. 服务注册

      • 引入eureka-client依赖
      • 在application.yml中配置eureka地址
    3. 服务发现

      • 引入eureka-client依赖
      • 在application.yml中配置eureka地址
      • 给RestTemplate添加@LoadBalanced注解
      • 用服务提供者的服务名称远程调用
  • 相关阅读:
    耗时4个月,阿里架构师打造Java面试突击文档,10位朋友已拿offer
    Linux操作系统平台
    2-3 Moves Codeforces 1716A
    做事软件开发-法的重要性所在以及合理结论的认识
    【AI面试】CrossEntropy Loss 、Balanced Cross Entropy、 Dice Loss 和 Focal Loss 横评对比
    Stream快速入门
    mysql8动态子查询;LATERAL
    vue实现canvas电子签名
    从零开始学网站建设:从需求分析到上线发布
    JSON Schema的应用(具体的使用场景)
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/128091035