微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,可以成功访问。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐