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

Dubbo 快速学习笔记2(Dubbo快速入门)

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

Dubbo 快速学习笔记2(Dubbo快速入门)

一:Dubbo 概述 1.1 Dubbo概念:

• Dubbo是阿里巴巴公司开源的一个高性能、轻量级的 Java RPC 框架。 • 致力于提供高性能和透明化的 RPC 远程服务调用方案,以及 SOA 服务治理方案。 官网

1.2:Dubbo 架构:

服务发现的一个核心组件是注册中心,Provider 注册地址到注册中心,Consumer 从注册中心读取和订阅 Provider 地址列表。 因此,要启用服务发现,需要为 Dubbo 增加注册中心

节点角色说明:

• Provider:暴露服务的服务提供方

• Container:服务运行容器

• Consumer:调用远程服务的服务消费方

• Registry:服务注册与发现的注册中心

• Monitor:统计服务的调用次数和调用时间的监控中心

二:Dubbo 快速入门 2.1:Dubbo官方推荐使用Zookeeper作为注册中心

(这里的安装不做讲解  后面会出教程)

2.2:Dubbo 快速入门

我们要做Dubbo 的案例需要做如下几件事情:

① 创建服务提供者Provider模块

② 创建服务消费者Consumer模块

③ 在服务提供者模块编写 UserServiceImpl 提供服务

④ 在服务消费者中的 UserController 远程调用 UserServiceImpl 提供的服务

⑤ 分别启动两个服务,测试

测试项目已经上传码云,已经相关安装包

地址:https://gitee.com/heyutingfeng/spring_dubbo/tree/master/

1:创建三个项目模块

其中 interface 是公共模块,service 是提供者, web 是消费者

2: 先配置pom 文件  interface 不用配置pom  只要install 一下被其余模块引入即可 

我们需要配置tomcat 的端口号 不要让其冲突,另外引入dubbo 依赖还有zookeeper 依赖



    4.0.0

    org.example
    dubbo-web
    1.0-SNAPSHOT
    war


    
        5.1.9.RELEASE
        2.7.4.1
        4.0.0

    

    
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
        
            org.slf4j
            slf4j-api
            1.7.21
        
        
            org.slf4j
            slf4j-log4j12
            1.7.21
        



        
        
            org.apache.dubbo
            dubbo
            ${dubbo.version}
        
        
        
            org.apache.curator
            curator-framework
            ${zookeeper.version}
        
        
        
            org.apache.curator
            curator-recipes
            ${zookeeper.version}
        

        
        
            org.aming
            dubbo-interface
            1.0-SNAPSHOT
        


        
        

    


    
        
            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.1
                
                    8000
                    /
                
            
        
    

3:spring 配置文件

dubbo-web 配置文件 springmvc.xml 配置文件中 我们要避免与消费者qos端口发生冲突所以需要重新设置,另外注册中心地址也要配置






    
    


    
    
    
        
    
    
    
    
    

 dubbo-service  的applicationContext.xml 配置文件





	

	
	
	
	
	
	
	

4:配置web.xml 文件




    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring/springmvc.xml
        
    

    
        springmvc
        /
    



    
    
        contextConfigLocation
        classpath*:spring/applicationContext*.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

5:编写代码 

生产者:

package com.hlm.service.impl;



//一般情况我们是@Service  将该类创建出来 放到SpringIoC 容器当中

import com.hlm.service.UserService;
import org.apache.dubbo.config.annotation.Service;


@Service
public class UserServiceImpl implements UserService {

    @Override
    public String sayHello() {
        return "hello dubbo ~~~~";
    }
}

消费者:

package com.hlm.controller;

import com.hlm.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    

    @Reference
    private  UserService userService;

    @RequestMapping("/sayHello")
    public String sayHello(){
        return userService.sayHello();
    }

}

6:启动测试 

 

 然后用dubbo-admin 查看我们的服务

2.3:Dubbo-admin 教程:

点击下面连接学习

https://mp.csdn.net/mp_blog/creation/editor/123673870

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

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

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