山东大学软件学院项目实训-创新实训-基于大模型的旅游平台(二十一)- 微服务(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();
      }
相关推荐
milanyangbo37 分钟前
“卧槽,系统又崩了!”——别慌,这也许是你看过最通俗易懂的分布式入门
分布式·后端·云原生·架构
失散1340 分钟前
分布式专题——1.1 Redis单机、主从、哨兵、集群部署
java·数据库·redis·分布式·架构
刘一说40 分钟前
Linux调试命令速查:Java/微服务必备
java·linux·微服务
刘一说43 分钟前
Spring Boot+Nacos+MySQL微服务问题排查指南
spring boot·mysql·微服务
2301_779503761 小时前
MySQL集群高可用架构---mysql高可用之组复制 (MGR)
数据库·mysql·架构
大咖分享课1 小时前
系统越拆越乱?你可能误解了微服务的本质!
微服务·云原生·架构
18538162800余+4 小时前
数字人系统源码搭建与定制化开发:从技术架构到落地实践
架构
叫我阿柒啊4 小时前
从Java全栈到云原生:一场技术深度对话
java·spring boot·docker·微服务·typescript·消息队列·vue3
SmalBox5 小时前
【URP】Unity 插入自定义RenderPass
架构
叫我阿柒啊5 小时前
从Java全栈到Vue3实战:一次真实面试的深度复盘
java·spring boot·微服务·vue3·响应式编程·前后端分离·restful api