增加 spring cloud 用例
This commit is contained in:
parent
0004596d6f
commit
78f9e43988
BIN
pictures/spring-cloud-ribbon-app.png
Normal file
BIN
pictures/spring-cloud-ribbon-app.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
BIN
pictures/spring-cloud-ribbon-eureka.png
Normal file
BIN
pictures/spring-cloud-ribbon-eureka.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 68 KiB |
BIN
pictures/spring-cloud-ribbon-product.png
Normal file
BIN
pictures/spring-cloud-ribbon-product.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
pictures/spring-cloud-ribbon-products-8020.png
Normal file
BIN
pictures/spring-cloud-ribbon-products-8020.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 37 KiB |
BIN
pictures/spring-cloud-ribbon-products-8030.png
Normal file
BIN
pictures/spring-cloud-ribbon-products-8030.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 37 KiB |
@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.heibaiying</groupId>
|
<groupId>com.heibaiying.eureka.cluster</groupId>
|
||||||
<artifactId>eureka-cluster</artifactId>
|
<artifactId>eureka-cluster</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.heibaiying</groupId>
|
<groupId>com.heibaiying.eureka.cluster</groupId>
|
||||||
<artifactId>eureka-cluster</artifactId>
|
<artifactId>eureka-cluster</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>com.heibaiying</groupId>
|
<groupId>com.heibaiying.eureka.cluster</groupId>
|
||||||
<artifactId>eureka-cluster</artifactId>
|
<artifactId>eureka-cluster</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>eureka-cluster</name>
|
<name>eureka-cluster</name>
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.heibaiying</groupId>
|
<groupId>com.heibaiying.eureka</groupId>
|
||||||
<artifactId>eureka</artifactId>
|
<artifactId>eureka</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.heibaiying</groupId>
|
<groupId>com.heibaiying.eureka</groupId>
|
||||||
<artifactId>eureka</artifactId>
|
<artifactId>eureka</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>com.heibaiying</groupId>
|
<groupId>com.heibaiying.eureka</groupId>
|
||||||
<artifactId>eureka</artifactId>
|
<artifactId>eureka</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>eureka</name>
|
<name>eureka</name>
|
||||||
|
@ -17,7 +17,7 @@ import java.util.Date;
|
|||||||
public class Product implements Serializable {
|
public class Product implements Serializable {
|
||||||
|
|
||||||
// 产品序列号
|
// 产品序列号
|
||||||
private int id;
|
private long id;
|
||||||
|
|
||||||
// 产品名称
|
// 产品名称
|
||||||
private String name;
|
private String name;
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package com.heibaiying.common.feign;
|
package com.heibaiying.common.feign;
|
||||||
|
|
||||||
import com.heibaiying.common.bean.Product;
|
import com.heibaiying.common.bean.Product;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -12,13 +11,17 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface ProductFeign {
|
public interface ProductFeign {
|
||||||
|
|
||||||
@RequestMapping("products")
|
@GetMapping("products")
|
||||||
List<Product> productList();
|
List<Product> productList();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 这是需要强调的是使用feign时候@PathVariable一定要用value指明参数,
|
* 这是需要强调的是使用feign时候@PathVariable一定要用value指明参数,
|
||||||
* 不然会抛出.IllegalStateException: PathVariable annotation was empty on param 异常
|
* 不然会抛出.IllegalStateException: PathVariable annotation was empty on param 异常
|
||||||
*/
|
*/
|
||||||
@RequestMapping("product/{id}")
|
@GetMapping("product/{id}")
|
||||||
Product productDetail(@PathVariable(value = "id") int id);
|
Product productDetail(@PathVariable(value = "id") int id);
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("product")
|
||||||
|
void save(@RequestBody Product product);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.heibaiying.consumer.config;
|
||||||
|
|
||||||
|
import feign.Retryer;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : feign 配置
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class FeignConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Retryer retryer(){
|
||||||
|
//重试间隔为 100ms,最大重试时间为 1s, 重试次数为 5 次
|
||||||
|
return new Retryer.Default(100,SECONDS.toMillis(1),5);
|
||||||
|
}
|
||||||
|
}
|
@ -6,9 +6,9 @@ import com.heibaiying.consumer.feign.CProductFeign;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,17 +21,26 @@ public class SellController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CProductFeign cproductFeign;
|
private CProductFeign cproductFeign;
|
||||||
|
|
||||||
@RequestMapping
|
@GetMapping("products")
|
||||||
public String productList(Model model) {
|
public String productList(Model model) {
|
||||||
List<Product> products = cproductFeign.productList();
|
List<Product> products = cproductFeign.productList();
|
||||||
model.addAttribute("products", products);
|
model.addAttribute("products", products);
|
||||||
return "products";
|
return "products";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("product/{id}")
|
@GetMapping("product/{id}")
|
||||||
public String productDetail(@PathVariable int id, Model model) {
|
public String productDetail(@PathVariable int id, Model model) {
|
||||||
Product product = cproductFeign.productDetail(id);
|
Product product = cproductFeign.productDetail(id);
|
||||||
model.addAttribute("product", product);
|
model.addAttribute("product", product);
|
||||||
return "product";
|
return "product";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("product")
|
||||||
|
public String save(@RequestParam String productName) {
|
||||||
|
long id = Math.round(Math.random() * 100);
|
||||||
|
Product product = new Product(id, productName, false, new Date(), 88);
|
||||||
|
cproductFeign.save(product);
|
||||||
|
return "redirect:products";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package com.heibaiying.consumer.feign;
|
package com.heibaiying.consumer.feign;
|
||||||
|
|
||||||
import com.heibaiying.common.feign.ProductFeign;
|
import com.heibaiying.common.feign.ProductFeign;
|
||||||
|
import com.heibaiying.consumer.config.FeignConfig;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : heibaiying
|
* @author : heibaiying
|
||||||
* @description : 声明式接口调用
|
* @description : 声明式接口调用
|
||||||
*/
|
*/
|
||||||
@FeignClient("producer")
|
@FeignClient(value = "producer",configuration = FeignConfig.class)
|
||||||
public interface CProductFeign extends ProductFeign {
|
public interface CProductFeign extends ProductFeign {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 8030
|
port: 8080
|
||||||
# 指定服务命名
|
# 指定服务命名
|
||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
|
@ -5,10 +5,14 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h3>产品列表:点击查看详情</h3>
|
<h3>产品列表:点击查看详情</h3>
|
||||||
|
<form action="/sell/product" method="post">
|
||||||
|
<input type="text" name="productName">
|
||||||
|
<input type="submit" value="新增产品">
|
||||||
|
</form>
|
||||||
<ul>
|
<ul>
|
||||||
<#list products as product>
|
<#list products as product>
|
||||||
<li>
|
<li>
|
||||||
<a href="sell/product/${product.id}">${product.name}</a>
|
<a href="/sell/product/${product.id}">${product.name}</a>
|
||||||
</li>
|
</li>
|
||||||
</#list>
|
</#list>
|
||||||
</ul>
|
</ul>
|
||||||
|
@ -5,9 +5,7 @@ import com.heibaiying.common.bean.Product;
|
|||||||
import com.heibaiying.common.feign.ProductFeign;
|
import com.heibaiying.common.feign.ProductFeign;
|
||||||
import com.heibaiying.producer.service.IProductService;
|
import com.heibaiying.producer.service.IProductService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -15,18 +13,23 @@ import java.util.List;
|
|||||||
* @author : heibaiying
|
* @author : heibaiying
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class ProducerController{
|
public class ProducerController implements ProductFeign {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IProductService productService;
|
private IProductService productService;
|
||||||
|
|
||||||
@RequestMapping("products")
|
@GetMapping("products")
|
||||||
public List<Product> productList() {
|
public List<Product> productList() {
|
||||||
return productService.queryAllProducts();
|
return productService.queryAllProducts();
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("product/{id}")
|
@GetMapping("product/{id}")
|
||||||
public Product productDetail(@PathVariable int id) {
|
public Product productDetail(@PathVariable int id) {
|
||||||
return productService.queryProductById(id);
|
return productService.queryProductById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("product")
|
||||||
|
public void save(@RequestBody Product product) {
|
||||||
|
productService.saveProduct(product);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,4 +8,6 @@ public interface IProductService {
|
|||||||
Product queryProductById(int id) ;
|
Product queryProductById(int id) ;
|
||||||
|
|
||||||
List<Product> queryAllProducts();
|
List<Product> queryAllProducts();
|
||||||
|
|
||||||
|
void saveProduct(Product product);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package com.heibaiying.producer.service.impl;
|
|||||||
|
|
||||||
import com.heibaiying.common.bean.Product;
|
import com.heibaiying.common.bean.Product;
|
||||||
import com.heibaiying.producer.service.IProductService;
|
import com.heibaiying.producer.service.IProductService;
|
||||||
|
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -13,16 +15,10 @@ import java.util.List;
|
|||||||
* @description : 产品提供接口实现类
|
* @description : 产品提供接口实现类
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class ProductService implements IProductService {
|
public class ProductService implements IProductService, ApplicationListener<WebServerInitializedEvent> {
|
||||||
|
|
||||||
private static List<Product> productList = new ArrayList<>();
|
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) {
|
public Product queryProductById(int id) {
|
||||||
for (Product product : productList) {
|
for (Product product : productList) {
|
||||||
if (product.getId() == id) {
|
if (product.getId() == id) {
|
||||||
@ -36,4 +32,17 @@ public class ProductService implements IProductService {
|
|||||||
public List<Product> queryAllProducts() {
|
public List<Product> queryAllProducts() {
|
||||||
return productList;
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.heibaiying</groupId>
|
<groupId>com.heibaiying.ribbon</groupId>
|
||||||
<artifactId>spring-cloud-ribbon</artifactId>
|
<artifactId>spring-cloud-ribbon</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
@ -13,4 +13,6 @@ public interface IProductService {
|
|||||||
Product queryProductById(int id);
|
Product queryProductById(int id);
|
||||||
|
|
||||||
List<Product> queryAllProducts();
|
List<Product> queryAllProducts();
|
||||||
|
|
||||||
|
void saveProduct(Product product);
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ import java.util.Date;
|
|||||||
public class Product implements Serializable {
|
public class Product implements Serializable {
|
||||||
|
|
||||||
// 产品序列号
|
// 产品序列号
|
||||||
private int id;
|
private long id;
|
||||||
|
|
||||||
// 产品名称
|
// 产品名称
|
||||||
private String name;
|
private String name;
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.heibaiying</groupId>
|
<groupId>com.heibaiying.ribbon</groupId>
|
||||||
<artifactId>spring-cloud-ribbon</artifactId>
|
<artifactId>spring-cloud-ribbon</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
@ -31,7 +31,7 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<!--引入对公共模块的依赖-->
|
<!--引入对公共模块的依赖-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.heibaiying.eureka</groupId>
|
<groupId>com.heibaiying.ribbon</groupId>
|
||||||
<artifactId>common</artifactId>
|
<artifactId>common</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
|
@ -6,9 +6,9 @@ import com.heibaiying.common.bean.Product;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,17 +21,25 @@ public class SellController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IProductService productService;
|
private IProductService productService;
|
||||||
|
|
||||||
@RequestMapping
|
@GetMapping("products")
|
||||||
public String productList(Model model) {
|
public String productList(Model model) {
|
||||||
List<Product> products = productService.queryAllProducts();
|
List<Product> products = productService.queryAllProducts();
|
||||||
model.addAttribute("products", products);
|
model.addAttribute("products", products);
|
||||||
return "products";
|
return "products";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("product/{id}")
|
@GetMapping("product/{id}")
|
||||||
public String productDetail(@PathVariable int id, Model model) {
|
public String productDetail(@PathVariable int id, Model model) {
|
||||||
Product product = productService.queryProductById(id);
|
Product product = productService.queryProductById(id);
|
||||||
model.addAttribute("product", product);
|
model.addAttribute("product", product);
|
||||||
return "product";
|
return "product";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("product")
|
||||||
|
public String save(@RequestParam String productName) {
|
||||||
|
long id = Math.round(Math.random() * 100);
|
||||||
|
Product product = new Product(id, productName, false, new Date(), 88);
|
||||||
|
productService.saveProduct(product);
|
||||||
|
return "redirect:products";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,4 +31,7 @@ public class ProductService implements IProductService {
|
|||||||
return productList;
|
return productList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void saveProduct(Product product) {
|
||||||
|
restTemplate.postForObject("http://producer/product", product, Void.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 8030
|
port: 8080
|
||||||
# 指定服务命名
|
# 指定服务命名
|
||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
|
@ -5,10 +5,14 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h3>产品列表:点击查看详情</h3>
|
<h3>产品列表:点击查看详情</h3>
|
||||||
|
<form action="/sell/product" method="post">
|
||||||
|
<input type="text" name="productName">
|
||||||
|
<input type="submit" value="新增产品">
|
||||||
|
</form>
|
||||||
<ul>
|
<ul>
|
||||||
<#list products as product>
|
<#list products as product>
|
||||||
<li>
|
<li>
|
||||||
<a href="sell/product/${product.id}">${product.name}</a>
|
<a href="/sell/product/${product.id}">${product.name}</a>
|
||||||
</li>
|
</li>
|
||||||
</#list>
|
</#list>
|
||||||
</ul>
|
</ul>
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.heibaiying</groupId>
|
<groupId>com.heibaiying.ribbon</groupId>
|
||||||
<artifactId>spring-cloud-ribbon</artifactId>
|
<artifactId>spring-cloud-ribbon</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>com.heibaiying</groupId>
|
<groupId>com.heibaiying.ribbon</groupId>
|
||||||
<artifactId>spring-cloud-ribbon</artifactId>
|
<artifactId>spring-cloud-ribbon</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>spring-cloud-ribbon</name>
|
<name>spring-cloud-ribbon</name>
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<!--引入对公共模块的依赖-->
|
<!--引入对公共模块的依赖-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.heibaiying.eureka</groupId>
|
<groupId>com.heibaiying.ribbon</groupId>
|
||||||
<artifactId>common</artifactId>
|
<artifactId>common</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
|
@ -4,9 +4,7 @@ package com.heibaiying.producer.controller;
|
|||||||
import com.heibaiying.common.api.IProductService;
|
import com.heibaiying.common.api.IProductService;
|
||||||
import com.heibaiying.common.bean.Product;
|
import com.heibaiying.common.bean.Product;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -19,13 +17,18 @@ public class ProducerController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IProductService productService;
|
private IProductService productService;
|
||||||
|
|
||||||
@RequestMapping("products")
|
@GetMapping("products")
|
||||||
public List<Product> productList() {
|
public List<Product> productList() {
|
||||||
return productService.queryAllProducts();
|
return productService.queryAllProducts();
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("product/{id}")
|
@GetMapping("product/{id}")
|
||||||
public Product productDetail(@PathVariable int id) {
|
public Product productDetail(@PathVariable int id) {
|
||||||
return productService.queryProductById(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.api.IProductService;
|
||||||
import com.heibaiying.common.bean.Product;
|
import com.heibaiying.common.bean.Product;
|
||||||
|
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -13,16 +15,10 @@ import java.util.List;
|
|||||||
* @description : 产品提供接口实现类
|
* @description : 产品提供接口实现类
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class ProductService implements IProductService {
|
public class ProductService implements IProductService, ApplicationListener<WebServerInitializedEvent> {
|
||||||
|
|
||||||
private static List<Product> productList = new ArrayList<>();
|
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) {
|
public Product queryProductById(int id) {
|
||||||
for (Product product : productList) {
|
for (Product product : productList) {
|
||||||
if (product.getId() == id) {
|
if (product.getId() == id) {
|
||||||
@ -36,4 +32,17 @@ public class ProductService implements IProductService {
|
|||||||
public List<Product> queryAllProducts() {
|
public List<Product> queryAllProducts() {
|
||||||
return productList;
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user