mirror of
https://gitee.com/y_project/RuoYi-Cloud.git
synced 2026-01-29 21:11:57 +08:00
添加测试模块
This commit is contained in:
42
xuexi-parent/cloud-order/pom.xml
Normal file
42
xuexi-parent/cloud-order/pom.xml
Normal file
@@ -0,0 +1,42 @@
|
||||
<?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>
|
||||
<artifactId>xuexi-parent</artifactId>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<version>2.5.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>cloud-order</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</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-openfeign</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>cloud-common</artifactId>
|
||||
<version>2.5.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.ybk;
|
||||
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient //开启ribbon
|
||||
@EnableFeignClients //开启Feign
|
||||
public class OrderApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OrderApplication.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.ybk.controller;
|
||||
|
||||
import com.ybk.domain.Product;
|
||||
import com.ybk.service.ProductService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class OrderController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
@Autowired
|
||||
private DiscoveryClient discoveryClient;
|
||||
@Autowired
|
||||
private ProductService productService;
|
||||
|
||||
@RequestMapping(value = "/product/{pid}")
|
||||
public void order(@PathVariable("pid") Integer pid) {
|
||||
System.out.println("调用下单服务成功---" + pid);
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances("servier-product");
|
||||
// ServiceInstance serviceInstance = instances.get(0);
|
||||
// Product forObject = restTemplate.getForObject("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/" + pid, Product.class);
|
||||
// System.out.println("查询到商品信息---" + forObject);
|
||||
|
||||
//ribbon实现负载均衡
|
||||
// Product forObject1 = restTemplate.getForObject("http://servier-product/product/" + pid, Product.class);
|
||||
//System.out.println("查询到商品信息11---" + forObject1);
|
||||
|
||||
Product product = productService.findProduct(pid);
|
||||
System.out.println("查询到商品信息11---" + product);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.ybk.dao;
|
||||
|
||||
import com.ybk.domain.Order;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface OrderDao extends JpaRepository<Order, Integer> {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.ybk.service;
|
||||
|
||||
public interface OrderService {
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ybk.service;
|
||||
|
||||
import com.ybk.domain.Product;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@FeignClient(value = "servier-product",
|
||||
fallback = ProductServiceFallback.class)
|
||||
public interface ProductService {
|
||||
|
||||
@RequestMapping("/product/{pid}")
|
||||
Product findProduct(@PathVariable("pid") Integer pid);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.ybk.service;
|
||||
|
||||
import com.ybk.domain.Product;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
*容错类
|
||||
*/
|
||||
@Service
|
||||
public class ProductServiceFallback implements ProductService{
|
||||
|
||||
@Override
|
||||
public Product findProduct(Integer pid) {
|
||||
//容错逻辑
|
||||
Product product = new Product();
|
||||
return product;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.ybk.service.impl;
|
||||
|
||||
import com.ybk.service.OrderService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
}
|
||||
24
xuexi-parent/cloud-order/src/main/resources/application.yml
Normal file
24
xuexi-parent/cloud-order/src/main/resources/application.yml
Normal file
@@ -0,0 +1,24 @@
|
||||
server:
|
||||
port: 8091
|
||||
spring:
|
||||
application:
|
||||
name: servier-order
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://192.168.50.129:3306/xuexi-parent?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true
|
||||
username: root
|
||||
password: root
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: update
|
||||
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 192.168.50.129:8848/
|
||||
servier-product:
|
||||
ribbon:
|
||||
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
|
||||
feign:
|
||||
sentinel:
|
||||
enabled: true
|
||||
Reference in New Issue
Block a user