山东大学软件学院项目实训-创新实训-基于大模型的旅游平台(二十一)- 微服务(1)

微服务

1.认识微服务

SpringCloud底层是依赖于SpringBoot的,并且有版本的兼容关系,如下:

2. 服务拆分

需求 : 把订单信息和用户信息一起返回

从订单模块向用户模块发起远程调用 , 把查到的结果一起返回

步骤 :

2.1 注册RestTemplate

在启动类中注入RestTemplate

复制代码
  @MapperScan("cn.itcast.order.mapper")
  @SpringBootApplication
  public class OrderApplication {
  ​
      public static void main(String[] args) {
          SpringApplication.run(OrderApplication.class, args);
      }
  ​
      // 创建RestTemplate对象并注入容器
      @Bean
      public RestTemplate restTemplate(){
          return new RestTemplate();
      }
  ​
  }

2.2 在订单业务层 利用RestTemplate发出http请求

复制代码
  
  @RestController
  @RequestMapping("order")
  public class OrderController {
  ​
     @Autowired
     private OrderService orderService;
  ​
     @Autowired
     private RestTemplate restTemplate;
  ​
      @GetMapping("{orderId}")
      public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
          // 根据id查询订单并返回
          Order order = orderService.queryOrderById(orderId);
          // 利用RestTemplate发http请求
          // url路径
          String url = "http://localhost:8081/user/" + order.getUserId();
          // 发送请求
          User user = restTemplate.getForObject(url, User.class);
          // 封装user
          order.setUser(user);
          return order;
      }
  }

3. Eureka

3.1 提供者和消费者

提供者 : 一次业务中,被其它微服务调用的业务(提供接口给其它微服务)

消费者 : 一次业务中, 调用其他微服务(调用其它微服务的接口)

3.2 Eureka原理分析

3.2.1 Eureka注册中心
3.2.2搭建Eureka注册中心
复制代码
  
          <!--eureka服务端-->
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
          </dependency>

新建eureka-server模块

启动类:

复制代码
  
  ​
  @SpringBootApplication
  @EnableEurekaServer
  public class EurekaApplication {
      public static void main(String[] args) {
          SpringApplication.run(EurekaApplication.class,args);
      }
  }

配置文件 :

复制代码
  
  server:
    port: 10086
  spring:
    application:
      name:eurekaserver   # 服务名称
  eureka:
    client:
      service-url:
        defaultZone: http://localhost:10086/eureka   # 地址信息

服务注册:

把user-service注册到EurekaServer下:

引入依赖

配置user-service的yml文件

复制代码
  
  server:
    port: 8081
  spring:
    datasource:
      url: jdbc:mysql://localhost:3306/cloud_user?useSSL=false
      username: root
      password: 888888
      driver-class-name: com.mysql.jdbc.Driver
    application:
      name: userservice  # user服务名称
  mybatis:
    type-aliases-package: cn.itcast.user.pojo
    configuration:
      map-underscore-to-camel-case: true
  logging:
    level:
      cn.itcast: debug
    pattern:
      dateformat: MM-dd HH:mm:ss:SSS
  ​
  eureka:
    client:
      service-url:
        defaultZone: http://localhost:10086/eureka   # 地址 信息

服务列表 :

3.2.3 在order-service完成服务拉取

原来的代码:

复制代码
  
  String url = "http://localhost:8081/user/" + order.getUserId();

修改后 :

复制代码
  
  String url = "http://userservice/user/" + order.getUserId();

加上负载均衡注解

复制代码
  
      @Bean
      @LoadBalanced    // 标记要被Ribbon拦截处理!
      public RestTemplate restTemplate(){
          return new RestTemplate();
      }
相关推荐
hdsoft_huge6 小时前
SpringBoot 与 JPA 整合全解析:架构优势、应用场景、集成指南与最佳实践
java·spring boot·架构
DoraBigHead9 小时前
你写前端按钮,他们扛服务器压力:搞懂后端那些“黑话”!
前端·javascript·架构
isNotNullX10 小时前
数据中台架构解析:湖仓一体的实战设计
java·大数据·数据库·架构·spark
Kookoos11 小时前
ABP VNext + .NET Minimal API:极简微服务快速开发
后端·微服务·架构·.net·abp vnext
码字的字节11 小时前
深入理解Transformer架构:从理论到实践
深度学习·架构·transformer
wzj_what_why_how11 小时前
Android网络层架构:统一错误处理的问题分析到解决方案与设计实现
android·架构
bug攻城狮11 小时前
Alloy VS Promtail:基于 Loki 的日志采集架构对比与选型指南
运维·架构·grafana·数据可视化
代码改变世界ctw11 小时前
ARM汇编编程(AArch64架构)课程 - 第5章函数调用规范
汇编·arm开发·架构
求知摆渡13 小时前
共享代码不是共享风险——公共库解耦的三种进化路径
java·后端·架构