增加 spring cloud 用例
This commit is contained in:
parent
d9a7aef780
commit
bd7cc53b50
BIN
pictures/zipkin-detail.png
Normal file
BIN
pictures/zipkin-detail.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 52 KiB |
BIN
pictures/zipkin.png
Normal file
BIN
pictures/zipkin.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 47 KiB |
8
spring-cloud/spring-cloud-sleuth-zipkin/README.md
Normal file
8
spring-cloud/spring-cloud-sleuth-zipkin/README.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
https://zipkin.io/pages/quickstart
|
||||||
|
|
||||||
|
```$xslt
|
||||||
|
INFO [zuul,ecdee56c885e0d3b,ecdee56c885e0d3b,false] 16312 --- [nio-8090-exec-1]
|
||||||
|
INFO [zuul,ecdee56c885e0d3b,ecdee56c885e0d3b,false] 16312 --- [nio-8090-exec-1]
|
||||||
|
INFO [zuul,e2d282108f861cf4,e2d282108f861cf4,false] 16312 --- [nio-8090-exec-4]
|
||||||
|
INFO [zuul,e2d282108f861cf4,e2d282108f861cf4,false] 16312 --- [nio-8090-exec-4]
|
||||||
|
```
|
22
spring-cloud/spring-cloud-sleuth-zipkin/common/pom.xml
Normal file
22
spring-cloud/spring-cloud-sleuth-zipkin/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.sleuth.zipkin</groupId>
|
||||||
|
<artifactId>spring-cloud-sleuth-zipkin</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() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
55
spring-cloud/spring-cloud-sleuth-zipkin/consumer/pom.xml
Normal file
55
spring-cloud/spring-cloud-sleuth-zipkin/consumer/pom.xml
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?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.sleuth.zipkin</groupId>
|
||||||
|
<artifactId>spring-cloud-sleuth-zipkin</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.sleuth.zipkin</groupId>
|
||||||
|
<artifactId>common</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--zipkin-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-zipkin</artifactId>
|
||||||
|
</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,18 @@
|
|||||||
|
server:
|
||||||
|
port: 8080
|
||||||
|
# 指定服务命名
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: consumer
|
||||||
|
# 指定zipkin地址 默认就是http://localhost:9411/
|
||||||
|
zipkin:
|
||||||
|
base-url: http://localhost:9411/
|
||||||
|
# 指定注册中心地址
|
||||||
|
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-sleuth-zipkin/eureka/pom.xml
Normal file
31
spring-cloud/spring-cloud-sleuth-zipkin/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.sleuth.zipkin</groupId>
|
||||||
|
<artifactId>spring-cloud-sleuth-zipkin</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-sleuth-zipkin/pom.xml
Normal file
64
spring-cloud/spring-cloud-sleuth-zipkin/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.sleuth.zipkin</groupId>
|
||||||
|
<artifactId>spring-cloud-sleuth-zipkin</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>
|
48
spring-cloud/spring-cloud-sleuth-zipkin/producer/pom.xml
Normal file
48
spring-cloud/spring-cloud-sleuth-zipkin/producer/pom.xml
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?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.sleuth.zipkin</groupId>
|
||||||
|
<artifactId>spring-cloud-sleuth-zipkin</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.sleuth.zipkin</groupId>
|
||||||
|
<artifactId>common</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--zipkin-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-zipkin</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.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,13 @@
|
|||||||
|
server:
|
||||||
|
port: 8020
|
||||||
|
# 指定服务命名
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: producer
|
||||||
|
zipkin:
|
||||||
|
base-url: http://localhost:9411/
|
||||||
|
# 指定注册中心地址
|
||||||
|
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() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
74
spring-cloud/spring-cloud-sleuth-zipkin/zuul/pom.xml
Normal file
74
spring-cloud/spring-cloud-sleuth-zipkin/zuul/pom.xml
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
<?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>
|
||||||
|
<!--zipkin-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-zipkin</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,25 @@
|
|||||||
|
server:
|
||||||
|
port: 8090
|
||||||
|
# 指定服务命名
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: zuul
|
||||||
|
zipkin:
|
||||||
|
base-url: http://localhost:9411/
|
||||||
|
# 指定注册中心地址
|
||||||
|
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() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
72
spring-cloud/spring-cloud-stream/pom.xml
Normal file
72
spring-cloud/spring-cloud-stream/pom.xml
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<?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/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.heibaiying.stream</groupId>
|
||||||
|
<artifactId>spring-cloud-stream</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-cloud-stream</name>
|
||||||
|
<description>spring cloud stream project for Spring Boot</description>
|
||||||
|
|
||||||
|
<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-amqp</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</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>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.heibaiying.stream;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringCloudStreamApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringCloudStreamApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.heibaiying.stream.bean;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Programmer implements Serializable {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
private float salary;
|
||||||
|
|
||||||
|
private Date birthday;
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
package com.heibaiying.stream.controller;
|
||||||
|
|
||||||
|
import com.heibaiying.stream.bean.Programmer;
|
||||||
|
import com.heibaiying.stream.stream.Custom;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.messaging.Message;
|
||||||
|
import org.springframework.messaging.MessageHeaders;
|
||||||
|
import org.springframework.messaging.support.MessageBuilder;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 发送测试消息
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class MessageController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Custom custom;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 发送简单消息
|
||||||
|
*/
|
||||||
|
@RequestMapping("sendSimpleMessage")
|
||||||
|
public void sendSimpleMessage() {
|
||||||
|
custom.input().send(MessageBuilder.withPayload("hell spring cloud stream").build());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 发送消息体为对象的消息
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@RequestMapping("sendObject")
|
||||||
|
public void sendObject() {
|
||||||
|
Programmer programmer=new Programmer("pro",12,212.2f,new Date());
|
||||||
|
custom.input().send(MessageBuilder.withPayload(programmer).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送带有消息头的消息
|
||||||
|
*/
|
||||||
|
@RequestMapping("sendWithHeads")
|
||||||
|
public void sendWithHeads() {
|
||||||
|
Programmer programmer=new Programmer("pro",12,212.2f,new Date());
|
||||||
|
Map<String,Object> map=new HashMap<>();
|
||||||
|
map.put("code","868686");
|
||||||
|
MessageHeaders messageHeaders=new MessageHeaders(map);
|
||||||
|
Message<Programmer> message= MessageBuilder.createMessage(programmer,messageHeaders);
|
||||||
|
custom.input().send(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条件消息 可以看做是消息路由键的一种实现
|
||||||
|
*/
|
||||||
|
@RequestMapping("sendWithKey")
|
||||||
|
public void sendWithKey() {
|
||||||
|
// 创建消息头key 为 01 的消息
|
||||||
|
Programmer programmer=new Programmer("key01",12,212.2f,new Date());
|
||||||
|
Map<String,Object> map=new HashMap<>();
|
||||||
|
map.put("key","01");
|
||||||
|
MessageHeaders messageHeaders=new MessageHeaders(map);
|
||||||
|
Message<Programmer> message= MessageBuilder.createMessage(programmer,messageHeaders);
|
||||||
|
custom.input().send(message);
|
||||||
|
|
||||||
|
// 创建消息头key 为 02 的消息
|
||||||
|
programmer.setName("key02");
|
||||||
|
map.put("key","02");
|
||||||
|
MessageHeaders messageHeaders02=new MessageHeaders(map);
|
||||||
|
Message<Programmer> message02= MessageBuilder.createMessage(programmer,messageHeaders02);
|
||||||
|
custom.input().send(message02);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.heibaiying.stream.stream;
|
||||||
|
|
||||||
|
import org.springframework.cloud.stream.annotation.Input;
|
||||||
|
import org.springframework.cloud.stream.annotation.Output;
|
||||||
|
import org.springframework.messaging.MessageChannel;
|
||||||
|
import org.springframework.messaging.SubscribableChannel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
public interface Custom {
|
||||||
|
|
||||||
|
String INPUT = "input";
|
||||||
|
String OUTPUT = "output";
|
||||||
|
|
||||||
|
@Input(Custom.INPUT)
|
||||||
|
SubscribableChannel input();
|
||||||
|
|
||||||
|
@Output(Custom.OUTPUT)
|
||||||
|
MessageChannel output();
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
package com.heibaiying.stream.stream;
|
||||||
|
|
||||||
|
import com.heibaiying.stream.bean.Programmer;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||||
|
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||||
|
import org.springframework.messaging.handler.annotation.Header;
|
||||||
|
import org.springframework.messaging.handler.annotation.Headers;
|
||||||
|
import org.springframework.messaging.handler.annotation.Payload;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description :消息的监听
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@EnableBinding(Custom.class)
|
||||||
|
@Slf4j
|
||||||
|
public class StreamReceived {
|
||||||
|
|
||||||
|
@StreamListener(value = Custom.INPUT)
|
||||||
|
public void simple(Object payload) {
|
||||||
|
log.info("收到简单消息: {}", payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
@StreamListener(value = Custom.INPUT)
|
||||||
|
public void object(Programmer programmer) {
|
||||||
|
log.info("收到对象消息: {}", programmer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用 @Header 监听时候需要注意,指定名称的属性必须在消息头中存在 不然就会抛出异常 MessageHandlingException: Missing header 'XXXX' for method parameter type [class java.lang.String]
|
||||||
|
*/
|
||||||
|
@StreamListener(value = Custom.INPUT)
|
||||||
|
public void heads(@Payload Programmer programmer, @Headers Map<String, Object> map, @Header(name = "code") String code) {
|
||||||
|
log.info("收到对象消息: {}", programmer);
|
||||||
|
map.forEach((key, value) -> {
|
||||||
|
log.info("消息头{}的值为{}", key, value);
|
||||||
|
});
|
||||||
|
log.info("绑定指定消息头: code = {}", code);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听消息头key = 01 的消息
|
||||||
|
*/
|
||||||
|
@StreamListener(target = Custom.INPUT, condition = "headers['key']=='01'")
|
||||||
|
public void key01(@Payload Programmer programmer) {
|
||||||
|
log.info("key01 监听器接收到消息: {}", programmer.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听消息头key = 02 的消息
|
||||||
|
*/
|
||||||
|
@StreamListener(target = Custom.INPUT, condition = "headers['key']=='01'")
|
||||||
|
public void key02(@Payload Programmer programmer) {
|
||||||
|
log.info("key02 监听器接收到消息: {}", programmer.getName());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
spring:
|
||||||
|
rabbitmq:
|
||||||
|
host: localhost
|
||||||
|
port: 5672
|
||||||
|
username: guest
|
||||||
|
password: guest
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.stream;
|
||||||
|
|
||||||
|
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 SpringCloudStreamApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user