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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.guo.dubbo

001-link-userservice-provider

1.0.0

war

org.springframework

spring-context

5.3.20

org.springframework

spring-webmvc

5.3.20

com.alibaba

dubbo

2.6.2

org.apache.maven.plugins

maven-compiler-plugin

3.8.1

1.8

1.8

compile

服务的提供者:创建一个实体bean查询的结果,提供一个服务接口:xxXX,实现这个服务接口:xxxxImpl

配置dubbo服务提供者的核心配置文件 a. 声明dubbo服务提供者的名称:保证唯一 b. 声明dubbo使用的协议和端口号 c. 暴露服务,使用直连方式

src/main/resources/dubbo-userservice-provider.xml

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">

添加监听器

src/main/webapp/WEB-INF/web.xml

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">

contextConfigLocation

classpath:dubbo-userservice-provider.xml

org.springframework.web.context.ContextLoaderListener

创建一个Tomcat

2.4.2 服务的消费者

思路: 1.创建一个maven web工程:服务的消费者 2.配置pom文件 :添加需要的依赖(spring,dubbo) 3.设置dubbo的核心配置文件() 4.编写controller 5.配置中央调度器(servlet:DispatcherServlet)

配置pom文件 :添加需要的依赖(spring,dubbo)

服务消费者需要引入服务提供者的业务接口,调用业务接口所提供的方法,因此,我们需要在服务提供者的项目上打一下jar包,发布到本地仓库,这样在服务消费者直接可以引入服务消费者的相关依赖

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.guo.dubbo

002-link-consumer

1.0.0

war

org.springframework

spring-context

5.3.9

org.springframework

spring-webmvc

5.3.9

com.alibaba

dubbo

2.6.2

com.guo.dubbo

001-link-userservice-provider

1.0.0

org.apache.maven.plugins

maven-compiler-plugin

3.10.1

1.8

1.8

设置dubbo的核心配置文件()

src/main/resources/dubbo-consumer.xml

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">

interface="com.guo.dubbo.service.UserService"

url="dubbo://localhost:20880"

registry="N/A"/>

src/main/resources/aplication.xml

核心配置文件中的命名空间引入易出错,在这里调试的很久才搞好

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">

控制层 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

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">

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:aplication.xml,classpath:dubbo-consumer.xml

dispatcherServlet

/

jsp页面展示数据 src/main/webapp/index.jsp

<%@ page contentType="text/html; charset=UTF-8" language="java" %>

用户详情

用户详情

用户标识:${user.id}

用户名称:${user.name}

用户年龄:${user.age}

创建一个Tomcat服务

2.4.1 服务的提供者、消费者两个服务跑起来

服务成功启动,去访问服务消费者的控制层 http://localhost:8080/user?id=1001

BUG:

xml 中引入命名空间容易出错实体类的序列化实现序列化接口

dubbo官方推荐必须有一个接口工程,它就是一个maven java工程要求接口工程里存放到内容如下: 1.对外暴露的服务接口(service接口) 2.实体bean对象

文章来源

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: