新增spring cloud用例
This commit is contained in:
50
spring-cloud/spring-cloud-zuul/consumer/pom.xml
Normal file
50
spring-cloud/spring-cloud-zuul/consumer/pom.xml
Normal file
@ -0,0 +1,50 @@
|
||||
<?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.heibaiying.zuul</groupId>
|
||||
<artifactId>spring-cloud-zuul</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>consumer</artifactId>
|
||||
|
||||
<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-freemarker</artifactId>
|
||||
</dependency>
|
||||
<!-- feign 依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<!--引入对公共模块的依赖-->
|
||||
<dependency>
|
||||
<groupId>com.heibaiying.zuul</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,18 @@
|
||||
package com.heibaiying.consumer;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
public class ConsumerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.heibaiying.consumer.controller;
|
||||
|
||||
|
||||
import com.heibaiying.common.bean.Product;
|
||||
import com.heibaiying.consumer.feign.CProductFeign;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("sell")
|
||||
public class SellController {
|
||||
|
||||
@Autowired
|
||||
private CProductFeign cproductFeign;
|
||||
|
||||
@GetMapping("products")
|
||||
public String productList(Model model) {
|
||||
List<Product> products = cproductFeign.productList();
|
||||
model.addAttribute("products", products);
|
||||
return "products";
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("product/{id}")
|
||||
public String productDetail(@PathVariable int id, Model model) {
|
||||
Product product = cproductFeign.productDetail(id);
|
||||
model.addAttribute("product", 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";
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.heibaiying.consumer.feign;
|
||||
|
||||
import com.heibaiying.common.feign.ProductFeign;
|
||||
import com.heibaiying.consumer.config.FeignConfig;
|
||||
import com.heibaiying.consumer.feign.impl.CProductFeignImpl;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 声明式接口调用
|
||||
*/
|
||||
@FeignClient(value = "producer",configuration = FeignConfig.class,fallback = CProductFeignImpl.class)
|
||||
public interface CProductFeign extends ProductFeign {
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.heibaiying.consumer.feign.impl;
|
||||
|
||||
import com.heibaiying.common.bean.Product;
|
||||
import com.heibaiying.consumer.feign.CProductFeign;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 定义发生错误时候的熔断处理。除了继承自CProductFeign,还需要用@Component声明为spring的组件
|
||||
*/
|
||||
@Component
|
||||
public class CProductFeignImpl implements CProductFeign {
|
||||
|
||||
@Override
|
||||
public List<Product> productList() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Product productDetail(int id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Product product) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
server:
|
||||
port: 8080
|
||||
# 指定服务命名
|
||||
spring:
|
||||
application:
|
||||
name: consumer
|
||||
# 指定注册中心地址
|
||||
eureka:
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://localhost:8010/eureka/
|
||||
feign:
|
||||
hystrix:
|
||||
# 如果为true,则OpenFign客户端将使用Hystrix断路器进行封装 默认为false
|
||||
enabled: true
|
@ -0,0 +1,15 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>产品详情</title>
|
||||
</head>
|
||||
<body>
|
||||
<ul>
|
||||
<li>产品名称:${product.name}</li>
|
||||
<li>产品序列号:${product.id}</li>
|
||||
<li>是否贵重品:${product.isPrecious?string('是','否')}</li>
|
||||
<li>生产日期: ${product.dateInProduced?string("yyyy-MM-dd HH:mm:ss")}</li>
|
||||
<li>产品价格:${product.price}</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,24 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>产品列表</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>产品列表:点击查看详情</h3>
|
||||
<form action="/sell/product" method="post">
|
||||
<input type="text" name="productName">
|
||||
<input type="submit" value="新增产品">
|
||||
</form>
|
||||
<ul>
|
||||
<#if (products?size>0) >
|
||||
<#list products as product>
|
||||
<li>
|
||||
<a href="/consumer/sell/product/${product.id}">${product.name}</a>
|
||||
</li>
|
||||
</#list>
|
||||
<#else>
|
||||
<h4 style="color: red">当前排队人数过多,请之后再购买!</h4>
|
||||
</#if>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.consumer;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ConsumerApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user