写点什么

新人一看就懂:Dubbo+Zookeeper 的 RPC 远程调用框架 demo

作者:Java高工P7
  • 2021 年 11 月 12 日
  • 本文字数:2845 字

    阅读完需:约 9 分钟

4、DubboProviderApplication


@SpringBootApplication


@EnableDubboConfiguration // 启用 dubbo 自动配置


public class DubboProviderApplication {


public static void main(String[] args) {


SpringApplication.run(DubboProviderApplication.class, args);


}


}


5、pom.xml


POM 是项目对象模型(Project Object Model)的简称,它是 Maven 项目中的文件,使用 XML 表示,名称叫做 pom.xml。作用类似 ant 的 build.xml 文件,功能更强大。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的 url、项目的依赖关系等等。


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


<groupId>com.boot.dubbo.demo</groupId>


<artifactId>dubbo-provider</artifactId>


<version>0.0.1-SNAPSHOT</version>


<packaging>jar</packaging>


<name>dubbo-provider</name>


<description>Demo project for Spring Boot</description>


<parent>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-parent</artifactId>


<version>2.0.6.RELEASE</version>


</parent>


<properties>


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>


<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>


<java.version>1.8</java.version>


</properties>


<dependencies>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-web</artifactId>


</dependency>


<dependency>


<groupId>com.alibaba.spring.boot</groupId>


<artifactId>dubbo-spring-boot-starter</artifactId>


<version>2.0.0</version>


</dependency>


<dependency>


<groupId>org.projectlombok</groupId>


<artifactId>lombok</artifactId>


<version>1.16.20</version>


<scope>provided</scope>


</dependency>


<dependency>


<groupId>com.101tec</groupId>


<artifactId>zkclient</artifactId>


<version>0.10</version>


</dependency>


<dependency>


<groupId>org.apache.zookeeper</groupId>


<artifactId>zookeeper</artifactId>


<version>3.4.10</version>


<exclusions>


<exclusion>


<groupId>org.slf4j</groupId>


<artifactId>slf4j-log4j12</artifactId>


</exclusion>


<exclusion>


<groupId>log4j</groupId>


<artifactId>log4j</artifactId>


</exclusion>


</exclusions>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-test</artifactId>


<scope>test</scope>


</dependency>


</dependencies>


<build>


<plugins>


<plugin>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-maven-plugin</artifactId>


</plugin>


</plugins>


</build>


</project>


6、application.yml


Spring Boot 配置文件有两种:application.yml 和 application.properties,作用是一样的,不过因为 yml 文件是树状结构,写起来有更好的层次感,更易于理解,所以很多人都选择了 yml 文件。


spring:


dubbo:


application:


name: dubbo-provider


protocol:


name: dubbo


port: 20880


registry:


address: zookeeper://127.0.0.1:2181


四、dubbo-consumer(服务消费方)





1、UserController


@RestController 注解相当于 @ResponseBody + @Controller 合在一起的作用。如果要返回 json 内容,就用 @RestController,如果要返回到指定页面,就用 @Controller。


@Reference 注解就是用于标记这个服务具体使用了生产者的哪个接口实现。


@RestController


public class UserController {


@Reference // 引用 dubbo 服务器提供服务器接口


private UserService userService;


@GetMapping("/login")


public UserInfo login(UserInfo userInfo) {


return userService.login(userInfo);


}


}


2、DubboConsumerApplication


@SpringBootApplication


@EnableDubboConfiguration // 启用 dubbo 自动配置


public class DubboConsumerApplication {


public static void main(String[] args) {


SpringApplication.run(DubboConsumerApplication.class, args);


}


}


3、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="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


<modelVersion>4.0.0</modelVersion>


<groupId>com.boot.dubbo.demo</groupId>


<artifactId>dubbo-consumer</artifactId>


<version>0.0.1-SNAPSHOT</version>


<packaging>jar</packaging>


<name>dubbo-consumer</name>


<description>Demo project for Spring Boot</description>


<parent>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-parent</artifactId>


<version>2.0.6.RELEASE</version>


</parent>


<properties>


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>


<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>


<java.version>1.8</java.version>


</properties>


<dependencies>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-web</artifactId>


</dependency>


<dependency>


<groupId>com.boot.dubbo.demo</groupId>


<artifactId>dubbo-provider</artifactId>


<version>0.0.1-SNAPSHOT</version>


</dependency>


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


<dependency>


<groupId>org.apache.zookeeper</groupId>


<artifactId>zookeeper</artifactId>


<version>3.4.10</version>


<exclusions>


<exclusion>


<groupId>org.slf4j</groupId>


<artifactId>slf4j-log4j12</artifactId>


</exclusion>


<exclusion>


<groupId>log4j</groupId>


<artifactId>log4j</artifactId>


</exclusion>


</exclusions>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-test</artifactId>


<scope>test</scope>


</dependency>


</dependencies>


<build>


<plugins>


<plugin>


<groupId>org.springf


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


ramework.boot</groupId>


<artifactId>spring-boot-maven-plugin</artifactId>


</plugin>


</plugins>


</build>


</project>


4、application.yml


spring:


dubbo:


application:


name: dubbo-consumer


protocol:


name: dubbo


port: 20880

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
新人一看就懂:Dubbo+Zookeeper的RPC远程调用框架demo