dubbo5:springboot整合

一、修改user-service-provider模块

(1)修改依赖

      因为dubbo-spring-boot-starter携带了dubbo包和zookeeper等包,所以把一开始导入的dubbo相关包移除。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-service-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>dubbo-demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.10</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.1.0</version>
        </dependency>
    </dependencies>
</project>

(2)添加配置信息

    把provider.xml中的配置移到application.xml中

server:
  port: 8801
dubbo:
  application:
    name: user-service-provider
  registry:
    address: 127.0.0.1:2181 # 注册中心地址
    protocol: zookeeper # 注册中心协议
  protocol:
    name: dubbo
    port: 20080

(3)暴露服务

package com.buba.service.impl;

import com.buba.pojo.UserAddress;
import com.buba.service.UserService;
import org.apache.dubbo.config.annotation.dubboService;
import org.springframework.stereotype.Service;

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

@dubboService // 暴露服务
@Service
public class UserServiceImpl implements UserService {
    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        UserAddress userAddress1 = new UserAddress(1, "北京", "1", "张三", "17611112222", "是");
        UserAddress userAddress2 = new UserAddress(2, "天津", "1", "张三", "17611112222", "是");
        return Arrays.asList(userAddress1, userAddress2);
    }
}

(4)编写启动类

package com.buba;

import org.apache.dubbo.config.spring.context.annotation.Enabledubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Enabledubbo // 开启dubbo注解功能
@SpringBootApplication
public class UserServiceProvider_8801 {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceProvider_8801.class, args);
    }
}

二、修改order-service-consumer模块

(1)修改Controller和业务代码

package com.buba.controller;

import com.buba.pojo.UserAddress;
import com.buba.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/Order")
public class OrderController {

    private OrderService orderService;

    @RequestMapping("/initOrder/{userId}")
    public List<UserAddress> initOrder(@PathVariable("userId") String userId) {
       return orderService.initOrder(userId);
    }

    @Autowired
    public void setorderService(OrderService orderService) {
        this.orderService = orderService;
    }
}
package com.buba.service.impl;

import com.buba.pojo.UserAddress;
import com.buba.service.OrderService;
import com.buba.service.UserService;
import org.apache.dubbo.config.annotation.dubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @dubboReference // 去zookeeper注册中心中寻找服务
    private UserService userService;

    @Override
    public List<UserAddress> initOrder(String userId) {
        return userService.getUserAddressList("1");
    }

}
package com.buba;

import org.apache.dubbo.config.spring.context.annotation.Enabledubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Enabledubbo // 开启基于注解的dubbo功能
@SpringBootApplication
public class OrderServiceConsumer_8802 {
    public static void main(String[] args) {
        SpringApplication.run(OrderServiceConsumer_8802.class, args);
    }
}

(2)修改依赖

    因为dubbo-spring-boot-starter携带了dubbo包和zookeeper等包,所以把一开始导入的dubbo相关包移除。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-service-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>dubbo-demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.10</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>5.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-x-discovery</artifactId>
            <version>5.1.0</version>
        </dependency>
    </dependencies>
</project>

(3)添加配置文件

    把consumer.xml中的配置移到application.yml中进行配置。

server:
  port: 8802
dubbo:
  application:
    name: order-service-consumer
  registry:
    address: zookeeper://127.0.0.1:2181 # 注册中心地址

三、运行测试

    登录监控中心查看

    访问地址:http://localhost:8802/Order/initOrder/1,可以成功访问。

相关文章

在网络请求时,总会有各种异常情况出现,我们需要提前处理这...
作者:宇曾背景软件技术的发展历史,从单体的应用,逐渐演进...
hello,大家好呀,我是小楼。最近一个技术群有同学at我,问我...
 一个软件开发人员,工作到了一定的年限(一般是3、4年左右...
当一个服务调用另一个远程服务出现错误时的外观Dubbo提供了多...
最近在看阿里开源RPC框架Dubbo的源码,顺带梳理了一下其中用...