栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

初识Dubbo(直连方式)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

初识Dubbo(直连方式)

Dubbo直连方式具体步骤实现 1、创建服务提供者 1.1、创建maven web工程

1.2、整理pom文件,添加spring依赖和dubbo依赖和jdk1.8编译插件

maven中央仓库地址
下面是完整的pom.xml




  4.0.0

  com.why
  ch03-service-provide
  1.0
  war

  
    
    
      org.springframework
      spring-context
      5.3.12
    
    
      org.springframework
      spring-webmvc
      5.3.12
    
    
    
      com.alibaba
      dubbo
      2.6.2
    
  

  
    
      
      
        maven-compiler-plugin
        3.1
        
          1.8
          1.8
        
      
    
  


1.3、在src/main目录下创建java和resources资源文件夹

选中文件夹右键 >> Mark Directory as

1.4、创建实体类,服务接口和接口实现类
实体类

public class Student implements Serializable{
    private Integer id;
    private String name;
    private boolean sex;
    private Integer age;
	getter和setter方法;
}

服务接口

public interface StudentService {
    Student QueryById(Integer id);
}

接口实现类

public class StudentServiceImp implements StudentService {
    
    @Override
    public Student QueryById(Integer id) {
        Student student = new Student();
        student.setId(001);
        student.setName("张三");
        student.setSex(true);
        student.setAge(20);
        return student;
    }
}
1.5、创建dubbo核心配置文件(spring的xml配置文件)

a.声明dubbo服务提供者名称:保证唯一
b.声明dubbo使用的协议和端口
c.暴露服务(直连方式)

dubbo核心配置文件全部代码:




    
    
    
    
    
    
    
    
    
    

1.6、web.xml中创建监听器



  
    contextConfigLocation
    classpath:dubbo-service-provide.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  

1.7、配置Tomcat修改端口

双击ctrl进行打包

打包前先注释掉war标签,打包完成后放开注释
输入mvn ch03-service-provide install

服务提供者就创建好了,接下来创建服务消费者

2、创建服务消费者

2.1、创建maven web工程

2.2、整理pom文件,添加spring依赖、dubbo依赖、服务提供者jar包和jdk1.8编译插件



  4.0.0

  com.why
  ch04-service-consumer
  1.0
  war

  
    
      org.springframework
      spring-context
      5.3.12
    
    
      org.springframework
      spring-webmvc
      5.3.12
    
    
      com.alibaba
      dubbo
      2.6.2
    
    
      com.why
      ch03-service-provide
      1.0
    
  

  
    
      
      
        maven-compiler-plugin
        3.1
        
          1.8
          1.8
        
      
    
  


2.3、在src/main目录下创建java和resources资源文件夹 2.4、创建dubbo核心配置文件和springmvc配置文件(spring的xml配置文件)

创建dubbo核心配置文件



    
    
    
    
    
    
    
    

创建springMVC的配置文件




    
    
    
    
    
    
    
        
        
    

2.5、编写controller
@Controller
public class Mycontroller {
    @Resource
    StudentService studentService;

    @RequestMapping("")
    public String studnetQuery(Model model, Integer id){
        Student student = studentService.QueryById(id);
        model.addAttribute("student",student);
        return "student";
    }
}

2.6、配置中央调度器



  Archetype Created Web Application
  
    DispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:application.xml,classpath:dubbo-service-consumer.xml
    
  
  
    DispatcherServlet
    /
  


2.7创建jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>


    Title


    学生id=${student.id}
学生name=${student.name}
学生age=${student.age}
测试:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/463205.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号