新增spring cloud用例
This commit is contained in:
parent
5861cd8a4a
commit
2c862b5124
BIN
pictures/zuul-broker.png
Normal file
BIN
pictures/zuul-broker.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 31 KiB |
BIN
pictures/zuul-consumer-8030.png
Normal file
BIN
pictures/zuul-consumer-8030.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 34 KiB |
BIN
pictures/zuul-consumer-8040.png
Normal file
BIN
pictures/zuul-consumer-8040.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 33 KiB |
BIN
pictures/zuul-consumer.png
Normal file
BIN
pictures/zuul-consumer.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 34 KiB |
BIN
pictures/zuul-producer.png
Normal file
BIN
pictures/zuul-producer.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
@ -4,8 +4,8 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>spring-cloud-ribbon</artifactId>
|
||||
<groupId>com.heibaiying.feign</groupId>
|
||||
<artifactId>spring-cloud-feign</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
|
@ -27,13 +27,13 @@ public class ProductService implements IProductService, ApplicationListener<WebS
|
||||
|
||||
|
||||
public List<Product> queryAllProducts() {
|
||||
// 用于测试 hystrix 超时熔断
|
||||
/*用于测试 hystrix 超时熔断
|
||||
try {
|
||||
int i = new Random().nextInt(2500);
|
||||
Thread.sleep(i);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}*/
|
||||
return productList;
|
||||
}
|
||||
|
||||
|
11
spring-cloud/spring-cloud-zuul/README.md
Normal file
11
spring-cloud/spring-cloud-zuul/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
***************************
|
||||
APPLICATION FAILED TO START
|
||||
***************************
|
||||
|
||||
Description:
|
||||
|
||||
The bean 'counterFactory', defined in class path resource [org/springframework/cloud/netflix/zuul/ZuulServerAutoConfiguration$ZuulCounterFactoryConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/cloud/netflix/zuul/ZuulServerAutoConfiguration$ZuulMetricsConfiguration.class] and overriding is disabled.
|
||||
|
||||
Action:
|
||||
|
||||
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
|
22
spring-cloud/spring-cloud-zuul/common/pom.xml
Normal file
22
spring-cloud/spring-cloud-zuul/common/pom.xml
Normal file
@ -0,0 +1,22 @@
|
||||
<?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>common</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.common;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CommonApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CommonApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,33 @@
|
||||
package com.heibaiying.common.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 产品实体类
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Product implements Serializable {
|
||||
|
||||
// 产品序列号
|
||||
private long id;
|
||||
|
||||
// 产品名称
|
||||
private String name;
|
||||
|
||||
// 是否贵重品
|
||||
private Boolean isPrecious;
|
||||
|
||||
//生产日期
|
||||
private Date dateInProduced;
|
||||
|
||||
//产品价格
|
||||
private float price;
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.heibaiying.common.feign;
|
||||
|
||||
import com.heibaiying.common.bean.Product;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 声明式接口调用
|
||||
*/
|
||||
public interface ProductFeign {
|
||||
|
||||
@GetMapping("products")
|
||||
List<Product> productList();
|
||||
|
||||
/**
|
||||
* 这是需要强调的是使用feign时候@PathVariable一定要用value指明参数,
|
||||
* 不然会抛出.IllegalStateException: PathVariable annotation was empty on param 异常
|
||||
*/
|
||||
@GetMapping("product/{id}")
|
||||
Product productDetail(@PathVariable(value = "id") int id);
|
||||
|
||||
|
||||
@PostMapping("product")
|
||||
void save(@RequestBody Product product);
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.common;
|
||||
|
||||
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 CommonApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
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() {
|
||||
}
|
||||
|
||||
}
|
||||
|
31
spring-cloud/spring-cloud-zuul/eureka/pom.xml
Normal file
31
spring-cloud/spring-cloud-zuul/eureka/pom.xml
Normal file
@ -0,0 +1,31 @@
|
||||
<?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>eureka</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,16 @@
|
||||
package com.heibaiying.eureka;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableEurekaServer
|
||||
public class EurekaApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(EurekaApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
server:
|
||||
port: 8010
|
||||
eureka:
|
||||
instance:
|
||||
hostname: localhost
|
||||
client:
|
||||
# 设置为false,代表不向注册中心注册自己
|
||||
register-with-eureka: false
|
||||
# 注册中心主要用于维护服务,并不需要检索服务,所以设置为false
|
||||
fetch-registry: false
|
||||
serviceUrl:
|
||||
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.eureka;
|
||||
|
||||
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 EurekaApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
64
spring-cloud/spring-cloud-zuul/pom.xml
Normal file
64
spring-cloud/spring-cloud-zuul/pom.xml
Normal file
@ -0,0 +1,64 @@
|
||||
<?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>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<groupId>com.heibaiying.zuul</groupId>
|
||||
<artifactId>spring-cloud-zuul</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-cloud-zuul</name>
|
||||
<description>zuul project for Spring Boot</description>
|
||||
|
||||
<modules>
|
||||
<module>eureka</module>
|
||||
<module>common</module>
|
||||
<module>consumer</module>
|
||||
<module>producer</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
</project>
|
43
spring-cloud/spring-cloud-zuul/producer/pom.xml
Normal file
43
spring-cloud/spring-cloud-zuul/producer/pom.xml
Normal file
@ -0,0 +1,43 @@
|
||||
<?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>producer</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>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,16 @@
|
||||
package com.heibaiying.producer;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
public class ProducerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ProducerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
package com.heibaiying.producer.controller;
|
||||
|
||||
|
||||
import com.heibaiying.common.bean.Product;
|
||||
import com.heibaiying.common.feign.ProductFeign;
|
||||
import com.heibaiying.producer.service.IProductService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@RestController
|
||||
public class ProducerController implements ProductFeign {
|
||||
|
||||
@Autowired
|
||||
private IProductService productService;
|
||||
|
||||
@GetMapping("products")
|
||||
public List<Product> productList() {
|
||||
return productService.queryAllProducts();
|
||||
}
|
||||
|
||||
@GetMapping("product/{id}")
|
||||
public Product productDetail(@PathVariable int id) {
|
||||
return productService.queryProductById(id);
|
||||
}
|
||||
|
||||
@PostMapping("product")
|
||||
public void save(@RequestBody Product product) {
|
||||
productService.saveProduct(product);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.heibaiying.producer.service;
|
||||
|
||||
import com.heibaiying.common.bean.Product;
|
||||
|
||||
import java.util.List;
|
||||
public interface IProductService {
|
||||
|
||||
Product queryProductById(int id) ;
|
||||
|
||||
List<Product> queryAllProducts();
|
||||
|
||||
void saveProduct(Product product);
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.heibaiying.producer.service.impl;
|
||||
|
||||
import com.heibaiying.common.bean.Product;
|
||||
import com.heibaiying.producer.service.IProductService;
|
||||
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 产品提供接口实现类
|
||||
*/
|
||||
@Service
|
||||
public class ProductService implements IProductService, ApplicationListener<WebServerInitializedEvent> {
|
||||
|
||||
private static List<Product> productList = new ArrayList<>();
|
||||
|
||||
public Product queryProductById(int id) {
|
||||
return productList.stream().filter(p->p.getId()==id).collect(Collectors.toList()).get(0);
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
server:
|
||||
port: 8020
|
||||
# 指定服务命名
|
||||
spring:
|
||||
application:
|
||||
name: producer
|
||||
# 指定注册中心地址
|
||||
eureka:
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://localhost:8010/eureka/
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.producer;
|
||||
|
||||
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 ProducerApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
69
spring-cloud/spring-cloud-zuul/zuul/pom.xml
Normal file
69
spring-cloud/spring-cloud-zuul/zuul/pom.xml
Normal file
@ -0,0 +1,69 @@
|
||||
<?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>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.8.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<artifactId>zuul</artifactId>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!--eureka-client-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
<!--zuul-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
</project>
|
@ -0,0 +1,18 @@
|
||||
package com.heibaiying.zuul;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableZuulProxy
|
||||
@EnableDiscoveryClient
|
||||
public class ZuulApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ZuulApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,81 @@
|
||||
package com.heibaiying.zuul.config;
|
||||
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : zuul 的熔断器
|
||||
*/
|
||||
@Component
|
||||
public class CustomZuulFallbackProvider implements FallbackProvider {
|
||||
|
||||
/*
|
||||
* 定义熔断将用于哪些路由的服务
|
||||
*/
|
||||
@Override
|
||||
public String getRoute() {
|
||||
return "consumer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
|
||||
return new ClientHttpResponse() {
|
||||
|
||||
/**
|
||||
* 返回响应的HTTP状态代码
|
||||
*/
|
||||
@Override
|
||||
public HttpStatus getStatusCode() throws IOException {
|
||||
return HttpStatus.SERVICE_UNAVAILABLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回HTTP状态代码
|
||||
*/
|
||||
@Override
|
||||
public int getRawStatusCode() throws IOException {
|
||||
return HttpStatus.SERVICE_UNAVAILABLE.value();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回响应的HTTP状态文本
|
||||
*/
|
||||
@Override
|
||||
public String getStatusText() throws IOException {
|
||||
return HttpStatus.SERVICE_UNAVAILABLE.getReasonPhrase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将消息正文作为输入流返回
|
||||
*/
|
||||
@Override
|
||||
public InputStream getBody() throws IOException {
|
||||
return new ByteArrayInputStream("商城崩溃了,请稍后重试!".getBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将消息正文作为输入流返回
|
||||
*/
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
return httpHeaders;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.heibaiying.zuul.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 登录
|
||||
*/
|
||||
@Controller
|
||||
public class LoginController {
|
||||
|
||||
@RequestMapping("index")
|
||||
public String login(){
|
||||
return "index";
|
||||
}
|
||||
|
||||
@RequestMapping("login")
|
||||
public String login(String username,HttpSession session){
|
||||
session.setAttribute("code",username+String.valueOf(new Random().nextInt(10*1000)));
|
||||
return "redirect:/consumer/sell/products";
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package com.heibaiying.zuul.filter;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.exception.ZuulException;
|
||||
import org.omg.CORBA.Request;
|
||||
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 自定义filter过滤器
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class CustomZuulFilter extends ZuulFilter {
|
||||
|
||||
/**
|
||||
* 返回过滤器的类型
|
||||
*/
|
||||
@Override
|
||||
public String filterType() {
|
||||
return FilterConstants.PRE_TYPE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回过滤器的优先级顺序
|
||||
*/
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从此方法返回“true”意味着应该调用下面的 run()方法
|
||||
*/
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* ZuulFilter的核心校验方法
|
||||
*/
|
||||
@Override
|
||||
public Object run() throws ZuulException {
|
||||
RequestContext currentContext = RequestContext.getCurrentContext();
|
||||
HttpServletRequest request = currentContext.getRequest();
|
||||
String code = (String)request.getSession().getAttribute("code");
|
||||
if (StringUtils.isEmpty(code)){
|
||||
// 设置值为false 不将请求转发到对应的服务上
|
||||
currentContext.setSendZuulResponse(false);
|
||||
// 设置返回的状态码
|
||||
currentContext.setResponseStatusCode(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value());
|
||||
HttpServletResponse response = currentContext.getResponse();
|
||||
try {
|
||||
// 跳转到登录页面
|
||||
response.sendRedirect("/index");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
server:
|
||||
port: 8090
|
||||
# 指定服务命名
|
||||
spring:
|
||||
application:
|
||||
name: zuul
|
||||
# 指定注册中心地址
|
||||
eureka:
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://localhost:8010/eureka/
|
||||
# 网关的路由
|
||||
zuul:
|
||||
routes:
|
||||
xxxx: #这个地方的值是可以任意的字符串
|
||||
path: /producer/**
|
||||
serviceId: producer
|
||||
consumer:
|
||||
path: /consumer/**
|
||||
serviceId: consumer
|
||||
# 指定前缀
|
||||
# prefix: /v1
|
@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="/login" method="post">
|
||||
<input name="username" type="text">
|
||||
<button id="btn">输入临时用户名后登录!</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.zuul;
|
||||
|
||||
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 ZuulApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user