HM-SCAli2.2【微服务调用实现及测试】

1 微服务调用

image-20221015163148618

订单微服务是服务消费者;商品微服务是服务提供者

1.1

完善商品查询相关代码

image-20221017093221827

package com.yppah.dao;

import com.yppah.domain.Product;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductDao extends JpaRepository<Product, Integer> {

}

package com.yppah.service;

import com.yppah.domain.Product;

public interface ProductService {

    public Product findByPid(Integer pid);
}

package com.yppah.service.impl;

import com.yppah.dao.ProductDao;
import com.yppah.domain.Product;
import com.yppah.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Override
    public Product findByPid(Integer pid) {
        return productDao.findById(pid).get();
    }
}

package com.yppah.controller;

import com.alibaba.fastjson.JSON;
import com.yppah.domain.Product;
import com.yppah.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/product/{pid}")
    public Product product(@PathVariable("pid") Integer pid){
        Product product = productService.findByPid(pid);
        log.info("查询倒商品:" + JSON.toJSONString(product));
        return product;
    }
}

1.2

新建空的数据库hmscali_shop

image-20221017081902643

运行product模块

image-20221017093718894

通过JPA根据Java代码自动生成数据库表

image-20221017093841301

Navicat添加数据如下

INSERT INTO shop_product VALUE(NULL,'小米','1000','5000');
INSERT INTO shop_product VALUE(NULL,'华为','2000','5000');
INSERT INTO shop_product VALUE(NULL,'苹果','3000','5000');
INSERT INTO shop_product VALUE(NULL,'OPPO','4000','5000');

image-20221017094018877

image-20221017094206227

1.3

通过浏览器访问服务

http://localhost:8081/product/1

image-20221017094632163

1.4

完善订单相关代码

package com.yppah.dao;

import com.yppah.domain.Order;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OrderDao extends JpaRepository<Order, Integer> {

}

package com.yppah.service;

import com.yppah.domain.Order;

public interface OrderService {

    public void save(Order order);
}

package com.yppah.service.impl;

import com.yppah.dao.OrderDao;
import com.yppah.domain.Order;
import com.yppah.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Override
    public void save(Order order) {
        orderDao.save(order);
    }
}

package com.yppah;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class);
    }

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

package com.yppah.controller;

import com.alibaba.fastjson.JSON;
import com.yppah.domain.Order;
import com.yppah.domain.Product;
import com.yppah.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderService orderService;

    // 模拟买一件商品
    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info("客户下单,此时要调用商品微服务查询商品信息");
        // 通过restTemplate调用商品微服务
        Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product) + "<<");

        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户1");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);

        orderService.save(order);
        return order;
    }
}

1.5

依次启动product和order微服务

image-20221017171803134

通过浏览器访问服务

http://localhost:8091/order/prod/1

image-20221017171656799

image-20221017171829946

image-20221017171851531

posted @   yub4by  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示