增加 spring cloud 用例
This commit is contained in:
@ -22,7 +22,7 @@
|
||||
</dependency>
|
||||
<!--引入对公共模块的依赖-->
|
||||
<dependency>
|
||||
<groupId>com.heibaiying.eureka</groupId>
|
||||
<groupId>com.heibaiying.ribbon</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
|
@ -4,9 +4,7 @@ package com.heibaiying.producer.controller;
|
||||
import com.heibaiying.common.api.IProductService;
|
||||
import com.heibaiying.common.bean.Product;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -19,13 +17,18 @@ public class ProducerController {
|
||||
@Autowired
|
||||
private IProductService productService;
|
||||
|
||||
@RequestMapping("products")
|
||||
@GetMapping("products")
|
||||
public List<Product> productList() {
|
||||
return productService.queryAllProducts();
|
||||
}
|
||||
|
||||
@RequestMapping("product/{id}")
|
||||
@GetMapping("product/{id}")
|
||||
public Product productDetail(@PathVariable int id) {
|
||||
return productService.queryProductById(id);
|
||||
}
|
||||
|
||||
@PostMapping("product")
|
||||
public void save(@RequestBody Product product) {
|
||||
productService.saveProduct(product);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package com.heibaiying.producer.service;
|
||||
|
||||
import com.heibaiying.common.api.IProductService;
|
||||
import com.heibaiying.common.bean.Product;
|
||||
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -13,16 +15,10 @@ import java.util.List;
|
||||
* @description : 产品提供接口实现类
|
||||
*/
|
||||
@Service
|
||||
public class ProductService implements IProductService {
|
||||
public class ProductService implements IProductService, ApplicationListener<WebServerInitializedEvent> {
|
||||
|
||||
private static List<Product> productList = new ArrayList<>();
|
||||
|
||||
static {
|
||||
for (int i = 0; i < 20; i++) {
|
||||
productList.add(new Product(i, "产品" + i, i / 2 == 0, new Date(), 66.66f * i));
|
||||
}
|
||||
}
|
||||
|
||||
public Product queryProductById(int id) {
|
||||
for (Product product : productList) {
|
||||
if (product.getId() == id) {
|
||||
@ -36,4 +32,17 @@ public class ProductService implements IProductService {
|
||||
public List<Product> queryAllProducts() {
|
||||
return productList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveProduct(Product product) {
|
||||
productList.add(product);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(WebServerInitializedEvent event) {
|
||||
int port = event.getWebServer().getPort();
|
||||
for (long i = 0; i < 20; i++) {
|
||||
productList.add(new Product(i, port + "产品" + i, i / 2 == 0, new Date(), 66.66f * i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user