问题导读
1.Dubbo是什么?
2.Dubbo的服务提供方和服务消费方如何用代码实现?
3.Zookeeper如何安装配置?
Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。
Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
- provider:暴露服务的服务提供方
- consumer:调用远程服务的服务消费方
- registry:服务注册于发现的注册中心
- monitor:统计服务调用次数和调用时间的监控中心
- container:服务运行容器
一、dubbo-provider(服务提供方)
1、Java Bean
  为什么要实现Serializable接口?当我们需要把对象的状态信息通过网络进行传输,或者需要将对象的状态信息持久化,以便将来使用时都需要把对象进行序列化。
  使用了Lombok,它能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。
[mw_shl_code=java,true]@Data
public class UserInfo implements Serializable {
private String account;
private String password;
}[/mw_shl_code]
2、UserService
  服务提供方暴露的服务,将注册到zookeeper上。
[mw_shl_code=java,true]public interface UserService { // 定义用户登录的api
UserInfo login(UserInfo user);
}[/mw_shl_code]
3、UserServiceImpl
  服务提供方暴露的服务对应的实现类。
[mw_shl_code=java,true]@Component
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
public UserInfo login(UserInfo user) {
UserInfo reUser = new UserInfo();
reUser.setAccount("登录的账号为:"+user.getAccount());
reUser.setPassword("登录的密码为:"+user.getPassword());
return reUser;
}
}[/mw_shl_code]
4、DubboProviderApplication
[mw_shl_code=java,true]@SpringBootApplication
@EnableDubboConfiguration // 启用dubbo自动配置
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}[/mw_shl_code]
5、pom.xml
[mw_shl_code=xml,true]<?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>
<relativePath/> <!-- lookup parent from repository -->
</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>
<!--Dubbo依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--自动生成getter,setter,equals,hashCode和toString等等-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<!--Zookeeper客户端-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--Zookeeper依赖,排除log4j避免依赖冲突-->
<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>[/mw_shl_code]
6、application.yml
[mw_shl_code=xml,true]spring:
dubbo:
application:
name: dubbo-provider
protocol:
name: dubbo
port: 20880
registry:
address: zookeeper://127.0.0.1:2181[/mw_shl_code]
二、dubbo-consumer(服务消费方)
1、UserController
[mw_shl_code=java,true]@RestController
public class UserController {
@Reference // 引用dubbo服务器提供服务器接口
private UserService userService;
@GetMapping("/login")
public UserInfo login(UserInfo userInfo) {
return userService.login(userInfo);
}
}[/mw_shl_code]
2、DubboConsumerApplication
[mw_shl_code=java,true]@SpringBootApplication
@EnableDubboConfiguration // 启用dubbo自动配置
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
[/mw_shl_code]
3、pom.xml
[mw_shl_code=xml,true]<?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>
<relativePath/> <!-- lookup parent from repository -->
</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>
<!--依赖dubbo-provider服务提供者-->
<dependency>
<groupId>com.boot.dubbo.demo</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--Dubbo依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--Zookeeper客户端-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--Zookeeper依赖,排除log4j避免依赖冲突-->
<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>[/mw_shl_code]
4、application.yml
[mw_shl_code=xml,true]spring:
dubbo:
application:
name: dubbo-consumer
protocol:
name: dubbo
port: 20880
registry:
address: zookeeper://127.0.0.1:2181
server:
port: 8081[/mw_shl_code]
三、Zookeeper安装配置
《Zookeeper是什么?》
《Zookeeper的安装配置》
《Zookeeper集群环境搭建》
四、项目启动
五、查看Zookeeper服务注册情况
六、通过浏览器发送请求(dubbo-provider的服务注册到Zookeeper上,dubbo-consumer消费者可以调用注册的服务)。
到此,rpc远程调用服务通了!感兴趣的话可以去学习下。
最新经典文章,欢迎关注公众号
来源:CSDN 作者:码农云帆哥 原文:《新人一看就懂:Dubbo+Zookeeper的RPC远程调用框架demo》 https://blog.csdn.net/sinat_27933301/article/details/100991670?utm_source=app
|