Dubbo与Zookeeper的使用
文章目录
1. 架构的演变
单体架构:将所有的业务功能都放在一个项目当中进行开发。(SSM,SpringBoot+MP)打一个war包 或者jar包
优点:架构简单;部署,运行比较简单;调试也简单
缺点:耦合度比较高(无论哪个功能代码出问题,都会导致整个服务失败)
分布式架构 :根据业务功能从整个大的项目当中拆分成多个独立的子项目,子项目直接互相通信,从而 完成整个工作。
优点:降低了耦合度,各个项目独立运行,影响非常小;利用扩展,可以快速通过添加新项目来扩 展业务
缺点:部署,运维比较麻烦;测试比较复杂(需要多个项目团队进行协调,配合)
- SOA架构(面向服务的架构):在分布式架构的基础上,根据业务功能拆分出逻辑独立的服务,服务和 服务之前都可以通过暴露的接口进行通信。
优点:提供代码的重复利用率了 (公用的部分只需要开发一份就可以了)
缺点:服务和服务之间通信比较麻烦,需要 ESB(企业服务总线 ),软件;成本比较高 soap协议进行通信(服务之间交互数据;http+xml),比较耗费带宽
- 微服务架构: 在SOA架构的基础进行改良产生的一种架构。在拆分项目当中粒度更细。项目和项目独立 性更加强(每个项目可以使用不同的语言开发,不同的数据库进行存储数据)
SpringCloud 框架(微服务架构的一整套解决方案)
SpringCloud Alibaba 框架 (在SpringCloud 框架的基础上进一步改良和优化,目前最火的微服务 框架)
2.Dubbo的架构原理
什么是Dubbo? Apache Dubbo 是一款微服务框架,为大规模微服务实践提供高性能 RPC (远程过程调用)通信、服 务发现、负载均衡、流量调度 等解决方案
Dubbo的工作原理
3.Zookeeper 注册中心
- 什么是Zookeeper?
Zookeeper 是一个分布式的,开放源码的分布式应用程序协调服务(注册中心服务器),并提供一下功能: 提供的功能包括:配置维护、名字服务、分布式同步、组服务
- 如何使用
1. 官网下载最新版的Zookeeper (https://dlcdn.apache.org/zookeeper/zookeeper-3.8.0/) 2. 解压到非中文目录下,修改配置文件 3. 在zookeeper目录下新建一个data目录,接下来把conf目录下的zoo_sample.cfg 名修改为 zoo.cfg 4. 把配置文件中dataDir=值更改为自己的data目录(eg:dataDir=D:\\apache-zookeeper-3.8.0- bin\\data) 5. 需要在配置文件中添加代码:audit.enable=true 6. 到bin目录下双击运行 zkServer.cmd 即可开启服务
4.代码演示Dubbo框架的使用 (方法一)
前提启动Zookeeper注册中心
1. 编写服务提供者(springboot-dubbo-provider)
- 架构图
- 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>org.example</groupId> <artifactId>springboot-dubbo-provider</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <!-- Spring Boot 启动父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> </parent> <dependencies> <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-rpc-dubbo</artifactId> <version>3.0.7</version> </dependency> <!--注册中心--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-zookeeper</artifactId> <version>3.0.7</version> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot Dubbo 服务接口 依赖 --> <dependency> <groupId>org.example</groupId> <artifactId>springboot-dubbo-interf</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
- 配置文件(application.yml)
dubbo: application: name: provider protocol: name: dubbo port: 20882 registry: address: zookeeper://127.0.0.1:2181 server: # 端口号 port: 8082
- 实现类(UserServiceImpl)
package com.mk.demo.serviceimpl; import org.apache.dubbo.config.annotation.DubboService; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Value; @DubboService public class UserServiceImpl implements UserService { @Value("${server.port}") private String port; @Override public String getUserId(Integer id) { System.out.println(" springboot-dubbo-provider "+port+" 被消费....id:" + id); if (id == 1) { return "王者"+port; } else if (id == 2) { return "吃鸡"+port; } else{ return "随缘"+port; } } }
- 启动类(Application)
package com.mk.demo; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
- 启动项目
2. 编写服务公共部分(springboot-dubbo-interf)
- 架构图
- 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>org.example</groupId> <artifactId>springboot-dubbo-interf</artifactId> <version>1.0-SNAPSHOT</version> <name>springboot-dubbo-interf</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <description>SpirngBoot 项目下 Dubbo 公共服务接口类</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
- 编写接口(UserService)
package org.example.service; public interface UserService { public String getUserId(Integer id); }
- 编写启动类(App)
package org.example; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }
- 启动项目
3. 编写服务消费者(springboot-dubbo-consumer)
- 架构图
- 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>org.example</groupId> <artifactId>springboot-dubbo-consumer</artifactId> <version>1.0-SNAPSHOT</version> <description>springboot-dubbo 客户端:整合 Dubbo/ZooKeeper</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding> UTF-8</project.reporting.outputEncoding> </properties> <!-- Spring Boot 启动父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-rpc-dubbo</artifactId> <version>3.0.7</version> </dependency> <!--注册中心--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-zookeeper</artifactId> <version>3.0.7</version> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot Dubbo 服务接口 依赖 --> <dependency> <groupId>org.example</groupId> <artifactId>springboot-dubbo-interf</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
- 编写配置文件(application.yml)
server: # 端口号 port: 8081 dubbo: application: name: consumer registry: address: zookeeper://127.0.0.1:2181
- 编写接口(UserServiceConsumer)
package com.mk.demo.service; import org.apache.dubbo.config.annotation.DubboReference; import org.example.service.UserService; import org.springframework.stereotype.Service; @Service //注意要导的包 public class UserServiceConsumer { @DubboReference private UserService userService; public String getUser(Integer id){ return userService.getUserId(id); } }
注意@Service,@DubboReference
- 编写控制层(UserController)
package com.mk.demo.controller; import com.mk.demo.service.UserServiceConsumer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class UserController { @Resource private UserServiceConsumer userServiceConsumer;//自动注入上一步创建的服务消费类 /** * http://localhost:8081/getUserId?id=2 * 使用 dubbo 协议调用远程服务 * @param id * @return */ @RequestMapping("/getUserId") public Object getUserId(Integer id) { return userServiceConsumer.getUser(id); } }
- 编写启动类(Application)
package com.mk.demo; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
注意@EnableDubbo
- 启动项目
- 访问(http://localhost:8081/getUserId?id=2)
- 结果
5.代码演示Dubbo框架的使用 (方法二)
1. 编写服务提供者(springboot-dubbo-provider)
- 架构图
- 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>org.example</groupId> <artifactId>springboot-dubbo-provider</artifactId> <version>1.0-SNAPSHOT</version> <description>springboot-dubbo 服务端:整合 Dubbo/ZooKeeper</description> <!-- Spring Boot 启动父依赖 --> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.7.0</version> </parent> <dependencies> <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0</version> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- Spring Boot Dubbo 服务接口 依赖 --> <dependency> <groupId>org.example</groupId> <artifactId>springboot-dubbo-interf</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>
- 配置文件(application.properties)
#服务端提供端使用协议 spring.dubbo.protocol.name=dubbo #服务端提供端暴露端口号 spring.dubbo.protocol.port=20882 #服务所在包名称(为三个项目的公共部分) spring.dubbo.scan=com.mk.demo #应用端口 server.port=8002 #应用项目名称 #server.context-path=/ #修改 tomcat 的 URIEncoding 为 UTF-8 server.tomcat.uri-encoding=UTF-8 #jackson 对日期时间格式化设置:时间格式 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss #jackson 对日期时间格式化设置:时区设置 spring.jackson.time-zone=GMT+8
- 实现类(UserServiceImpl)
package com.mk.demo.impl; import com.alibaba.dubbo.config.annotation.Service; import com.mk.demo.service.UserService; import org.springframework.beans.factory.annotation.Value; @Service public class UserServiceImpl implements UserService { @Value("${server.port}") private String port; @Override public String getUserId(Integer id) { System.out.println(" springboot-dubbo-provider "+port+" 被消费....id:" + id); if (id == 1) { return "王者"+port; } else if (id == 2) { return "吃鸡"+port; } else{ return "随缘"+port; } } }
注意@Service注解
- 启动类(Application)
package com.mk.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
- 启动项目
2. 编写服务公共部分(springboot-dubbo-interf)
- 架构图
- 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>org.example</groupId> <artifactId>springboot-dubbo-interf</artifactId> <version>1.0-SNAPSHOT</version> <description>SpirngBoot 项目下 Dubbo 公共服务接口类</description> </project>
- 编写接口(UserService)
package com.mk.demo.service; public interface UserService { public String getUserId(Integer id); }
- 编写启动类(Application)
public class Application { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }
- 启动项目
3. 编写服务消费者(springboot-dubbo-consumer)
- 架构图
- 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>org.example</groupId> <artifactId>springboot-dubbo-consumer</artifactId> <version>1.0-SNAPSHOT</version> <description>springboot-dubbo 客户端:整合 Dubbo/ZooKeeper</description> <!-- Spring Boot 启动父依赖 --> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.7.0</version> </parent> <dependencies> <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0</version> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- Spring Boot Dubbo 服务接口 依赖 --> <dependency> <groupId>org.example</groupId> <artifactId>springboot-dubbo-interf</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>
- 编写配置文件(application.properties)
## StringBoot 集成 Dubbo 相关配置信息:消费端配置示例 #注册服务名称 spring.dubbo.application.name=consumer #zookeeper 注册中心地址 spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 #服务所在包名称 spring.dubbo.scan=com.mk.demo #应用端口 server.port=8001 #应用项目名称 #server.context-path=/ #修改 tomcat 的 URIEncoding 为 UTF-8 server.tomcat.uri-encoding=UTF-8 #jackson 对日期时间格式化设置:时间格式 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss #jackson 对日期时间格式化设置:时区设置 spring.jackson.time-zone=GMT+8
- 编写接口(UserServiceConsumer)
package com.mk.demo.impl; import com.alibaba.dubbo.config.annotation.Reference; import com.mk.demo.service.UserService; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @Component public class UserServiceConsumer { @Reference private UserService userService; public String getUserId(Integer id) { return userService.getUserId(id); } }
- 编写控制层(UserController)
package com.mk.demo.controller; import com.mk.demo.impl.UserServiceConsumer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class UserController { @Resource private UserServiceConsumer userServiceConsumer;//自动注入上一步创建的服务消费类 /** * http://localhost:8001/getUserId?id=2 * 使用 dubbo 协议调用远程服务 * @param id * @return */ @RequestMapping("/getUserId") public Object getUserId(Integer id) { return userServiceConsumer.getUserId(id); } }
- 编写启动类(Application)
package com.mk.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
- 启动项目
- 访问( localhost:8001/getUserId?id=1 )
- 结果