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>

点击两个启动类:

访问:

原文地址:https://www.cnblogs.com/xiufengchen/p/10337412.html

时间: 2024-08-03 10:21:54

springboot整合dubbo注解方式的相关文章

Springboot整合Dubbo和Zookeeper

Dubbo是一款由阿里巴巴开发的远程服务调用框架(RPC),其可以透明化的调用远程服务,就像调用本地服务一样简单.截至目前,Dubbo发布了基于Spring Boot构建的版本,版本号为0.2.0,这使得其与Spring Boot项目整合变得更为简单方便.而Zookeeper在这里充当的是服务注册中心的角色,我们将各个微服务提供的服务通过Dubbo注册到Zookeeper中,然后服务消费者通过Dubbo从Zookeeper中获取相应服务并消费.本文案例的架构图可以简单用下图表示: 本文案例最终项

SpringBoot整合dubbo(yml格式配置)

yml文件 如果只作为服务的消费者不用暴露端口号,扫描的包名根据自己service改 dubbo: application: name: springboot-dubbo-demo #应用名 registry: address: zookeeper://127.0.0.1:2181 #zookeeper地址 # port: 2181 #提供注册的端口 protocol: name: dubbo port: "20889" #dubbo服务暴露的端口 scan: com.bw.ssm.s

springboot整合mybatis(注解)

springboot整合mybatis(注解) 1.pom.xml: <?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="ht

Springboot 整合 Dubbo/ZooKeeper

本文提纲 一.为啥整合 Dubbo 实现 SOA 二.运行 springboot-dubbo-server 和 springboot-dubbo-client 工程 三.springboot-dubbo-server 和 springboot-dubbo-client 工程配置详解 一.为啥整合 Dubbo 实现 SOA Dubbo 不单单只是高性能的 RPC 调用框架,更是 SOA 服务治理的一种方案. 核心: 1. 远程通信,向本地调用一样调用远程方法. 2. 集群容错 3. 服务自动发现和

SpringBoot整合dubbo

Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成. 以上介绍来源于百度百科,具体dubbo相关可以自行查找资料,本文只是介绍SpringBoot简单整合dubbo. 1.安装zookeeper 1.1 去官网下载,本文以3.4.12 版本为例子http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/ 1.2 下载之后解压ZooKeepe

SpringBoot整合Dubbo案例

使用框架: jdk 1.8 springboot-2.1.3 dubbo-2.6 spring-data-jpa-2.1.5 一.开发dubbo服务接口: 按照Dubbo官方开发建议,创建一个接口项目,该项目只定义接口和model类: 1.创建springboot工程 spring-boot-demo-dubbo-interface 坐标: <groupId>com.example</groupId> <artifactId>spring-boot-demo-dubbo

纯手写SpringBoot框架之注解方式启动SpringMVC容器

使用Java语言创建Tomcat容器,并且通过Tomcat执行Servlet,接下来,将会使用Java语言在SpringBoot创建内置Tomcat,使用注解方式启动SpringMVC容器. 代码实现.1.pom.xml文件,需要依赖的jar包. <dependencies> <!--Java语言操作Tomcat--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <arti

springBoot 整合 dubbo 遇到的坑

一.注意springBoot 和 dubbo 之间版本的问题 <?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:/

spring-boot整合dubbo:Spring-boot-dubbo-starter

为什么要写这个小工具 如果你用过Spring-boot来提供dubbo服务,相信使用中有很多"不爽"的地方.既然使用spring boot,那么能用注解的地方绝不用xml配置,这才是spring-boot-style.开个玩笑,真正意思是,spring-boot适合一些简单的.独立的服务,一个大的系统是不适合使用spring-boot来开发.相反,spring-boot适合那些简单服务的搭建. 网上大多数的方法还是使用xml配置,通过@Import注解来引入xml配置. 怎么使用 对于