您的位置:首页 - 文章 - Spring Cloud - 正文

SpringCould-order_server

按照服务:

SpringCould-product_server 

创建 order_server 服务  只需要改变名字 其他的都一样
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sxpcwlkj</groupId> <artifactId>order_server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>order_server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>

配置文件application.yml

server: port: 8781
#指定注册中心地址
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
#服务的名称
spring: application: name: order-service

启动项目  查看Eureka 网页控制台

一、实现商品服务的接口

Order类

package com.sxpcwlkj.order_server.entity;
import java.math.BigDecimal;
public class Order { private int id; //订单号 private String codeNumber; //用户id private int memberId; //商品Id private int produceId; private String name; //商品价格 private BigDecimal pice; //库存 private int num; public Order() { } public Order(int id, String codeNumber, int memberId, int produceId, String name, BigDecimal pice, int num) { this.id = id; this.codeNumber = codeNumber; this.memberId = memberId; this.produceId = produceId; this.name = name; this.pice = pice; this.num = num; } public int getId() { return id; } public int getProduceId() { return produceId; } public void setProduceId(int produceId) { this.produceId = produceId; } public void setId(int id) { this.id = id; } public String getCodeNumber() { return codeNumber; } public void setCodeNumber(String codeNumber) { this.codeNumber = codeNumber; } public int getMemberId() { return memberId; } public void setMemberId(int memberId) { this.memberId = memberId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public BigDecimal getPice() { return pice; } public void setPice(BigDecimal pice) { this.pice = pice; } public int getNum() { return num; } public void setNum(int num) { this.num = num; }
}

OrderService类

package com.sxpcwlkj.order_server.service;
import com.sxpcwlkj.order_server.entity.Order;
import org.aspectj.weaver.ast.Or;
public interface OrderService { /** * 创建订单 * @return */ Order addOrder(int memberId ,int produceId); /** * 查询订单 * @param id * @return */ Order selectById(int id);
}

OrderServiceImpl类

package com.sxpcwlkj.order_server.service.impl;
import com.sxpcwlkj.order_server.entity.Order;
import com.sxpcwlkj.order_server.service.OrderService;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.UUID;
@Service
public class OrderServiceImpl implements OrderService { @Override public Order addOrder(int memberId ,int produceId) { Order order =new Order(); order.setCodeNumber(UUID.randomUUID().toString()); //订单号 order.setMemberId(memberId); //用户Id order.setProduceId(produceId); //商品Id // 准备从 商品服务 获取数据 :更具商品id查询商品详情 // /api/v1/product/selectById?id=1 order.setName(""); //商品名称 order.setPice(new BigDecimal(00)); //商品价格 order.setNum(0); //商品库存 return order; } @Override public Order selectById(int id) { return null; }
}

OrderController类

package com.sxpcwlkj.order_server.controller;
import com.sxpcwlkj.order_server.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/order")
public class OrderController { @Autowired private OrderService orderService; /** * 用户下单 * @param memberId 用户Id * @param produceId 商品Id * @return */ @RequestMapping("addOrder") public Object setlstOrder(int memberId ,int produceId){ return orderService.addOrder(memberId,produceId); }
}

/api/v1/order/addOrder?memberId=1&productId=1

本文原创,作者:西决,其版权均为品创网络所有。如需转载,请注明出处:https://www.sxpcwlkj.com/springcould-order_server/

发表评论