当前位置:   article > 正文

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

山东大学软件学院项目实训-创新实训-基于大模型的旅游平台(二十一)- 微服务(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();
      }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/677646
推荐阅读
相关标签
  

闽ICP备14008679号