dubbo(5):springboot整合

一、修改user-service-provider模块

(1)修改依赖

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



    
        dubbo-demo
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    user-service-provider

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.example
            dubbo-demo-api
            1.0-SNAPSHOT
        

        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.10
        

        
            org.apache.curator
            curator-recipes
            5.1.0
        
    

(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 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 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 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相关包移除。



    
        dubbo-demo
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    order-service-consumer

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot
        
        
            org.example
            dubbo-demo-api
            1.0-SNAPSHOT
        

        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.10
        

        
            org.apache.curator
            curator-recipes
            5.1.0
        

        
            org.apache.curator
            curator-framework
            5.1.0
        

        
            org.apache.curator
            curator-x-discovery
            5.1.0
        
    

(3)添加配置文件

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

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

三、运行测试

    登录监控中心查看

dubbo(5):springboot整合_第1张图片

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

你可能感兴趣的:(dubbo)