Dubbo接口测试的几种方式

测试前需准备zookeeper以及dubbo服务

一:windows本地安装Zookeeper

1. 下载zookeeper,下载地址:https://www.apache.org/dyn/closer.cgi/zookeeper/

 

2. 解压后,进入目录中的conf目录,有一个zoo_sample.cfg文件,将其重命名为zoo.cfg

 

3.新建data文件,修改zoo.cfg文件

 

4. 进入bin目录双击zkServer.cmd即可开启zookeeper本地服务

 

二:编写dubbo服务接口,获取省市区信息接口

1.接口服务

package com.sgr.md.service;

import com.sgr.md.request.Area;

import com.sgr.md.response.Result;

import java.util.List;

public interface AreaService {

Result<List<Area>> findCityByPartnerId(Integer partnerId);

Result<Area> findCityByCityName(String cityName);

}

2.dao层

package com.sgr.md.provider.dao;

import com.sgr.md.request.Area;

import java.util.List;

public interface AreaMapper {

List<Area> findCityByPartnerId(Integer partnerId);

Area findCityByCityName(String cityName);

}

3.AreaMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.sgr.md.provider.dao.AreaMapper">

<select id="findCityByPartnerId" resultType="com.sgr.md.request.Area">

select id,cityName,parentId from s_provinces where parentId=#{parentId}

</select>

<select id="findCityByCityName" resultType="com.sgr.md.request.Area">

SELECT id,cityName,parentId FROM s_provinces where cityName=#{cityName}

</select>

</mapper>

4.接口实现类

package com.sgr.md.provider.impl;



import com.alibaba.dubbo.config.annotation.Service;

import com.sgr.md.request.Area;

import com.sgr.md.provider.dao.AreaMapper;

import com.sgr.md.response.Result;

import com.sgr.md.service.AreaService;

import org.springframework.beans.factory.annotation.Autowired;



import java.util.List;



@Service(group = "mm")

public class AreaServiceImpl implements AreaService {



@Autowired

private AreaMapper areaMapper;



@Override

public Result<List<Area>> findCityByPartnerId(Integer partnerId) {

List<Area> result = areaMapper.findCityByPartnerId(partnerId);

return new Result(true, result);

}



@Override

public Result<Area> findCityByCityName(String cityName) {

Area result = areaMapper.findCityByCityName(cityName);

return new Result(true, result);

}

}

5.application.properties配置文件

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.max-active=10

spring.datasource.max-idle=5

spring.datasource.min-idle=0



spring.dubbo.appname=mm-dubbo

spring.dubbo.registry=zookeeper://127.0.0.1:2181

spring.dubbo.protocol=dubbo

spring.dubbo.port=20800

6.服务提供者启动类

package com.sgr.md.provider;



import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.tomcat.jdbc.pool.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import org.springframework.transaction.PlatformTransactionManager;



@SpringBootApplication

@MapperScan("com.sgr.md.provider.dao")

@EnableDubbo

public class ProviderApplication {

@Bean

@ConfigurationProperties(prefix="spring.datasource")

public DataSource dataSource() {

return new DataSource();

}

@Bean

public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

sqlSessionFactoryBean.setDataSource(dataSource());

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));

return sqlSessionFactoryBean.getObject();

}

@Bean

public PlatformTransactionManager transactionManager() {

return new DataSourceTransactionManager(dataSource());

}

public static void main(String[] args) throws InterruptedException {

SpringApplication.run(ProviderApplication.class, args);

Thread.sleep(Long.MAX_VALUE);

}

}

三、启动zookeeper、启动dubbo服务,将服务注册到zookeeper

1.双击zkServer.cmd

 

2.启动dubbo服务

 

四、开始测试

(注意:telnet测试参数为基本类型的时候,可以使用下面的形式;但如果参数是一个对象,那就需要为该参数添加一个class属性,来声明该json对应的java类,例如:invoke service.method({"class":"xx.xx.xx","key":"value"}) 这样的形式)

1.telnet形式调用dubbo

 

 

 

2.java连接zookeeper调用dubbo

 

 

3.jmeter自定义sampler调用dubbo

jmeter1

jmeter2

 

 

4.各系统之间的服务调用形式

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐