服务创建 测试

main
夜灬瞬 2 years ago
parent 86573a8023
commit 4233d106c1

51
.gitignore vendored

@ -1,26 +1,33 @@
# ---> Java HELP.md
# Compiled class file target/
*.class !.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
# Log file ### STS ###
*.log .apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
# BlueJ files ### IntelliJ IDEA ###
*.ctxt .idea
*.iws
*.iml
*.ipr
# Mobile Tools for Java (J2ME) ### NetBeans ###
.mtj.tmp/ /nbproject/private/
/nbbuild/
# Package Files # /dist/
*.jar /nbdist/
*.war /.nb-gradle/
*.nar build/
*.ear !**/src/main/**/build/
*.zip !**/src/test/**/build/
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
### VS Code ###
.vscode/

@ -0,0 +1,20 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.shun</groupId>
<artifactId>a-rabbitMQ</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>business</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

@ -0,0 +1,19 @@
package com.shun.business;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author
* @since 2023/6/17 15:32
*/
@SpringBootApplication
@EnableDiscoveryClient
public class BusinessApplication {
public static void main(String[] args) {
SpringApplication.run(BusinessApplication.class,args);
}
}

@ -0,0 +1,23 @@
package com.shun.business.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:57
*/
@RestController
public class BusinessController {
/**
*
* @throws InterruptedException
*/
@GetMapping("/notify")
public void notifyBusiness() throws InterruptedException {
Thread.sleep(400);
System.out.println("通知商家成功!!");
}
}

@ -0,0 +1,9 @@
server:
port: 1
spring:
application:
name: business
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

@ -0,0 +1,21 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.shun</groupId>
<artifactId>a-rabbitMQ</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>coupon</artifactId>
<description>优惠券</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

@ -0,0 +1,18 @@
package com.shun.coupon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author
* @since 2023/6/19 15:51
*/
@SpringBootApplication
@EnableDiscoveryClient
public class CouponApplication {
public static void main(String[] args) {
SpringApplication.run(CouponApplication.class,args);
}
}

@ -0,0 +1,22 @@
package com.shun.coupon.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:55
*/
@RestController
public class CouponController {
/**
*
* @throws InterruptedException
*/
@GetMapping("/coupon")
public void coupon() throws InterruptedException {
Thread.sleep(400);
System.out.println("优惠券预扣除成功!");
}
}

@ -0,0 +1,9 @@
server:
port: 2
spring:
application:
name: coupon
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

@ -0,0 +1,21 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.shun</groupId>
<artifactId>a-rabbitMQ</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>integral</artifactId>
<description>积分</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

@ -0,0 +1,17 @@
package com.shun.integral;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author
* @since 2023/6/19 15:50
*/
@SpringBootApplication
@EnableDiscoveryClient
public class IntegralApplication {
public static void main(String[] args) {
SpringApplication.run(IntegralApplication.class,args);
}
}

@ -0,0 +1,22 @@
package com.shun.integral.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:56
*/
@RestController
public class IntegralController {
/**
*
* @throws InterruptedException
*/
@GetMapping("/up")
public void up() throws InterruptedException {
Thread.sleep(400);
System.out.println("扣除用户积分成功!!");
}
}

@ -0,0 +1,9 @@
server:
port: 3
spring:
application:
name: integral
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

@ -0,0 +1,21 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.shun</groupId>
<artifactId>a-rabbitMQ</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>order</artifactId>
<description>订单</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

@ -0,0 +1,18 @@
package com.shun.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author
* @since 2023/6/19 15:49
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}

@ -0,0 +1,23 @@
package com.shun.order.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:54
*/
@RestController
public class OrderController {
/**
*
* @throws InterruptedException
*/
@GetMapping("create")
public void create() throws InterruptedException {
Thread.sleep(400);
System.out.println("创建订单成功!");
}
}

@ -0,0 +1,9 @@
server:
port: 4
spring:
application:
name: order
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

@ -0,0 +1,27 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.shun</groupId>
<artifactId>a-rabbitMQ</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>placeOrder</artifactId>
<description>下单</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,20 @@
package com.shun.placeOrder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author
* @since 2023/6/17 16:16
*/
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class PlaceOrderApplication {
public static void main(String[] args) {
SpringApplication.run(PlaceOrderApplication.class,args);
}
}

@ -0,0 +1,20 @@
package com.shun.placeOrder.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:57
*/
@FeignClient("business")
public interface BusinessClient {
/**
*
*/
@GetMapping("/notify")
public void notifyBusiness();
}

@ -0,0 +1,19 @@
package com.shun.placeOrder.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:55
*/
@FeignClient("coupon")
public interface CouponClient {
/**
*
*/
@GetMapping("/coupon")
public void coupon();
}

@ -0,0 +1,19 @@
package com.shun.placeOrder.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:56
*/
@FeignClient("integral")
public interface IntegralClient {
/**
*
*/
@GetMapping("/up")
public void up() ;
}

@ -0,0 +1,20 @@
package com.shun.placeOrder.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:54
*/
@FeignClient("order")
public interface OrderClient {
/**
*
*/
@GetMapping("create")
public void create();
}

@ -0,0 +1,20 @@
package com.shun.placeOrder.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:52
*/
@FeignClient("stock")
public interface StockClient {
/**
*
*/
@GetMapping("/delStock")
public void delStock();
}

@ -0,0 +1,44 @@
package com.shun.placeOrder.contoller;
import com.shun.placeOrder.client.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:46
*/
@RestController
public class PlaceOrderController {
@Resource
BusinessClient businessClient;
@Resource
CouponClient couponClient;
@Resource
IntegralClient integralClient;
@Resource
OrderClient orderClient;
@Resource
StockClient stockClient;
@GetMapping("po")
public String po(){
long start = System.currentTimeMillis();
// 调用库存服务扣减商品库存
stockClient.delStock();
// 调用订单服务 创建订单
orderClient.create();
// 调用优惠券服务,预扣除优惠券
couponClient.coupon();
// 调用用户积分服务,预扣除用户积分
integralClient.up();
// 调用商家服务,通知商家用户已下单
businessClient.notifyBusiness();
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start));
return "PlaceOrderController finish";
}
}

@ -0,0 +1,9 @@
server:
port: 5
spring:
application:
name: placeOrder
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

@ -0,0 +1,110 @@
<?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>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shun</groupId>
<artifactId>a-rabbitMQ</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>a-rabbitMQ</name>
<description>a-rabbitMQ</description>
<modules>
<module>placeOrder</module>
<module>stock</module>
<module>order</module>
<module>coupon</module>
<module>integral</module>
<module>business</module>
</modules>
<properties>
<java.version>17</java.version>
<spring-cloud-alibaba.version>2022.0.0.0-RC2</spring-cloud-alibaba.version>
<spring-cloud.version>2022.0.2</spring-cloud.version>
</properties>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-amqp</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- spring cloud alibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud -->
<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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,21 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.shun</groupId>
<artifactId>a-rabbitMQ</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>stock</artifactId>
<description>库存</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

@ -0,0 +1,18 @@
package com.shun.stock;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author
* @since 2023/6/19 15:50
*/
@SpringBootApplication
@EnableDiscoveryClient
public class StockApplication {
public static void main(String[] args) {
SpringApplication.run(StockApplication.class,args);
}
}

@ -0,0 +1,31 @@
package com.shun.stock.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @since 2023/6/19 15:52
*/
@RestController
public class StockController {
/**
*
*/
private static Integer stock = 10;
/**
*
* @throws InterruptedException
*/
@GetMapping("/delStock")
public void delStock() throws InterruptedException {
Thread.sleep(400);
stock--;
if(stock < 0){
throw new RuntimeException("商品库存不足!");
}
System.out.println("扣减库存成功!");
}
}

@ -0,0 +1,9 @@
server:
port: 6
spring:
application:
name: stock
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

@ -0,0 +1 @@
GET http://localhost:5/po

@ -0,0 +1 @@
RabbitMQ 模拟电商
Loading…
Cancel
Save