SpringCloud--服务注册中心--Eureka支付模块集群配置

  1. 支付模块微服务集群模式搭建;
    1. package com.model.controller;
      
      import com.model.pojo.CommonResult;
      import com.model.pojo.Payment;
      import com.model.service.PaymentService;
      import lombok.extern.slf4j.Slf4j;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.web.bind.annotation.*;
      
      import javax.annotation.Resource;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/9/5 23:28
       */
      @RestController
      @Slf4j
      @RequestMapping("/payment")
      public class PaymentController {
          @Resource
          private PaymentService paymentService;
      
          @Value("${server.port}")
          private String serverPort;
          @PostMapping("/create")
      //    @RequestBody : Post请求时接受json格式的参数
          public CommonResult create(@RequestBody Payment payment){
              Integer result = paymentService.create(payment);
              log.info("插入结果:"+result);
              if (result>0){
                  return new CommonResult(200,"插入成功,服务端口号"+serverPort,result);
              }
              return new CommonResult(444,"插入失败",null);
          }
      
          @GetMapping("/getById/{id}")
          public CommonResult getPaymentById(@PathVariable("id")Long id){
              Payment payment = paymentService.getPaymentById(id);
              log.info("查询结果:"+payment.toString());
              if (payment!=null){
                  return new CommonResult(200,"查询成功,服务端口号:"+serverPort,payment);
              }
              return new CommonResult(444,"查询失败",null);
          }
      }
  2. 测试

    1. 问题我们访问的服务只能是8001端口,无法实现负载均衡
  3. 解决BUG,实现负载均衡 ,将写死的访问地址,改为注册中心上的服务名

  4. 测试

    1.  访问同一个服务地址,可以有不同的服务模块提供服务,实现了负载均衡

    2.  

       

posted @ 2021-09-07 00:17  张紫韩  阅读(27)  评论(0编辑  收藏  举报