Dubbo实例~直连的方式
- 2.4 Dubbo实例~直连的方式
- 2.4.1 服务的提供者
- 2.4.2 服务的消费者
- 2.4.1 服务的提供者、消费者两个服务跑起来
2.4 Dubbo实例~直连的方式
提供者和消费者直接连接,不需要注册中心
2.4.1 服务的提供者
步骤:
1.创建一个maven web工程:服务的提供者
2.创建一个实体bean查询的结果
3.提供一个服务接口:xxXX
4.实现这个服务接口:xxxxImpl
5.配置dubbo服务提供者的核心配置文件
a. 声明dubbo服务提供者的名称:保证唯一
b. 声明dubbo使用的协议和端口号
c. 暴露服务,使用直连方式
6.添加监听器
- 配置相关依赖(创建一个maven web工程:服务的提供者
)
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http:///POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:///POM/4.0.0 http:///xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.guo.dubbo</groupId>
<artifactId>001-link-userservice-provider</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<!--让提供者项目打成jar包
双击Ctrl 输入命令:mvn 001-link-userservice-provider install-->
<dependencies>
<!--引入spring依赖 spring-context上下文 spring-webmvc -->
<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>
</dependencies>
<build>
<plugins>
<!--JDK 1.8 编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<defaultGoal>compile</defaultGoal>
</build>
</project>
- 服务的提供者:创建一个实体bean查询的结果,提供一个服务接口:xxXX,实现这个服务接口:xxxxImpl
- 配置dubbo服务提供者的核心配置文件
a. 声明dubbo服务提供者的名称:保证唯一
b. 声明dubbo使用的协议和端口号
c. 暴露服务,使用直连方式
src/main/resources/dubbo-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:///schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http:///schema/dubbo http:///schema/dubbo/dubbo.xsd">
<!--服务的提供者声明名称:必须保证服务名称的唯一性,它的名称是dubbo内部使用的唯一表示-->
<dubbo:application name="001-link-userservice-provider"/>
<!--访问服务协议的名称及端口号,dubbo官网推荐使用的是dubbo协议20880-->
<!--name:指定协议的名称
port:指定协议的端口号(默认是20880)-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--暴露服务接口 dubbo:service
interface:暴露的服务接口的全限定类名
ref:接口引用的实现类在spring容器中的标识
registry:如果不使用注册中心,则值为:N/A (直连)
-->
<dubbo:service interface="com.guo.dubbo.service.UserService" ref="userService" registry="N/A"/>
<!--将接口的实现类加载到spring容器中-->
<bean id="userService" class="com.guo.dubbo.service.impl.UserSericeImpl"/>
</beans>
- 添加监听器
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-userservice-provider.xml</param-value>
</context-param>
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
- 创建一个Tomcat
2.4.2 服务的消费者
思路:
1.创建一个maven web工程:服务的消费者
2.配置pom文件 :添加需要的依赖(spring,dubbo)
3.设置dubbo的核心配置文件()
4.编写controller
5.配置中央调度器(servlet:DispatcherServlet)
- 配置pom文件 :添加需要的依赖(spring,dubbo)
服务消费者需要引入服务提供者的业务接口,调用业务接口所提供的方法,因此,我们需要在服务提供者的项目上打一下jar包,发布到本地仓库,这样在服务消费者直接可以引入服务消费者的相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http:///POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:///POM/4.0.0 http:///xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.guo.dubbo</groupId>
<artifactId>002-link-consumer</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<dependencies>
<!--spring依赖 spring-context spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--引入服务提供者的依赖,,这里需要先在服务的提供者项目打一下jar包,这里引用那个jar包-->
<dependency>
<groupId>com.guo.dubbo</groupId>
<artifactId>001-link-userservice-provider</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 设置dubbo的核心配置文件()
src/main/resources/dubbo-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:///schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http:///schema/dubbo http:///schema/dubbo/dubbo.xsd">
<!--声明消费者的名称:保证唯一性-->
<dubbo:application name="002-link-consumer"/>
<!--引用远程服务接口:
id:远程服务接口对象的名称
interface:调用远程接口的全限定类名
url:访问服务接口的地址
registry:不使用注册中心,直连值为:N/A-->
<!--服务消费者dubbo的核心配置文件-->
<dubbo:reference id="userService"
interface="com.guo.dubbo.service.UserService"
url="dubbo://localhost:20880"
registry="N/A"/>
</beans>
src/main/resources/aplication.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.web"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--前端页面——视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--视图解析器-->
</beans>
- 控制层
UserController.java
package com.guo.dubbo.web;
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;
import javax.jws.WebParam;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user")
public String userDetail(Model model,Integer id){
User user = userService.queryUserById(id);
model.addAttribute("user",user);
return "index";
}
}
- 配置中央调度器servlet
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">
<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:aplication.xml,classpath:dubbo-consumer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- jsp页面展示数据
src/main/webapp/index.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
<title>用户详情</title>
</head>
<body>
<h1>用户详情</h1>
<div>用户标识:${user.id}</div>
<div>用户名称:${}</div>
<div>用户年龄:${user.age}</div>
</body>
</html>
- 创建一个Tomcat服务
2.4.1 服务的提供者、消费者两个服务跑起来
服务成功启动,去访问服务消费者的控制层
http://localhost:8080/user?id=1001
BUG:
- xml 中引入命名空间容易出错
- 实体类的序列化实现序列化接口
dubbo官方推荐必须有一个
接口工程
,它就是一个maven java工程要求接口工程里存放到内容如下:
1.对外暴露的服务接口(service接口)
2.实体bean对象