1.安装Zookeeper
第一步:安装 jdk(略)
第二步:把 zookeeper 的压缩包(zookeeper-3.4.6.tar.gz)上传到 linux 系统根路径
第三步:解压缩压缩包
tar -zxvf zookeeper-3.4.6.tar.gz
tar -zxvf zookeeper-3.4.6.tar.gz -C /usr/local/
第四步:进入zookeeper-3.4.6目录,创建data目录
mkdir data
[root@localhost /]# cd /usr/local
[root@localhost local]# ll
[root@localhost local]# cd zookeeper-3.4.6/
[root@localhost zookeeper-3.4.6]# ll
[root@localhost zookeeper-3.4.6]# mkdir data
第五步:进入conf目录 ,把zoo_sample.cfg 改名为zoo.cfg
cd conf
mv zoo_sample.cfg zoo.cfg
[root@localhost zookeeper-3.4.6]# cd conf
[root@localhost conf]# cp zoo_sample.cfg zoo.cfg
[root@localhost conf]# ll
第六步:打开zoo.cfg文件, 修改data属性:dataDir=/root/zookeeper-3.4.6/data
[root@localhost conf]# vim zoo.cfg
[root@localhost conf]# cd …
进入Zookeeper的bin目录,启动服务命令
./zkServer.sh start
[root@localhost zookeeper-3.4.6]# cd bin
[root@localhost bin]# ./zkServer.sh start
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/…/conf/zoo.cfg
Starting zookeeper … STARTED
停止服务命令
./zkServer.sh stop
[root@localhost bin]# ./zkServer.sh stop
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/…/conf/zoo.cfg
Stopping zookeeper … STOPPED
查看服务状态:
./zkServer.sh status
[root@localhost bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/…/conf/zoo.cfg
Mode: standalone
2.服务提供方开发
springmvc配置applicationContext-service.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--每个dubbo应用(服务提供方和服务消费方)都必须指定一个唯一的名称-->
<dubbo:application name="dubbodemo_provider"></dubbo:application>
<!--指定服务的注册中心-->
<dubbo:registry address="zookeeper://192.1.1.1(zookeeper):2181"></dubbo:registry>
<!--配置协议和端口-->
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<!--指定包扫描,用于发布dubbo服务-->
<dubbo:annotation package="com.learn.service.impl"></dubbo:annotation>
<!--数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="username" value="root" />
<property name="password" value="root" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务控制的注解支持-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>
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>
<artifactId>dubbodemo_provider</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>dubbodemo_provider Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>8081</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
serviceimpl
import com.alibaba.dubbo.config.annotation.Service;
import com.learn.service.HelloService;
import org.springframework.transaction.annotation.Transactional;
@Service(interfaceClass = HelloService.class,protocol = "dubbo") //发布服务必须使用Dubbo提供的Service注解
@Transactional
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "8081 hello " + name;
}
}
3.服务消费方开发
springmvc配置applicationContext-service.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<dubbo:application name="dubbodemo_consumer"></dubbo:application>
<dubbo:registry address="zookeeper://192.1.1.1:2181"></dubbo:registry>
<dubbo:annotation package="com.itheima.controller"></dubbo:annotation>
<dubbo:consumer check="false"></dubbo:consumer>
</beans>
Controller
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.learn.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/hello")
public class HelloController {
@Reference
private HelloService helloService;
@RequestMapping("/sayHello")
@ResponseBody
public String sayHello(String name){
return helloService.sayHello(name);
}
}