首页 > 其他分享 >HM-SCAli3【微服务调用实现及测试】

HM-SCAli3【微服务调用实现及测试】

时间:2022-10-17 17:36:30浏览次数:50  
标签:SCAli3 org yppah springframework 调用 HM import com public

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

标签:SCAli3,org,yppah,springframework,调用,HM,import,com,public
From: https://www.cnblogs.com/yppah/p/16799957.html

相关文章

  • js 外部调用 嵌套函数
     在函外部直接调用函数的内部函数是不可以的,因为是向外查找的 所以不能直接内部函数  functionone(){functiontwo(){varb=30;......
  • HashMap的尾部遍历问题 (Tail Traversing)
    JDK1.7的HashMap在实现resize()时,新table[]的列表采用LIFO方式,即队头插入。这样做的目的是:避免尾部遍历。避免尾部遍历是为了避免在新列表插入数据时,遍历到队尾的位置。因......
  • 将eclipse生成的javadoc制作为CHM文档
    介绍使用EasyCHM将eclipse生成的javadoc制作为CHM文档,这样就可以像看电子书一样来预览项目内容准备工作1、下载EasyCHM软件使用eclipse生成javadoc文件 制作流程1、利用ec......
  • ConcurrentHashMap源码,看我这篇就够了
    思考:HashTable是线程安全的,为什么不推荐使用?HashTable是一个线程安全的类,它使用synchronized来锁住整张Hash表来实现线程安全,即每次锁住整张表让线程独占,相当于所有线程进......
  • ConcurrentHashMap源码,看我这篇就够了
    持续创作,加速成长!这是我参与「掘金日新计划·10月更文挑战」的第5天,点击查看活动详情思考:HashTable是线程安全的,为什么不推荐使用?HashTable是一个线程安全的类,它使用s......
  • python 调用ansible脚本并输出回显
    在运维的过程中,我们经常遇到这样的场景,在我们的python运维平台中,经常会执行一些常用的运维命令操作,这时候我们想要实现ansible的可视化批量执行,所以,实现最简便的操作就是pyt......
  • 方法的多种调用方式
    方法调用确实我们每个人都会,但是我觉得大家可能用的最多的就是那种常规的方式,这里呢,我要记录一下另外几种方式1fromoperatorimportattrgetter2fromoperator......
  • Vue事件对象如何调用
    <script>exportdefault{ data(){  return{   message:0,   put1:'www.96net.com.cn',  } }, methods:{  dianji(e){   ......
  • Potyczki Algorythmiczne 2011
    TrialRound:Tulips按题意模拟。#include<cstdio>constintN=15000;intn,ans=N,x,v[N+1];intmain(){scanf("%d",&n);while(n--){scanf("%d",&x);......
  • Mysql 系列 | 性能优化 - 函数调用
    sql性能优化,是代码编写过程中必定要考虑的内容,弄懂性能背后的逻辑,起到事半功倍的效果。今天学习几种常见的简单优化场景。条件字段中调用函数以公司的打卡信息表为例,......