centos7安装zookeeper步骤

创建zookeeper服务器文件夹

mkdir -p /usr/local/services/zookeeper

切换

cd /usr/local/services/zookeeper

下载

wget wget https://downloads.apache.org/zookeeper/stable/apache-zookeeper-3.5.7-bin.tar.gz(注意下载版本)

解压缩

tar zxvf apache-zookeeper-3.5.7-bin.tar.gz

进入配置文件夹

cd apache-zookeeper-3.5.7-bin/conf

重命名

mv zoo_sample.cfg zoo.cfg

编辑配置zookeeper配置信息

vim zoo.cfg
tickTime=30000
# 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 an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/services/zookeeper/zookeeper-3.5.7/data
dataLogDir=/usr/local/services/zookeeper/zookeeper-3.5.7/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

编辑环境变量文件,在其尾部追加如下内容

vim /etc/profile
export ZOOKEEPER_HOME=/usr/local/services/zookeeper/apache-zookeeper-3.5.7-bin/
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH

编译环境变量文件

source /etc/profile

启动zkServer

[root@VM_0_8_centos bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/apache-zookeeper-3.5.7-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

查看状态zkServer启动状态

[root@VM_0_8_centos apache-zookeeper-3.5.7-bin]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/apache-zookeeper-3.5.7-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone

生产者

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 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.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.springboot</groupId>
	<artifactId>ticket</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>ticket</name>
	<description>Demo project for Spring Boot</description>
 
	<properties>
		<java.version>1.8</java.version>
	</properties>
 
	<dependencies>
		<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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
 
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.7.5</version>
		</dependency>
 
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>2.13.0</version>
		</dependency>
 
	</dependencies>
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
 
</project>

application.properties

dubbo.scan.base-packages=com.springboot.ticket.service
dubbo.application.name=ticket
dubbo.registry.address=zookeeper://111.230.6.126:2181

主入口类

package com.springboot.ticket;
 
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@EnableDubbo  // 启用Dubbo注解
@SpringBootApplication
public class TicketApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(TicketApplication.class, args);
	}
 
}

服务接口

package com.springboot.ticket.service;
 
public interface TicketService {
    public String getTicket();
}

实现发布服务接口类

package com.springboot.ticket.service;
import org.apache.dubbo.config.annotation.Service;
 
@Service  // 将服务发布出去
public class TicketServiceImpl implements TicketService{
    @Override
    public String getTicket() {
        return "《廊桥遗梦》";
    }
}

用户端
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 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.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.springboot</groupId>
	<artifactId>user</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>user</name>
	<description>Demo project for Spring Boot</description>
 
	<properties>
		<java.version>1.8</java.version>
	</properties>
 
	<dependencies>
		<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>
			<!--<exclusions>-->
				<!--<exclusion>-->
					<!--<groupId>org.junit.vintage</groupId>-->
					<!--<artifactId>junit-vintage-engine</artifactId>-->
				<!--</exclusion>-->
			<!--</exclusions>-->
		</dependency>
 
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.7.5</version>
		</dependency>
 
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>2.13.0</version>
		</dependency>
 
	</dependencies>
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
 
</project>

application.properties

dubbo.application.name=user
dubbo.registry.address=zookeeper://111.230.6.126:2181

主入口类

package com.springboot.user;
 
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@EnableDubbo
@SpringBootApplication
public class UserApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(UserApplication.class, args);
	}
 
}

复制服务接口

package com.springboot.ticket.service;
 
public interface TicketService {
    public String getTicket();
}

调用服务端服务

package com.springboot.user.service;
 
import com.springboot.ticket.service.TicketService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
 
@Service
public class UserService {
 
    //  指明用户端使用的是服务端的什么服务
    @Reference
    private TicketService ticketService;
 
    public String hello(){
        return ticketService.getTicket();
    }
 
}

单元测试

package com.springboot.user;
 
import com.springboot.user.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest
class UserApplicationTests {
 
	@Autowired
	UserService userService;
 
	@Test
	void contextLoads() {
		System.out.println(userService.hello());
	}
 
}

输出结果

2020-03-20 20:38:18.745  INFO 36548 --- [           main] c.springboot.user.UserApplicationTests   : Started UserApplicationTests in 10.448 seconds (JVM running for 12.644)
《廊桥遗梦》
2020-03-20 20:38:19.417  INFO 36548 --- [extShutdownHook] .b.c.e.AwaitingNonWebApplicationListener :  [Dubbo] Current Spring Boot Application is about to shutdown...

报错java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory

引入依赖
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-framework</artifactId>
	<version>4.2.0</version>
</dependency>

报错:The bean ‘dubboConfigConfiguration.Single’ could not be registered. A bean with that name has already been defined and overriding is disabled.
解决方案:spring.main.allow-bean-definition-overriding=true

发表评论

邮箱地址不会被公开。 必填项已用*标注