SpringBoot+Dubbo+ZooKeeper+Maven入门实践
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11798626.html
注*** 本实例为仅适合初学者,关于dubbo和springboot以及各种工具的功能介绍就免了,读者可以自行百度,这里仅示范操作.
准备工具(本人所使用工具和版本):
JDK1.8 + tomcat8.5 + springboot 2.2.0.RELEASE + zookeeper-3.4.14 + dubbo-admin-2.5.8 + Lombok插件(idea中搜索) + maven3.6.1
----------------分割线-----------------------------------------
开始!!!
1. 创建一个maven主项目oldhen, 目录结构和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.doubleyolk</groupId> <artifactId>oldhen</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>Double Yolk</name> <description>A Springboot with Dubbo Project</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> </parent> <modules> <module>model</module> <module>common</module> <module>provider</module> <module>consumer</module> <module>api</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <dubbo.version>2.5.5</dubbo.version> <zkclient.version>0.10</zkclient.version> <lombok.version>1.18.8</lombok.version> <spring-boot.version>2.2.0.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring-boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 使用lombok实现JavaBean的get、set、toString、hashCode、equals等方法的自动生成 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <!-- Dubbo依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <!--zookeeper的客户端依赖--> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
创建完之后配置好pom.xml之后什么都不用管,下一步.
2. 创建model子模块, 目录结构和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.doubleyolk</groupId> <artifactId>model</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>model</name> <description>Model module of oldhen</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.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> <dubbo.version>2.5.5</dubbo.version> <zkclient.version>0.10</zkclient.version> <lombok.version>1.18.8</lombok.version> <spring-boot.version>2.2.0.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 使用lombok实现JavaBean的get、set、toString、hashCode、equals等方法的自动生成 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> </dependencies> </project>
Test实体类如下:
package com.oldhen.model.test; import lombok.Data; import java.io.Serializable; /** * TODO * TEST 实体 * 使用lombok注解免去set/get,编译的时候会自动写进字节码文件 */ @Data public class Test implements Serializable { private String id; private String name; }
配置文件可以不需要.
3. 创建common子模块, 目录结构和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"> <groupId>com.doubleyolk</groupId> <artifactId>common</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <modelVersion>4.0.0</modelVersion> <name>common</name> <description>Common module of oldhen</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.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> <dubbo.version>2.5.5</dubbo.version> <zkclient.version>0.10</zkclient.version> <lombok.version>1.18.8</lombok.version> <spring-boot.version>2.2.0.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>com.doubleyolk</groupId> <artifactId>model</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
TestUtil工具类如下:
package com.oldhen.common.test; import com.oldhen.model.test.Test; /** * 测试工具类 */ public class TestUtil { private TestUtil(){} public static void Test(){ Test test = new Test(); } }
4. 创建api子模块, 目录结构和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.doubleyolk</groupId> <artifactId>api</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>api</name> <description>Api module of oldhen</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.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> <dubbo.version>2.5.5</dubbo.version> <zkclient.version>0.10</zkclient.version> <lombok.version>1.18.8</lombok.version> <spring-boot.version>2.2.0.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>com.doubleyolk</groupId> <artifactId>model</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.doubleyolk</groupId> <artifactId>common</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </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>
5. 创建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>com.doubleyolk</groupId> <artifactId>consumer</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>consumer</name> <description>Consumer module of oldhen</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.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> <zkclient.version>0.10</zkclient.version> <dubbo.version>2.5.5</dubbo.version> </properties> <dependencies> <dependency> <groupId>com.doubleyolk</groupId> <artifactId>api</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.doubleyolk</groupId> <artifactId>model</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.doubleyolk</groupId> <artifactId>common</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> </dependency> <!--zookeeper的客户端依赖--> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <!-- alibaba的druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
TestConsumer类如下:
package com.oldhen.consumer.test; import com.oldhen.api.test.TestApi; import com.oldhen.model.test.Test; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.List; /** * TODO * Test consumer 层 */ @RestController public class TestConsumer { @Resource TestApi testApi; @GetMapping("/test") public List<Test> findAllTest(HttpServletRequest request){ String param = request.getParameter("param"); System.out.println("********================****************"); System.out.println("it's comming! "); List<Test> tests = testApi.findAll(); System.out.println("********================****************"); return tests; } }
启动类如下:
package com.oldhen.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; /** * 启动类 */ @ImportResource({"classpath:spring-dubbo-consumer.xml"}) @SpringBootApplication(scanBasePackages = "com.oldhen.consumer.test") public class ConsumerApplication { public static void main(String[] args) throws InterruptedException { SpringApplication.run(ConsumerApplication.class, args); } }
配置文件application.yml和消费者dubbo配置文件如下:
server: port: 9093 spring: datasource: name: mysql url: jdbc:mysql://127.0.0.1:3306/oldhen username: root password: root # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- dubbo服务名称,需要唯一指定 --> <dubbo:application name="consumer"/> <!-- dubbo注册在zookeeper上,通过zookeeper实现服务的注册和发布以及订阅 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 使用dubbo协议和端口 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 此处的id要和提供者那边的一致,接口使用api提供的接口即可,避免与提供者代码耦合--> <dubbo:reference id="testApi" check="false" interface="com.oldhen.api.test.TestApi"/> <!-- dubbo注解扫描路径 --> <dubbo:annotation package="com.oldhen.consumer.test.**"/> </beans>
6. 创建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>com.doubleyolk</groupId> <artifactId>provider</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>provider</name> <description>Provider module of oldhen</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.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> <zkclient.version>0.10</zkclient.version> <dubbo.version>2.5.5</dubbo.version> </properties> <dependencies> <dependency> <groupId>com.doubleyolk</groupId> <artifactId>model</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.doubleyolk</groupId> <artifactId>api</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.doubleyolk</groupId> <artifactId>common</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> </dependency> <!--zookeeper的客户端依赖--> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <!-- alibaba的druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
业务层代码:
package com.oldhen.provider.test.biz; import com.alibaba.dubbo.config.annotation.Service; import com.oldhen.api.test.TestApi; import com.oldhen.model.test.Test; import com.oldhen.provider.test.TestProvider; import javax.annotation.Resource; import java.util.List; /** * 业务层 */ @Service public class TestApiBiz implements TestApi { @Resource TestProvider testProvider; @Override public List<Test> findAll() { // do something return testProvider.findAll(); } }
建库建表语句和dao层代码:
CREATE DATABASE IF NOT EXISTS oldhen; CREATE TABLE oldhen_model_test ( id INT NOT NULL PRIMARY KEY, NAME VARCHAR ( 20 ) NOT NULL ) ENGINE = INNODB DEFAULT charset = utf8; INSERT INTO oldhen_model_test VALUES ( 1, "one" ); INSERT INTO oldhen_model_test VALUES ( 2, "two" ); INSERT INTO oldhen_model_test VALUES ( 3, "three" ); INSERT INTO oldhen_model_test VALUES ( 4, "four" );
package com.oldhen.provider.test.dao; import com.oldhen.model.test.Test; import org.springframework.stereotype.Component; import java.util.List; /** * TODO * Test dao数据访问层 */ @Component public interface TestDao { List<Test> findAllTest(); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.oldhen.provider.test.dao.TestDao"> <resultMap id="test" type="com.oldhen.model.test.Test" > <!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id --> <!-- property:主键在pojo中的属性名 --> <!-- column:主键在数据库中的列名 --> <id property="id" column="id" /> <!-- 定义普通属性 --> <result property="name" column="name" /> </resultMap> <select id="findAllTest" resultType="com.oldhen.model.test.Test"> select mt.* from oldhen_model_test mt </select> </mapper>
service层代码:
package com.oldhen.provider.test; import com.oldhen.model.test.Test; import java.util.List; /** * TODO * Test provider 服务层 */ public interface TestProvider { List<Test> findAll(); }
package com.oldhen.provider.test.impl; import com.alibaba.dubbo.config.annotation.Service; import com.oldhen.model.test.Test; import com.oldhen.provider.test.TestProvider; import com.oldhen.provider.test.dao.TestDao; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; /** * 服务实现类 */ @Service public class TestProviderImpl implements TestProvider { @Resource TestDao dao; @Override public List<Test> findAll() { Test test = new Test(); test.setId("1"); test.setName("hello"); List<Test> tests = new ArrayList<>(); tests.add(test); return dao.findAllTest(); } }
启动类和配置文件:
package com.oldhen.provider; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; /** * 启动类 * MapperScan扫描mapperxml文件 */ @SpringBootApplication @MapperScan("com.oldhen.provider.test.dao") @ImportResource({"classpath:spring-dubbo-provider.xml"}) public class ProviderApplication { public static void main(String[] args) throws InterruptedException { SpringApplication.run(ProviderApplication.class, args); /** * 防止提供者程序中断或停止导致消费者无法调用 * 如果提供者主程序跑完了zookeeper也会中断(原因未知) */ synchronized (ProviderApplication.class){ while (true){ ProviderApplication.class.wait(); } } } }
server: port: 9099 spring: datasource: name: mysql url: jdbc:mysql://127.0.0.1:3306/oldhen?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC username: root password: root # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 mybatis: # type-aliases-package: domain # 指定全局配置文件位置 # config-location: classpath:mybatis/mybatis-config.xml # 指定sql映射文件位置 mapper-locations: classpath:mapper/*.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- dubbo服务名称,需要唯一指定 --> <dubbo:application name="provider"/> <!-- zookeeper注册中心的ip地址 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 --> <dubbo:annotation package="com.oldhen.provider.test.impl.**" /> <!-- 使用dubbo协议并且将服务发布在20880端口 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 定义实际的api实现类, 具体的功能实现都在在这里,此处的id要和消费者那边的一致 --> <bean id="testProviderApiBiz" class="com.oldhen.provider.test.biz.TestApiBiz"/> <!-- 发布api接口服务, 实际引用的实现类时上面那个,消费者只要调用这个接口就能直接调用实现类的方法和功能 --> <dubbo:service interface="com.oldhen.api.test.TestApi" ref="testProviderApiBiz"/> </beans>
7. Zookeeper的配置文件:
tickTime=2000 initLimit=10 syncLimit=5 #文件存储路径,如果是linux则修改为/tmp或者对应的路径 dataDir=D:/tmp/zookeeper #服务端口,和java中的配置一致即可 clientPort=2181
至此,准备工作已经完毕了!开始运行程序!
a) 执行zookeeper-3.4.14\bin目录下的zkServer.cmd, 本人是win系统,linux运行zkServer.sh即可;将dubbo-admin源码下载下来之后,执行mvn compile, 再执行mvn package打包,然后将打出来的war包放入tomcat中运行即可,此处不作过多讲解.
b) 数据库开起来, 执行sql文件创建数据库和数据表和插入数据;
c) 首先运行提供者的启动类;
d) 其次运行消费者的启动类;
e) 通过访问浏览器 http://192.168.137.1:9093/test/ 来查看是否返回如下数据:
[{"id":"1","name":"one"},{"id":"2","name":"two"},{"id":"3","name":"three"},{"id":"4","name":"four"}]
f) 有的话代表成功了! 没有的话请检查上述工具的版本是否对应!以及是否启动成功!
g) 具体的服务是否启动可以登录 http://127.0.0.1:9090/dubbo-admin-2.5.8 输入用户名root密码root进行登录查看
查看相应的服务是否启动即可!没有启动的话请检查环境(jdk版本, Zookeeper版本, dubbo-admin版本)是否正确!缺一不可,请不要随意改动pom文件,否则也可能出现各种兼容性问题导致程序无法运行!
总结: 整体的思路就是消费者通过实现api层提供的接口来访问接口里面的方法, 这其中消费者不需要关注其他事情,只需要将自己和Zookeeper绑定起来获取Zookeeper上发布的提供者服务即可, 提供者也是通过实现api层提供的接口来实现相应的对数据库访问操作,从而返回数据给消费者,这其中它也不需要关注其他事情,只需要将自己的服务注册发布到Zookeeper上由Zookeeper来给别的服务提供服务,提供者只需要关注自己的业务实现即可,当然model层和common层独立出来可以供消费者和提供者以及api层共同使用,无需重复写JavaBean.
附上笔记图:
- oldhen 主项目 - model 实体层(提供一般的实体类编写,采用lombok来实现去set/get化) - common 通用层(用来提供工具类或者通用的一些组件等功能) - api 接口层(主要是用来解耦consumer和provider, 消费者和提供者同时实现api中的接口,提供者进行发布该服务给消费者获取使用) - consumer 消费层(调用provider, 可以理解为普通的controller层) - provider 最终提供服务层(为consumer提供服务, 主要是集成了service,mapper/dao 层以及biz(对service进行包装和业务处理的业务层))
provider可以有多个例如 : 订单服务, 用户管理服务, 商品管理服务, 抢购服务, 购物车服务, 支付服务, 评论服务, 会员服务, 搜索服务, 秒杀服务等等模块
consumer也可以针对不同的服务增加不同的消费者,例如: 订单服务消费者, 用户管理服务消费者, 商品管理服务消费者等等.
整体架构示意如下:
前端(网站/APP) --> dubbo --> 服务层(privider中的各种服务, 通过consumer调用)
(并列于服务层的下一级)--> 数据访问层 --> 关系型数据库管理器(mycat数据库分片) --> 多个关系型数据库(多个mysql)
(并列于服务层的下一级)--> 缓存(Redis集群) --> 多个缓存服务(多个redis)
(并列于服务层的下一级)--> MQ消息队列 --> 通用支撑系统(推送等)
(并列于服务层的下一级)--> 搜索服务集群 --> 多个搜索服务
管理后台(网站) --> dubbo --> 服务层(privider中的各种服务, 通过consumer调用)
(并列于服务层的下一级)--> 数据访问层 --> 关系型数据库管理器(mycat数据库分片) --> 多个关系型数据库(多个mysql)
(并列于服务层的下一级)--> 缓存(Redis集群) --> 多个缓存服务(多个redis)
(并列于服务层的下一级)--> MQ消息队列 --> 通用支撑系统(推送等)
(并列于服务层的下一级)--> 搜索服务集群 --> 多个搜索服
有问题可以在下方留言,可以贴上错误代码!