springboot2.1.3整合dubbo2.6.5(提供者、消费者Demo 注解方式无xml)

news/2024/7/24 10:39:44 标签: dubbo2.6.5, springboot
  1. linux中启动zookeeper
    在这里插入图片描述
  2. dubbo-api
public interface DubboDemoService {
    List<Brand> findAll();
}
  1. dubbo-provider
  • pom.xml
<dependencies>
        <dependency>
            <groupId>com.chuai</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.3.16.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.thrift</groupId>
                    <artifactId>libthrift</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo-rpc-rest</artifactId>
            <version>2.6.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

  • DubboDemoServiceImpl.java 提供者

import com.alibaba.dubbo.config.annotation.Service;
import com.chuai.api.DubboDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

//注意:Service不是spring的,是阿里的dubbo的Service
@Service
public class DubboDemoServiceImpl implements DubboDemoService{

    @Autowired
    DubboDemoDao dubboDemoDao;

    public List<Brand> findAll() {
        return dubboDemoDao.findAll();
    }
}

  • 启动类
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class ServiceApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceApplication .class, args);
	}
}
  • application.yml
dubbo:
  application:
    name: dubbo-provider
  registry:
    address: zookeeper://192.168.67.130:2181
  protocol:
    name: dubbo
    port: 20880
server:
  port: 9001
  1. dubbo-consumer
  • pom.xml
<dependencies>
        <dependency>
            <groupId>com.chuai</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.3.16.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.thrift</groupId>
                    <artifactId>libthrift</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo-rpc-rest</artifactId>
            <version>2.6.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  • DubboDemoController.java 消费者
import com.alibaba.dubbo.config.annotation.Reference;
import com.chuai.api.DubboDemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DubboDemoController{

    @Reference
    DubboDemoService service;

    @GetMapping("/all")
    public JsonResult findAll(){
        return JsonResult.ok(service.findAll());
    }
}
  • 启动类
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * 	import org.springframework.boot.autoconfigure.domain.EntityScan;
 	import org.springframework.context.annotation.ComponentScan;
 	import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 */
@SpringBootApplication
@EnableDubbo
public class ManagerWebApplication {

	public static void main(String[] args) {
		SpringApplication.run(ManagerWebApplication.class, args);
	}

}
  • application.yml
dubbo:
  application:
    name: dubbo-consumer
  registry:
    address: zookeeper://192.168.67.130:2181
  protocol:
    name: dubbo
    port: 20880
server:
  port: 9101

注意事项:

1、 提供者和消费者的启动类包名要相同。
2、application.yml文件除了name:不同,其他的dubbo项要相同。
3、启动类添加@EnableDubbo
4、@Reference、@Service注解要使用阿里dubbo的包
5、提供者、消费者的pom.xml都要依赖api的工程
6、可在provider的application.yml更改dubbo序列化方式为kryo

dubbo:
  protocol:
    serialization: kryo

http://www.niftyadmin.cn/n/1301356.html

相关文章

微信小程序制作海报保存到相册发朋友圈

这个功能应该分三步来做&#xff1a; 一、制作海报图片 二、保存图片到相册 三、手动发朋友圈再到相册中取图片 详细步骤&#xff1a; 一、制作海报 1.要制作能保存到相册的图片&#xff0c;我们需要一个canvas标签&#xff0c;在我们的wxml的最后加入canvas标签&#xff1a; &…

面试web前端工程师的两个星期的心路历程及面试题

毕业接近3年半了&#xff0c;年初公司进行了一次业务调整&#xff0c;hr小姐姐明里暗里让我们自动滚蛋&#xff0c;但是我牛脾气上来了你不说可能我自己会走&#xff0c;你这一说我偏不走了&#xff0c;于是乎&#xff0c;生生熬到9月份。。。 9月中旬开始改简历&#xff0c;投…

mybatis sum(参数) 列名作为参数

项目中有很多个字段&#xff0c;当字段为空的时候&#xff0c;求该列的平均值并赋值给该字段。如&#xff1a; idnameage 当我需要插入一条数据的时候&#xff0c;这条数据的age为空&#xff0c;那么需要先从数据库查询age的平均值&#xff0c;然后赋值给当前的数据。当字段很…

Angular、React、Vue三选一,前端工程师更青睐使用哪款框架?

现在前端三架马车Angular、React、Vue各有所长。Angular从一开始就带有很明显的工程化血统&#xff0c;齐全的cli命令&#xff0c;背靠TypeScript&#xff0c;涉及模块、服务以及指令等概念&#xff0c;使用后端的依赖注入思想&#xff0c;特有模板语法。React和Vue就"轻&…

重启服务器前后需要做的事

重启前 1. 看服务器启动了哪些应用&#xff0c;并导入记录到本地 ps看哪些应用正在运行&#xff08;more–>空格或回车翻页&#xff09; ps -ef|more如tomcat、apache的httpd、zookeeper、memcache、还有springboot springcloud项目、solr等等服务。 导出ps信息到本地 …

TexturePacker破解版教程及下载

如果已经了解如何破解的同学可以忽略文章&#xff0c;直接下载&#xff1a; 百度网盘https://pan.baidu.com/s/1xPQEpsg33goeBEF-wXvuog#list/path%2F,密码egyf。 大家只需要下载TexturePacker_3.0.9.rar就可以了&#xff1a;虽然百度网盘下载的不是一般的慢&#xff0c;但是好…

一份全面的React、Angular和Vue.js比较指南

对于开发人员而言&#xff0c;选择技术栈有时是一件相当棘手的任务。因为您需要综合考虑诸如&#xff1a;预算、时间、应用大小、最终用户、项目目标、以及可用资源等多方面因素。 无论您是初学者、开发人员、自由职业者还是项目架构师&#xff0c;只有详细了解了每种框架的优…

微信小程序模板template总结

没有看过微信小程序template的同学们可以先去官网上了解一下。 以下是对template的几点总结&#xff1a; 一、 template&#xff0c;是一个wxml文件&#xff0c;所以在template中没有处理逻辑的功能&#xff08;如果希望了解有处理逻辑功能的模块化组件&#xff0c;可以查看下一…