springBoot+dubbo+zookeeper

一、前言

? ? ? ? ? ? 網上各種各樣的列子,大部分都是復制,粘貼!按說明操作完全搭建不起來,決定親自操作一把!@供大家學習參考。

1、環境簡紹

? ? centOS7、VMware15.0.0、zookeeper-3.4.10

1、安裝zookeeper,怎么下載就不說了,如果下載都不會,您嘞該轉行了!開個玩笑?。?/p>

xft客戶端將zookeeper-3.4.10.tar.gz上傳到/home/zookeeper中(看自己情況)

解壓tar -zxvf zookeeper-3.4.10.tar.gz

修改配置文件

cp ?/home/zookeeper/zookeeper-3.4.10/conf/zoo_sample.cfg ? ? /home/zookeeper /zookeeper-3.4.10/conf/zoo.cfg (其實就是將zoo_sample.cfg復制一份,名字修改為zoo.cfg)

設置 vim zoo.cfg

# The number of milliseconds of each tick

tickTime=2000

# The number of ticks that the initial

# synchronization phase can take

initLimit=10

# The number of ticks that can pass between

# sending a request and getting anacknowledgement

syncLimit=5

# the directory where the snapshot isstored.

# do not use /tmp for storage, /tmp here isjust

# example sakes.

dataDir=/opt/zookeeper/data

# the port at which the clients willconnect

clientPort=2181

# the maximum number of client connections.

# increase this if you need to handle moreclients

#maxClientCnxns=60

#

# Be sure to read the maintenance sectionof the

# administrator guide before turning onautopurge.

#

#http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance

#

# The number of snapshots to retain indataDir

#autopurge.snapRetainCount=3

# Purge task interval in hours

# Set to "0" to disable autopurge feature

#autopurge.purgeInterval=1

dataLogDir=/opt/zookeeper/dataLog

server.1=master:2888:3888

server.2=slave1:2888:3888

server.3=slave2:2888:3888

備注:黑體部分是我們需要配置的地方

dataDir 、dataLogDir創建出來

mkdir /opt/zookeeper/data

mkdir /opt/zookeeper/dataLog

?在/opt/zookeeper/data目錄下新建myid并設置server.1中.后面的數字

vim myid

1

:wq!

cat myid

1

將master slave1 slave2映射到host的文件中

vim /etc/host

192.168.6.10 master

192.168.6.11 slave1

192.168.6.12 slave2

:wq!

設置環境變量

vim /etc/profile

export ZOOKEEPER_HOME=/home/zookeeper/zookeeper-3.4.10

export PATH=$PATH:$ZOOKEEPER_HOME/bin

:wq!

設置開機啟動

在/etc/rc.d/init.d目錄下新建zookeeper,并編輯

vim zookeeper

#!/bin/bash

#chkconfig: 2345 10 90

#description: service zookeeper

export JAVA_HOME=/usr/java/jdk1.8.0_11

export ZOO_LOG_DIR=/opt/zookeeper/dataLog

ZOOKEEPER_HOME=/home/zookeeper/zookeeper-3.4.10

su root ${ZOOKEEPER_HOME}/bin/zkServer.sh? "$1"

:wq!

為新建的/etc/rc.d/init.d/zookeeper文件添加可執行權限,命令是:

chmod?+x ?/etc/rc.d/init.d/zookeeper

?把zookeeper這個腳本添加到開機啟動項里面,命令是:

chkconfig?--add ? zookeeper

如果想看看是否添加成功,命令是:

??????chkconfig? --list

查看2181端口是否啟用,執行命令:

? lsof? -i:2181

啟動zookeeper

cd /home/zookeeper/zookeeper-3.4.10/bin

./ zkServer.sh start

停止?./zkServer.shstop

查看狀態 ./zkServer.sh status

復制該機器上的zookeeper-3.4.10到其他服務器上(如果不想集群,下面繞過!)

cd /home/

scp -r zookeeper slave1:/home/

scp -r zookeeper slave2:/home/

slave1、slave2(slave1、slave2上面已經加入到hosts文件中了,否則你的用ip地址)做上面相同的配置

查看zookeeper啟動狀態

方法一

service??zookeeper? status

方法二

lsof ?-i:2181

方法三

netstat ? -lntup

客戶端連接

cd /home/zookeeper/zookeeper-3.4.10/bin/

./zkCli.sh -server 192.168.6.11:2181

二、上面環境準備好后就可以開發項目了

1、用IDEA開發(eclipse也是可以的,只是eclipse對xml提示太差?。?/p>

2、先看下項目結構maven項目,按結構創建好項目

sb_parent是maven父工程

3、配置pom.xml

sb_parent是maven父工程?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

? ? <groupId>com.hewanqiang

? ? <artifactId>sb_parent

? ? <packaging>pom

? ? <version>1.0-SNAPSHOT

? ? ? ? <module>spring-boot-dubbo-provider

? ? ? ? <module>spring-boot-dubbo-consumer

? ? ? ? ? ? ? ? <groupId>org.springframework.boot

? ? ? ? ? ? ? ? <artifactId>spring-boot-dependencies

? ? ? ? ? ? ? ? <version>2.0.6.RELEASE

? ? ? ? ? ? ? ? <type>pom

? ? ? ? ? ? ? ? <scope>import

? ? ? ? ? ? <groupId>org.springframework.boot

? ? ? ? ? ? <artifactId>spring-boot-starter-web

? ? ? ? ? ? <groupId>org.springframework.boot

? ? ? ? ? ? <artifactId>spring-boot-actuator

? ? ? ? ? ? <groupId>com.alibaba.boot

? ? ? ? ? ? <artifactId>dubbo-spring-boot-starter

? ? ? ? ? ? <version>0.2.0

? ? ? ? ? ? <groupId>org.apache.zookeeper

? ? ? ? ? ? <artifactId>zookeeper

? ? ? ? ? ? <version>3.4.9

? ? ? ? ? ? <groupId>com.github.sgroschupf

? ? ? ? ? ? <artifactId>zkclient

? ? ? ? ? ? <version>0.1

</project>

spring-boot-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">

? ? <parent>

? ? ? ? <artifactId>sb_parent</artifactId>

? ? ? ? <groupId>com.hewanqiang</groupId>

? ? ? ? <version>1.0-SNAPSHOT</version>

? ? </parent>

? ? <modelVersion>4.0.0</modelVersion>

? ? <artifactId>spring-boot-dubbo-provider</artifactId>

</project>

將spring-boot-dubbo-provider安裝到本地倉庫


spring-boot-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">

? ? <parent>

? ? ? ? <artifactId>sb_parent</artifactId>

? ? ? ? <groupId>com.hewanqiang</groupId>

? ? ? ? <version>1.0-SNAPSHOT</version>

? ? </parent>

? ? <modelVersion>4.0.0</modelVersion>

? ? <artifactId>spring-boot-dubbo-consumer</artifactId>

? ? <dependencies>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>com.hewanqiang</groupId>

? ? ? ? ? ? <artifactId>spring-boot-dubbo-provider</artifactId>

? ? ? ? ? ? <version>1.0-SNAPSHOT</version>

? ? ? ? </dependency>

? ? </dependencies>

</project>

spring-boot-dubbo-provider 項目類創建

IHelloService.java

HelloServiceImpl.java

DubboProvider.java


package com.alibaba.edas.boot;

public interface IHelloService {

StringsayHello(String str);

}

package com.alibaba.edas.boot;

import com.alibaba.dubbo.config.annotation.Service;

@Service //這里的 Service 注解是 Dubbo 提供的一個注解類,類的全名稱為

public class HelloServiceImplimplements IHelloService {

public StringsayHello(String name) {

return "Hello, " + name +" (from Dubbo with Spring Boot)";

? ? }

}

package com.alibaba.edas.boot;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class DubboProvider {

public static void main(String[] args) {

SpringApplication.run(DubboProvider.class,args);

? ? }

}


application.properties

# Base packages to scan Dubbo Components (e.g @Service , @Reference)

dubbo.scan.basePackages=com.alibaba.edas.boot

dubbo.application.name=dubbo-provider-demo

dubbo.registry.address=zookeeper://192.168.255.10:2181


spring-boot-dubbo-consumer 項目類創建


DemoConsumerController.java

package com.alibaba.edas.boot;

import com.alibaba.dubbo.config.annotation.Reference;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class DemoConsumerController

{

@Reference //alibaba的注解 前提是已經導入了 spring-boot-dubbo-provider的jar包(用maven的install首先將其安裝到本地倉庫)

? ? private IHelloServicedemoService;

? ? @RequestMapping("/sayHello/{name}")//REST風格

? ? public StringsayHello(@PathVariable String name)

{

return demoService.sayHello(name);

? ? }

}

DubboConsumer.java

package com.alibaba.edas.boot;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class DubboConsumer {

public static void main(String[] args) {

SpringApplication.run(DubboConsumer.class,args);

? ? }

}

application.properties

##server.port=8089 springBoot 同時啟動多個項目,需要修改端口

server.port=8089

dubbo.application.name=dubbo-consumer-demo

dubbo.registry.address=zookeeper://192.168.255.10:2181


運行 DubboProvider.java、DubboConsumer.java

登路dubbo管理控制臺(怎么安裝百度),下載對應.war扔進tomcat webapp下面就可以了,當然還需修改點東西@zookeeper注冊中心地址

http://192.168.255.10:8080/dubbo-admin

over!

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容