Dubbo的使用

新建spring-boot项目

使用XML方式:

客户端:

  1、在pom.xml配置:

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
   </dependency>
   <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.18</version>
      <scope>provided</scope>
   </dependency>
   <dependency>
      <groupId>com.alibaba.spring.boot</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
      <version>2.0.0</version>
   </dependency>
   <!--json操作-->
   <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.31</version>
   </dependency>
   <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.6</version>
      <exclusions>
         <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
         </exclusion>
         <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
         </exclusion>
      </exclusions>
   </dependency>
   <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
   </dependency>
</dependencies>

2、resource文件夹下新建application.yml和dubbo-consumer.xml两个文件(注意:将原来的application.properties改为application.yml)

2.1、application.yml:

#服务器端口
server:
  port: 8089
#ZooKeeper的地址和端口
dubbo:
  registry:
    address: XX:2181

2.2、dubbo-consumer.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!-- 消费方应用名 -->
    <dubbo:application name="consumer"  />

    <!-- 注册中心服务地址 -->
    <dubbo:registry id="zookeeper" protocol="zookeeper" address="${dubbo.registry.address}" />

    <!-- 引用CPService服务 此处的id要和服务端的接口实现的id一致-->
    <dubbo:reference id="CPService" interface="com.example.demo.service.CreditPolicyService"
                     check="false" version="1.0" url="" registry="zookeeper" protocol="dubbo" timeout="15000"/>

</beans>

3、客户端定义与服务端一样的接口方法

public interface CreditPolicyService {
    List<Map<String,String>> getQueryByRowKey(String rowkey);
}

4、调用服务:

import com.alibaba.fastjson.JSON;
import com.example.demo.service.CreditPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Controller")
public class CreditPolicyController {
    @Autowired
    CreditPolicyService creditPolicyService;

    @RequestMapping(value = "/getResult", method = RequestMethod.POST)
    public Map<String,Object> getResult(@RequestBody String json){
        //参数集合
        Map<String,Object> params = JSON.parseObject(json,Map.class);
        //结果集合
        Map<String,Object> resultMap = new HashMap<String,Object>();

        try{
            if(params.get("customerNo") == null || params.get("productId") == null){
                resultMap.put("resultCode","100");
                resultMap.put("resultDesc","参数异常");
                return resultMap;
            }
            String customerNo = params.get("customerNo").toString();
            String productId = params.get("productId").toString();
            //反转客户号 customerNo
            String rowKey = CreditPolicyController.ReserverStr(customerNo,productId);
            List<Map<String,String>> mapList = creditPolicyService.getQueryByRowKey(rowKey);
            //成功
            if(mapList.size() > 0){
                resultMap.put("resultCode","000");
                resultMap.put("resultDesc","成功");
                resultMap.put("rules",mapList);

            }
        }catch (Exception e){
            resultMap.put("resultCode","500");
            resultMap.put("resultDesc","系统内部异常");
            e.printStackTrace();
            return resultMap;
        }

        return resultMap;
    }
    /**
     * 客户号反转+产品号
     */
    public static String ReserverStr(String customerNo, String produceId){
        StringBuilder stringBuilder = new StringBuilder(customerNo);
        String resCustomerNo = stringBuilder.reverse().toString();
        StringBuilder stringBuilder1 = new StringBuilder(resCustomerNo);
        String str = stringBuilder1.append("_"+produceId).toString();
        return str;
    }
}

5、application类:

package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@AutoConfigurationPackage
@ImportResource({"classpath:dubbo-consumer.xml"})
public class CustomerApplication {

   public static void main(String[] args) {
      SpringApplication.run(CustomerApplication.class, args);
   }
}

服务端:

1、application.yml同客户端

2、dubbo-provider.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="cp-service"  />

    <!-- 注册中心服务地址 -->
    <dubbo:registry id="zookeeper" protocol="zookeeper" address="${dubbo.registry.address}" />

    <!-- 用dubbo协议在30001 -->
    <dubbo:protocol name="dubbo" port="30001" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.example.demo.service.CreditPolicyService" ref="CPService"
                   version="1.0" registry="zookeeper"/>

    <!-- 具体服务接口的实现 与客户端的id一样-->
    <bean id="CPService" class="com.example.demo.service.impl.CreditPolicyServiceImpl" />

</beans>

3、接口同客户端一样

4、接口的实现类:

package com.example.demo.service.impl;



import com.example.demo.service.CreditPolicyService;
import com.example.demo.util.HbaseUtil;



import java.util.List;
import java.util.Map;


public class CreditPolicyServiceImpl implements CreditPolicyService {
    /**
     * 通过rowkey查询记录
     * @param rowkey
     * @return
     */
    @Override
    public List<Map<String,String>> getQueryByRowKey(String rowkey) {
        List<Map<String,String>> mapList = HbaseUtil.getList(rowkey);
        return mapList;
    }
}

5、application类:

package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@AutoConfigurationPackage
@ImportResource({"classpath:dubbo-provider.xml"})
public class ProducerApplication {

   public static void main(String[] args) {
      SpringApplication.run(ProducerApplication.class, args);
   }
}

使用注解方式的时候,注意:调用接口方法的类一定要在接口方法的下一层目录:

比如接口的方法为: com.example.demo.service这层目录

package com.example.demo.service;

import java.util.List;
import java.util.Map;

public interface CreditPolicyService {
    List<Map<String,String>> getQueryByRowKey(String rowkey);
}

调用类:package com.example.demo.service.controller;则在这一层

 

可以查看github两种实现方式都有:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值