springboot整合dubbo注解方式

Posted 丁春秋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot整合dubbo注解方式相关的知识,希望对你有一定的参考价值。

工程结构:

主pom

<?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>service-privder</module>
        <module>service-consumer</module>
        <module>service-common</module>
    </modules>
    <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.cxy</groupId>
    <artifactId>springboot-dubbo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-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.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

注意事项:引入图中整个dubbo依赖时候要加入这个zookeeper的依赖,不然会报calssnotfound错误:

common代码:

1 pom

<?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>springboot-dubbo</artifactId>
        <groupId>com.cxy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-common</artifactId>


</project>

2 product

package com.cxy.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product implements Serializable {

    // 产品序列号
    private int id;

    // 产品名称
    private String name;

    // 是否贵重品
    private Boolean isPrecious;

    //生产日期
    private Date dateInProduced;

    //产品价格
    private float price;
}

api

package com.cxy.api;



import com.cxy.bean.Product;

import java.util.List;


public interface IProductService {

    Product queryProductById(int id);

    List<Product> queryAllProducts();
}

privoder代码

1 pom

<?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>springboot-dubbo</artifactId>
        <groupId>com.cxy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-privder</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.cxy</groupId>
            <artifactId>service-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2 productservice

package com.cxy.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.cxy.api.IProductService;
import com.cxy.bean.Product;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @description : 产品提供接口实现类
 */
@Service(timeout = 5000,version="1.0.0",interfaceClass = IProductService.class)
@Component
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() {
        List<Product> myProductList = new ArrayList<>();
        myProductList.add(new Product(1,"洗衣机",true,new Date(),66.66f));
        myProductList.add(new Product(2,"洗衣机",true,new Date(),66.66f));
        return myProductList;
    }
}

3 启动类:

package com.cxy;

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);
    }

}

4 yml:

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代码

1 pom

<?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>springboot-dubbo</artifactId>
        <groupId>com.cxy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.cxy</groupId>
            <artifactId>service-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2 controller

package com.cxy.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.cxy.api.IProductService;
import com.cxy.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;

@Controller
@RequestMapping("sell")
public class SellController {

    // dubbo远程引用注解
    @Reference(version = "1.0.0")
    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";
    }
}

3 启动类:

package com.cxy;

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);
    }

}

4 yml:

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

5 freemark模板文件:

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

点击两个启动类:

访问:

 

以上是关于springboot整合dubbo注解方式的主要内容,如果未能解决你的问题,请参考以下文章

dubbo与springboot的三种整合方式

手把手教你Dubbo与SpringBoot常用两种方式整合

dubbo-整合springboot基于注解的简单实例

最新官方版的SpringBoot 整合 Dubbo.md

springboot与dubbo整合遇到的坑

2020最新IDEA SpringBoot整合Dubbo(zookeeper版)