增加 spring cloud 用例
This commit is contained in:
		@@ -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.stereotype.Controller;
 | 
			
		||||
import org.springframework.ui.Model;
 | 
			
		||||
import org.springframework.web.bind.annotation.PathVariable;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -21,17 +21,26 @@ public class SellController {
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private CProductFeign cproductFeign;
 | 
			
		||||
 | 
			
		||||
    @RequestMapping
 | 
			
		||||
    @GetMapping("products")
 | 
			
		||||
    public String productList(Model model) {
 | 
			
		||||
        List<Product> products = cproductFeign.productList();
 | 
			
		||||
        model.addAttribute("products", products);
 | 
			
		||||
        return "products";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @RequestMapping("product/{id}")
 | 
			
		||||
    @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";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,14 @@
 | 
			
		||||
package com.heibaiying.consumer.feign;
 | 
			
		||||
 | 
			
		||||
import com.heibaiying.common.feign.ProductFeign;
 | 
			
		||||
import com.heibaiying.consumer.config.FeignConfig;
 | 
			
		||||
import org.springframework.cloud.openfeign.FeignClient;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author : heibaiying
 | 
			
		||||
 * @description : 声明式接口调用
 | 
			
		||||
 */
 | 
			
		||||
@FeignClient("producer")
 | 
			
		||||
@FeignClient(value = "producer",configuration = FeignConfig.class)
 | 
			
		||||
public interface CProductFeign extends ProductFeign {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
server:
 | 
			
		||||
  port: 8030
 | 
			
		||||
  port: 8080
 | 
			
		||||
# 指定服务命名
 | 
			
		||||
spring:
 | 
			
		||||
  application:
 | 
			
		||||
 
 | 
			
		||||
@@ -5,11 +5,15 @@
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
<h3>产品列表:点击查看详情</h3>
 | 
			
		||||
<form action="/sell/product" method="post">
 | 
			
		||||
    <input type="text" name="productName">
 | 
			
		||||
    <input type="submit" value="新增产品">
 | 
			
		||||
</form>
 | 
			
		||||
<ul>
 | 
			
		||||
    <#list products as product>
 | 
			
		||||
       <li>
 | 
			
		||||
           <a href="sell/product/${product.id}">${product.name}</a>
 | 
			
		||||
       </li>
 | 
			
		||||
        <li>
 | 
			
		||||
            <a href="/sell/product/${product.id}">${product.name}</a>
 | 
			
		||||
        </li>
 | 
			
		||||
    </#list>
 | 
			
		||||
</ul>
 | 
			
		||||
</body>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user