山东大学软件学院项目实训-创新实训-基于大模型的旅游平台(二十一)- 微服务(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();
      }
相关推荐
Lee川5 小时前
深度拆解:基于面向对象思维的“就地编辑”组件全模块解析
javascript·架构
勤劳打代码5 小时前
Flutter 架构日记 — 状态管理
flutter·架构·前端框架
子兮曰11 小时前
后端字段又改了?我撸了一个 BFF 数据适配器,从此再也不怕接口“屎山”!
前端·javascript·架构
卓卓不是桌桌13 小时前
如何优雅地处理 iframe 跨域通信?这是我的开源方案
javascript·架构
Qlly13 小时前
DDD 架构为什么适合 MCP Server 开发?
人工智能·后端·架构
用户881586910911 天前
AI Agent 协作系统架构设计与实践
架构
鹏北海1 天前
Qiankun 微前端实战踩坑历程
前端·架构
货拉拉技术1 天前
货拉拉海豚平台-大模型推理加速工程化实践
人工智能·后端·架构
RoyLin2 天前
libkrun 深度解析:架构设计、模块实现与 Windows WHPX 后端
架构
CoovallyAIHub2 天前
实时视觉AI智能体框架来了!Vision Agents 狂揽7K Star,延迟低至30ms,YOLO+Gemini实时联动!
算法·架构·github