springboot的WEB开发笔记(dubbo与zookeeper)

分布式

将服务放于多台电脑,每台电脑之间都有联系,互相协作完成任务,形成分布式服务

RPC

远程过程调用,一台电脑调用另外一台电脑的服务或方法

RPC两个核心模块:通讯、序列化

apache-zookeeper-3.6.1-bin需要包名有bin的要不然在cmd运行zkServer.cmd会报错

dubbo-admin下载

https://github.com/apache/dubbo-admin/tree/master

打包成jar包有两种方法

①下载后可用cmd在dubbo-admin-master目录下进行打包

mvn clean package -Dmaven.test.skip=true

②导入idea的maven项目进行package打包,在目录target下

启动dubbo-admin

①首先要打开zookeeper的服务端:启动打开zkServer.cmd

②再执行target 下的dubbo-admin-0.0.1-SNAPSHOT.jar

java -jar dubbo-admin-0.0.1-SNAPSHOT.jar

③访问地址:http://localhost:7001/

初始的账号密码都为root

springboot整合dubbo,zookeeper

①导入依赖

<dependency>
    <groupId>org.apache.dubbogroupId>
    <artifactId>dubbo-spring-boot-starterartifactId>
    <version>2.7.7version>
dependency>
<dependency>
    <groupId>com.github.sgroschupfgroupId>
    <artifactId>zkclientartifactId>
    <version>0.1version>
dependency>
<dependency>
    <groupId>org.apache.curatorgroupId>
    <artifactId>curator-frameworkartifactId>
    <version>2.12.0version>
dependency>
<dependency>
    <groupId>org.apache.curatorgroupId>
    <artifactId>curator-recipesartifactId>
    <version>2.12.0version>
dependency>
<dependency>
    <groupId>org.apache.zookeepergroupId>
    <artifactId>zookeeperartifactId>
    <version>3.4.14version>
    
    <exclusions>
        <exclusion>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
        exclusion>
    exclusions>
dependency>

application.properties

#不同model端口不同
server.port=8001
#服务应用名字
dubbo.application.name=provider-server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#扫描的包
dubbo.scan.base-packages=com.wei.service

一个module调用另外一个module的方法

两个module都得有共同的接口

public interface TicketService {
    /**
     * 获取票
     * @return
     */
    public String getTicket();
}

然后一个是实现一个调用

实现类的@Service注意是org.apache.dubbo.config.annotation.Service

@Service
@Component
public class TicketServiceImpl implements TicketService{

    @Override
    public String getTicket() {
        return "取得票";
    }
}

调用类的是org.springframework.stereotype.Service,还需要对远程的接口对象进行注入@Reference

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Reference
    TicketService ticketService;
    public void getTicket(){
        String ticket = ticketService.getTicket();
        System.out.println(ticket);
    }
}

你可能感兴趣的:(java,zookeeper,分布式,spring,boot)