dubbo实例-使用注册中心Zookeeper
Dubbo实例-使用注册中心Zookeeper——启动Windows上的zookeeper服务~相关配置
1. 易出错
1.1 对象序列化
分布式开发要求所有的对象都要在网络中进行传输,
网络中传输要求这些对象序列化。
所以在分布式开发中我们的对象
都要序列化
操作。
1.2 修改Zookeeper注册中心的配置文件
zookeeper在启动的时候,内部还会启动另外一个程序他默认所占用的端口是8080
admin.serverPort=8888
不配置的话,默认启动端口号8080,8080端口容易被占用,这里修改为8888端口
dubbo服务化最佳实战:https://blog.csdn.net/qq_45896330/article/details/125711245
2. 使用注册中心和不使用的区别
- 引入依赖
<!--zookeeper依赖-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
- dubbo核心配置文件
服务提供端
服务消费端
3. 使用注册中心
3.1 接口工程 maven java
maven java工程:存放实体Bean
(必须要进行序例化操作)和业务接口
dubbo服务化最佳实战:https://blog.csdn.net/qq_45896330/article/details/125711245
3.2 服务提供者 maven web
- 配置相关依赖(spring,dubbo,接口工程,注册中心,编译插件)
- 实现业务接口(dubbo-zk-userservice-provider.xml)
- 核心配置文件
- 监听器
- tomcat
3.2.1. 配置相关依赖
spring,dubbo,接口工程,注册中心,编译插件
pom引入相关依赖
<?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.guo</groupId>
<artifactId>02-zk-userservice-provide</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!--Spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<!--Dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--添加接口工程-->
<dependency>
<groupId>com.guo</groupId>
<artifactId>01-zk-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--zookeeper依赖-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--JDK1.8编译插件-->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.2.2. 实现业务接口
package com.guo.dubbo.service.impl;
import com.guo.dubbo.model.User;
import com.guo.dubbo.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public User queryUserById(int id){
User user = new User();
user.setId(id);
user.setName("zhangshan");
return user;
}
@Override
public int Count() {
return 28;
}
}
3.2.3. 核心配置文件
src/main/resources/dubbo-zk-userservice-provider.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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--声明dubbo服务提供者的名称:保证唯一性-->
<dubbo:application name="002-zk-userservice-provider"/>
<!--声明dubbo使用的协议名称和端口号-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--使用zookeeper注册中心-->
<!--指定注册中心和端口号-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--暴露服务接口-->
<dubbo:service interface="com.guo.dubbo.service.UserService" ref="userServiceImpl"/>
<!--加载接口实现类-->
<bean id="userServiceImpl" class="com.guo.dubbo.service.impl.UserServiceImpl"/>
</beans>
3.2.4 监听器
src/main/webapp/WEB-INF/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:dubbo-zk-userservice-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.2.5. tomcat
3.3 服务消费者 maven web
- 配置相关依赖(spring,dubbo,接口工程,注册中心,编译插件)
- 核心配置文件(dubbo-zk-consumer.xml、appcationContext.xml)
- 中央调度器Servlet(web.xml 用于调用页面)
- 创建控制层(调用远程接口服务)
- 创建页面
- tomcat
3.3.1 配置相关依赖
spring,dubbo,接口工程,注册中心,编译插件
<?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.guo</groupId>
<artifactId>03-zk-userservice-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!--Spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--接口工程-->
<dependency>
<groupId>com.guo</groupId>
<artifactId>01-zk-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--zookeeper依赖-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.3.2 核心配置文件
(dubbo-zk-consumer.xml、appcationContext.xml)
dubbo-zk-consumer.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="03-zk-consumer"/>
<!--指定注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--引用远程接口服务-->
<dubbo:reference id="userService" interface="com.guo.dubbo.service.UserService"/>
</beans>
appcationContext.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!--扫描组件-->
<context:component-scan base-package="com.guo.dubbo.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.3.3 中央调度器Servlet(web.xml 用于调用页面)
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">
<!--中央调度器-->
<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:appcationContext.xml,classpath:dubbo-zk-consumer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.3.4 创建控制层
(调用远程接口服务)
com/guo/dubbo/controller/UserController.java
package com.guo.dubbo.controller;
import com.guo.dubbo.model.User;
import com.guo.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/userquery")
public String queryUserById(Model model,int id){
User user = userService.queryUserById(id);
model.addAttribute("user",user);
return "user";
}
}
3.3.5 创建页面
src/main/webapp/user.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户详情</title>
</head>
<body>
<h1>用户详情</h1>
<div>用户编号:${user.id}</div>
<div>用户姓名:${user.name}</div>
</body>
</html>
3.3.6 tomcat
4. 运行结果
-
这里用到的是本地的zookeeper,需要先把zookeeper服务跑起来
注册中心-Zookeeper~Windows下载安装:https://blog.csdn.net/qq_45896330/article/details/125732093
-
然后把服务提供者和服务消费者服务都跑起来
-
打开浏览器访问