增加 spring boot dubbo 用例

This commit is contained in:
luoxiang 2019-01-01 17:45:50 +08:00
parent 236620cacd
commit d472a23eb3
16 changed files with 405 additions and 0 deletions

View File

@ -0,0 +1,16 @@
<?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">
<parent>
<artifactId>spring-boot-dubbo</artifactId>
<groupId>com.heibaiying</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>boot-dubbo-common</artifactId>
</project>

View File

@ -0,0 +1,16 @@
package com.heibaiying.api;
import com.heibaiying.bean.Product;
import java.util.List;
/**
* @author : heibaiying
* @description : 产品服务接口类
*/
public interface IProductService {
Product queryProductById(int id);
List<Product> queryAllProducts();
}

View File

@ -0,0 +1,33 @@
package com.heibaiying.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 int id;
// 产品名称
private String name;
// 是否贵重品
private Boolean isPrecious;
//生产日期
private Date dateInProduced;
//产品价格
private float price;
}

View File

@ -0,0 +1,40 @@
<?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>
<artifactId>spring-boot-dubbo</artifactId>
<groupId>com.heibaiying</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>boot-dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-dubbo-consumer</name>
<description>dubbo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--引入对公共模块的依赖-->
<dependencies>
<dependency>
<groupId>com.heibaiying</groupId>
<artifactId>boot-dubbo-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>

View File

@ -0,0 +1,16 @@
package com.heibaiying.dubboconsumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo //开启dubbo的注解支持
public class BootDubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootDubboConsumerApplication.class, args);
}
}

View File

@ -0,0 +1,38 @@
package com.heibaiying.dubboconsumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.heibaiying.api.IProductService;
import com.heibaiying.bean.Product;
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 java.util.List;
/**
* @author : heibaiying
* @description :
*/
@Controller
@RequestMapping("sell")
public class SellController {
// dubbo远程引用注解
@Reference
private IProductService productService;
@RequestMapping
public String productList(Model model) {
List<Product> products = productService.queryAllProducts();
model.addAttribute("products", products);
return "products";
}
@RequestMapping("product/{id}")
public String productDetail(@PathVariable int id, Model model) {
Product product = productService.queryProductById(id);
model.addAttribute("product", product);
return "product";
}
}

View File

@ -0,0 +1,14 @@
dubbo:
application:
name: boot-duboo-provider
# 指定注册协议和注册地址 dubbo推荐使用zookeeper作为注册中心并且在start依赖中引入了zookeeper的java客户端Curator
registry:
protocol: zookeeper
address: 127.0.0.1:2181
protocol.name: dubbo
# 关闭所有服务的启动时检查 (没有提供者时报错)视实际情况设置
consumer:
check: false
server:
port: 8090

View File

@ -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>

View File

@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<title>产品列表</title>
</head>
<body>
<h3>产品列表:点击查看详情</h3>
<ul>
<#list products as product>
<li>
<a href="sell/product/${product.id}">${product.name}</a>
</li>
</#list>
</ul>
</body>
</html>

View File

@ -0,0 +1,17 @@
package com.heibaiying.dubboconsumer;
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 BootDubboConsumerApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@ -0,0 +1,41 @@
<?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>
<artifactId>spring-boot-dubbo</artifactId>
<groupId>com.heibaiying</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>boot-dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-dubbo-provider</name>
<description>dubbo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--引入对公共模块的依赖-->
<dependencies>
<dependency>
<groupId>com.heibaiying</groupId>
<artifactId>boot-dubbo-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>

View File

@ -0,0 +1,16 @@
package com.heibaiying.dubboprovider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo //开启dubbo的注解支持
public class BootDubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootDubboProviderApplication.class, args);
}
}

View File

@ -0,0 +1,39 @@
package com.heibaiying.dubboprovider.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.heibaiying.api.IProductService;
import com.heibaiying.bean.Product;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author : heibaiying
* @description : 产品提供接口实现类
*/
@Service(timeout = 5000)
public class ProductService implements IProductService {
private static List<Product> productList = new ArrayList<>();
static {
for (int i = 0; i < 20; i++) {
productList.add(new Product(i, "产品" + i, i / 2 == 0, new Date(), 66.66f * i));
}
}
public Product queryProductById(int id) {
for (Product product : productList) {
if (product.getId() == id) {
return product;
}
}
return null;
}
public List<Product> queryAllProducts() {
return productList;
}
}

View File

@ -0,0 +1,8 @@
dubbo:
application:
name: boot-duboo-provider
# 指定注册协议和注册地址 dubbo推荐使用zookeeper作为注册中心并且在start依赖中引入了zookeeper的java客户端Curator
registry:
protocol: zookeeper
address: 127.0.0.1:2181
protocol.name: dubbo

View File

@ -0,0 +1,17 @@
package com.heibaiying.dubboprovider;
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 BootDubboProviderApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@ -0,0 +1,63 @@
<?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>
<modules>
<module>boot-dubbo-common</module>
<module>boot-dubbo-consumer</module>
<module>boot-dubbo-provider</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.heibaiying</groupId>
<artifactId>spring-boot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-dubbo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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>
<!--引入dubbo start依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
</project>