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

  • 28
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值