1 微服务调用
订单微服务是服务消费者;商品微服务是服务提供者
1.1
完善商品查询相关代码
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
运行product模块
通过JPA根据Java代码自动生成数据库表
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');
1.3
通过浏览器访问服务
http://localhost:8081/product/1
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微服务
通过浏览器访问服务
http://localhost:8091/order/prod/1
标签:SCAli3,org,yppah,springframework,调用,HM,import,com,public From: https://www.cnblogs.com/yppah/p/16799957.html