Zookeeper的作用:管理服务提供者和服务消费者,服务提供者将服务在Zookeeper注册,服务消费者从Zookeeper获取服务。

1、 注册中心工作方式

在这里插入图片描述

2、从Zookeeper官网下载Zookeeper

在这里插入图片描述
对Zookeeper压缩包解压
在这里插入图片描述
配置 Zookeeper
1、修改 zookeeper-3.8.0-bin/conf/目录下配置文件,复制 zoo-sample.cfg 改名为 zoo.cfg。
在这里插入图片描述
2、修改zoo.conf
dataDir : zookeeper 数据的存放目录
admin.serverPort=8888
原因:zookeeper 3.8.0 内部默认会启动一个应用服务器,默认占用
8080 端口
在这里插入图片描述
启动zk,双击zkServer.cmd
在这里插入图片描述
启动成功
在这里插入图片描述

3、创建maven项目

项目结构:
dubbo-commons:公共接口(普通maven项目)
dubbo-service:服务提供者(web项目)
dubbo-web:服务消费者(web项目)
在这里插入图片描述

3.1、创建dubbo-common(公共接口)

创建产品实体类:Product.java

package com.wen.model;

import java.io.Serializable;

public class Product implements Serializable {
    String id;
    String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

创建产品服务接口:ProductService

package com.wen.service;

import com.wen.model.Product;

public interface ProductService {
    Product getProductById(String id);
}

3.2、创建服务提供者项目

dubbo-service(web项目)

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>  
  <groupId>com.wen</groupId>  
  <artifactId>dubbo-service</artifactId>  
  <version>1.0-SNAPSHOT</version>  
  <packaging>war</packaging>
  <dependencies> 
    <!--引入公共工程-->  
    <dependency> 
      <groupId>com.wen</groupId>  
      <artifactId>dubbo-commons</artifactId>  
      <version>1.0-SNAPSHOT</version> 
    </dependency>  
    <!--spring-->  
    <dependency> 
      <groupId>org.springframework</groupId>  
      <artifactId>spring-webmvc</artifactId>  
      <version>5.3.18</version> 
    </dependency>  
    <!--dubbo-->  
    <dependency> 
      <groupId>com.alibaba</groupId>  
      <artifactId>dubbo</artifactId>  
      <version>2.6.5</version> 
    </dependency>  
    <!-- zookeeper 客户端依赖 -->  
    <dependency> 
      <groupId>org.apache.curator</groupId>  
      <artifactId>curator-framework</artifactId>  
      <version>4.1.0</version> 
    </dependency> 
  </dependencies> 
</project>

新建产品服务接口实现类:ProductServiceImpl.java

package com.wen.service.impl;

import com.wen.model.Product;
import com.wen.service.ProductService;

public class ProductServiceImpl implements ProductService {
    @Override
    public Product getProductById(String id) {
        Product product = new Product();
        product.setId(id);
        product.setName("iphone 13");
        return product;
    }
}

创建dubbo的配置文件:applicationContext.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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!--dubbo服务名称-->
    <dubbo:application name="zk-productService"/>

    <!--使用注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <dubbo:protocol name="dubbo" port="20881"/>
    <!--暴露服务-->
    <dubbo:service interface="com.wen.service.ProductService" ref="productService" version="1.0.0"/>
    <dubbo:service interface="com.wen.service.ProductService" ref="productService2" version="2.0.0"/>

    <!--实例化bean-->
    <bean id="productService" class="com.wen.service.impl.ProductServiceImpl"/>
    <bean id="productService2" class="com.wen.service.impl.ProductServiceImpl2"/>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	version="4.0">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


</web-app>

3.3、创建服务消费者项目

dubbo-web(web项目)

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>  
  <groupId>com.wen</groupId>  
  <artifactId>dubbo-web</artifactId>  
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <!--引入公共工程-->
    <dependency>
      <groupId>com.wen</groupId>
      <artifactId>dubbo-commons</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--spring-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.18</version>
    </dependency>
    <!--dubbo-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.5</version>
    </dependency>
    <!-- zookeeper 客户端依赖 -->
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.1.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.13.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
  </dependencies>
</project>

创建控制器:ProductController.java

package com.wen.web;

import com.wen.model.Product;
import com.wen.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SuppressWarnings("all")
@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("{id}")
    @ResponseBody
    public Product getProduct(@PathVariable String id){
        System.out.println(productService);
        return productService.getProductById(id);
    }
}

创建dubbo的配置文件:applicationContext.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: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/context https://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.wen.web"/>
    <!--解析json 参数封装-->
    <mvc:annotation-driven/>

    <dubbo:application name="web"/>

    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--订阅服务,不需要指定url,直接找注册中心订阅interface的实例-->
    <dubbo:reference id="productService" interface="com.wen.service.ProductService" version="1.0.0"/>
    <dubbo:reference id="productService2" interface="com.wen.service.ProductService" version="2.0.0"/>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
		 version="4.0">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application.xml</param-value>
	</context-param>
	
	<!--SpringMVC的核心处理器-->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!--初始化参数:配置文件所在位置-->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:application.xml</param-value>
		</init-param>
<!--		启动时初始化实例,默认是第一次访问时才进行初始化-->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

3.4、测试

启动Zookeeper–启动服务提供者–启动服务消费者
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐