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

Dubbo(二)Hello测试(Spring)

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

Dubbo(二)Hello测试(Spring)

需要创建 发布者、订阅者

一:创建统一项目,主要是定义接口及实体,方便发布和订阅两个项目调用

dubbo-demo-service

定义实体类

package com.hhm.gmall.bean;

import java.io.Serializable;


public class UserAddress implements Serializable {

    private Integer id;
    
    private String userAddress;
    
    private String userId;
    
    private String consignee;
    
    private String phoneNum;
    
    private String isDefault;

    public UserAddress() {
        super();
    }

    public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum, String isDefault) {
        this.id = id;
        this.userAddress = userAddress;
        this.userId = userId;
        this.consignee = consignee;
        this.phoneNum = phoneNum;
        this.isDefault = isDefault;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getConsignee() {
        return consignee;
    }

    public void setConsignee(String consignee) {
        this.consignee = consignee;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public String getIsDefault() {
        return isDefault;
    }

    public void setIsDefault(String isDefault) {
        this.isDefault = isDefault;
    }

    @Override
    public String toString() {
        return "UserAddress{" +
                "id=" + id +
                ", userAddress='" + userAddress + ''' +
                ", userId='" + userId + ''' +
                ", consignee='" + consignee + ''' +
                ", phoneNum='" + phoneNum + ''' +
                ", isDefault='" + isDefault + ''' +
                '}';
    }
}

定义发布者的接口

package com.hhm.gmall.service;

import com.hhm.gmall.bean.UserAddress;

import java.util.List;


public interface UserService {
    
    List getUserAddressList(String userId);
}

定义订阅者的调用接口

package com.hhm.gmall.service;


public interface OrderService {
    void initOrder(String userId);
}

二:dubbo-demo-service-provider 发布者的项目,提供服务

1. jar的引用 pom.xml



    
        Spring
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    dubbo-demo-service-provider

    
        
            org.springframework
            spring-context
            5.0.9.RELEASE
        
        
            org.example
            dubbo-demo-service
            1.0-SNAPSHOT
        
        
        
            com.alibaba
            dubbo
            2.6.11
        
        
        
            org.apache.curator
            curator-framework
            4.0.1
        
        
        
            io.netty
            netty-all
            4.1.56.Final
        
        
            log4j
            log4j
            1.2.17
        
        
            org.slf4j
            slf4j-api
            1.7.32
        
        
            org.slf4j
            slf4j-log4j12
            1.7.32
        
    


需要引用 netty-all 不然会报错

java.lang.NoClassDefFoundError: io/netty/channel/EventLoopGroup

需要引用slfj 不然会报错

 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

之前一直报错(破解版的问题): 点击文件->作废缓存重启

参考:【异常】java: 程序包org.springframework.context.annotation不存在。_ID:CheneyWang的博客-CSDN博客

java: 程序包org.springframework.context.annotation不存在

1.创建实现

package com.hhm.gmall.service.impl;

import com.hhm.gmall.bean.UserAddress;
import com.hhm.gmall.service.UserService;

import java.util.Arrays;
import java.util.List;


public class UserServiceImpl implements UserService {
    
    @Override
    public List getUserAddressList(String userId) {
        UserAddress userAddress1 = new UserAddress(1, "厦门市思明区软件园二期科技大楼3层", "1", "李老师", "13754698745", "Y");
        UserAddress userAddress2 = new UserAddress(2, "广州市思明区软件园二期科技大楼3层", "1", "姜老师", "13754567425", "N");

        return Arrays.asList(userAddress1,userAddress2);
    }
}

2. 创建 dubbo的配置 provider.xml




    
    
    

    

    
    

    
    
    
    


后续启动报错:

[main] ERROR [com.alibaba.dubbo.qos.server.Server] -  [DUBBO] qos-server can not bind localhost:22222, dubbo version: 2.6.11, current host: 192.168.1.24
  java.net.BindException: Address already in use: bind

是因为这里的端口冲突了,换一个就好

 3. 创建main进行测试

package com.hhm.gmall;


import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;


public class MainApplication {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
        ioc.start();

        System.in.read();
    }
}

附上 log4j.properties  

log4j.rootLogger=ERROR, Console  

#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  

log4j.logger.java.sql.ResultSet=INFO  
log4j.logger.org.apache=INFO  
log4j.logger.java.sql.Connection=DEBUG  
log4j.logger.java.sql.Statement=DEBUG  
log4j.logger.java.sql.PreparedStatement=DEBUG

###显示SQL语句部分
#log4j.logger.com.ibatis=DEBUG
#log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
#log4j.logger.com.ibatis.common.jdbc.scriptRunner=DEBUG
#log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
#log4j.logger.Java.sql.Connection=DEBUG

 三:订阅的项目

项目名: dubbo-demo-service-consumer

1. pom.xml



    
        Spring
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    dubbo-demo-service-consumer
    
        
            org.example
            dubbo-demo-service
            1.0-SNAPSHOT
        
        
            com.alibaba
            dubbo
            2.6.11
        
        
        
            org.apache.curator
            curator-framework
            4.0.1
        
        
            io.netty
            netty-all
            4.1.56.Final
        
        
            log4j
            log4j
            1.2.17
        
        
            org.slf4j
            slf4j-api
            1.7.32
        
        
            org.slf4j
            slf4j-log4j12
            1.7.32
        
    


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

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

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