SpringBoot+Dubbo+Zookeeper搭建分布式项目练习

本文介绍了一个使用SpringBoot、Dubbo和Zookeeper搭建的分布式项目练习,目标是创建一个分布式博客系统。通过创建SpringBoot模块、配置pom文件、编写接口和服务,最终实现服务在Zookeeper上的注册与客户端调用。文中详细阐述了每个步骤,包括接口定义、提供者和消费者的编写以及测试过程。

SpringBoot+Dubbo+Zookeeper分布式项目练习

——java博客系统

由于一台服务器的能力有限,所以越来越多的大型项目采用分布式的布置方式,多台服务器共同去完成一个大型的集成服务。对分布式的练习还是很有必要的。

这个项目练习主要是使用SpringBoot、Dubbo和Zookeeper来完成一个分布式的项目练习,目标是多个服务器提供一套服务的多个模块,客户端通过网页进行数据的读取等功能。本项目是个人设计的一个练习项目,不可能一蹴而就的,需要多次的迭代,每次增加一些小功能,最后完成一个大项目。本文主要是项目的搭建和简单测试。
后续有很多都是这个系列的,感兴趣的朋友可以看看我后面的文章。

前提知识:

(现在或将来会用到)
javase、maven、SpringBoot(最好也要会Spring、SpringMVC、javaweb)、Mysql、Dubbo、Zookeeper、RPC等分布式技术和概念(基本就行,因为我也会的不是很深)、Redis…

使用工具:

idea、zookeeper3.6.2(请注意版本问题,如果这里的版本和maven版本不一致可能会有问题)、Redis-x64-3.2.100

步骤:

1.创建一个SpringBoot的父项目,并创建几个SpringBoot模块

创建时将所有模块的组名设置为同一个(如果模块创建后无法识别为maven项目,需要手动为其添加maven模块支持)
在这里插入图片描述

2.配置porm文件

这一步十分的关键,涉及到关键的依赖,而且要是porm文件配不好,各个模块可能无法相互引用。

父依赖:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.zhz</groupId>
    <artifactId>bigprc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>bigprc</name>
    <description>Demo project for Spring Boot</description>
    <!--pom打包模式-->
    <packaging>pom</packaging>
    
    <properties>
        <java.version>11</java.version>
    </properties>

    <modules>
        <module>f-api</module>
        <module>f-client</module>
        <module>f-provider</module>
        <module>f-common</module>
    </modules>

    <dependencies>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

子依赖:(以f-client为例)

这里有两个依赖需要注意,一个是f-api,一个是f-common-coredependence(在f-common下),这两个都是自定义的模块,f-api中定义了接口,f-common-coredependence中设置了关键的依赖。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<groupId>com.zhz</groupId>

	<artifactId>f-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>f-client</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.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>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.big</groupId>
			<artifactId>f-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.zhz</groupId>
			<artifactId>f-common-coredependence</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
f-common-coredependence:

mybatis暂时不用,但是如果不注销的话就必须要配置,否则会报错

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.zhz</groupId>
	<artifactId>f-common-coredependence</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>f-common-coredependence</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.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>
		</dependency>
		<!--        &lt;!&ndash;mybatis依赖&ndash;&gt;-->
		<!--        <dependency>-->
		<!--            <groupId>org.mybatis.spring.boot</groupId>-->
		<!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
		<!--            <version>2.0.1</version>-->
		<!--        </dependency>-->

		<!--导入Dubbo+zookeeper-->
		<!-- Dubbo Spring Boot Starter -->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.7.8</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>

		<!-- 引入zookeeper -->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>5.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>5.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.6.2</version>
			<!--排除这个slf4j-log4j12-->
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!--日志打印-->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.18</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

3.编写接口

在这里插入图片描述
接口供提供者和客户使用。
两段分别为

package com.zhz.f.api.service;
public interface HelloFriend {
    String hello();
}
===============================
package com.zhz.f.api.service;
public interface HelloWorld {
    String helloWorld();
}

4.编写提供者

在这里插入图片描述
服务提供者需要将服务注册到zookeeper中,在Server中编写:
(FProviderApplication不用修改)

package com.zhz.f.provider.service;

import com.zhz.f.api.service.HelloWorld;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

//将服务注册到zookeeper
@DubboService
//将服务注册到Spring
@Service
public class Server implements HelloWorld {
    @Override
    public String helloWorld() {
        return "你好世界";
    }
}

同时也需要配置端口号等参数:在application.yaml中配置:

server:
  port: 8081
  tomcat:
    uri-encoding: utf-8
spring:
  application:
    name: spring-provide-server1
dubbo:
  application:
    name: provide-server1
  registry:
    address: zookeeper://localhost:2181 # 注册中心地址
  provider:
    port: -1 # 防止多服务用一个端口造成冲突
  scan:
    base-packages: com.zhz.f.provider.service # 扫描要被注册的服务

4.编写客户

在这里插入图片描述

FClientApplication:

package com.zhz.f.client;

import com.zhz.f.client.hello.Hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FClientApplication implements CommandLineRunner {
	@Autowired
	private Hello hello;

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

	@Override
	//服务启动后运行的程序
	public void run(String... args) throws Exception {
		hello.sayHelloWorld();
		hello.sayHelloFriend();
	}
}

Hello:

package com.zhz.f.client.hello;

import com.zhz.f.api.service.HelloFriend;
import com.zhz.f.api.service.HelloWorld;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

@Component
public class Hello {
	//得到注册中心中的服务
    @DubboReference
    private HelloWorld helloWorld;
    @DubboReference
    private HelloFriend helloFriend;

    public void sayHelloWorld(){
        System.out.println(helloWorld.helloWorld());
    }
    public void sayHelloFriend(){
        System.out.println(helloFriend.hello());
    }
}

5.运行测试

先打开zookeeper的服务器:
在这里插入图片描述等待其启动完毕后,运行两个服务提供者的springboot启动器。
服务启动后,会将服务注册到zookeeper中。
这时启动客户端,可以获得预期结果。
在这里插入图片描述至此,服务的搭建和测试完成,基本可以实现利用zookeeper和dubbo分布式提供服务,客户端从zookeeper中提取服务的功能。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值