Dubbo(二)Hello测试(SpringBoot)

Dubbo(二)Hello测试(SpringBoot),第1张

Dubbo(二)Hello测试(SpringBoot)

一、创建发布者项目

dubbo-boot-service-provider

选定 springboot的模板创建

 

 填入参数

 

 这边暂时不选任何

 

接着下一步‘完成

1. 引用依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.2
         
       
    com.hhm
    dubbo-boot-service-provider
    0.0.1-SNAPSHOT
    dubbo-boot-service-provider
    dubbo的发布者
    jar
    
        1.8
        2.6.2
        2.7.8
        true
    
    
        
            
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            

            
            
                org.apache.dubbo
                dubbo-dependencies-bom
                ${dubbo.version}
                pom
                import
            

            
                org.apache.dubbo
                dubbo
                ${dubbo.version}
                
                    
                        org.springframework
                        spring
                    
                    
                        javax.servlet
                        servlet-api
                    
                    
                        log4j
                        log4j
                    
                
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.example
            dubbo-demo-service
            1.0-SNAPSHOT
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.8
        
        
            org.apache.curator
            curator-framework
        
        
            org.apache.curator
            curator-recipes
        
        
            org.apache.zookeeper
            zookeeper
        
    
    
        
            apache.snapshots.https
            Apache Development Snapshot Repository
            https://repository.apache.org/content/repositories/snapshots
            
                false
            
            
                true
            
        
    
    
    
        
            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.22.2
                
                    true
                
            
        
    

 

2. 配置 application.properties ,这边配置跟 spring的配置一样

dubbo.application.name=dubbo-boot-service-provider

dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1
dubbo.registry.port=2181

dubbo.protocol.name=dubbo
dubbo.protocol.port=20881

dubbo.monitor.protocol=registry

server.port=8081

 

3. 创建实现及调用

package com.hhm.gmall.service.impl;

import com.hhm.gmall.bean.UserAddress;
import com.hhm.gmall.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;

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


@DubboService(version = "1.0.0")
public class UserServiceImpl implements UserService {
    @Value("${dubbo.application.name}")
    private String serviceName;

    
    @Override
    public List getUserAddressList(String userId) {
        System.out.println("serviceName=="+serviceName);
        UserAddress userAddress1 = new UserAddress(1, "厦门市思明区软件园二期科技大楼3层", "1", "李老师", "13754698745", "Y");
        UserAddress userAddress2 = new UserAddress(2, "广州市思明区软件园二期科技大楼3层", "1", "姜老师", "13754567425", "N");

        return Arrays.asList(userAddress1,userAddress2);
    }
} 

 

package com.hhm.gmall;

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


//@SpringBootApplication
@EnableDubbo
@EnableAutoConfiguration
public class DubboBootServiceProviderApplication {

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

}

@EnableAutoConfiguration 这个包含了 @SpringBootApplication

执行启动,调用监控发现服务已经暴露了

可以点击测试

 

控制台也有自己打印的,也侧面说明调用到

 

二、创建消费者项目 dubbo-boot-service-consumer

1. 配置 pom.xml

 



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.2
         
    
    com.hhm
    dubbo-boot-service-consumer
    0.0.1-SNAPSHOT
    dubbo-boot-service-consumer
    dubbo的订阅者
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.example
            dubbo-demo-service
            1.0-SNAPSHOT
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.8
        
        
            org.apache.curator
            curator-framework
            4.0.1
        
        
            org.apache.curator
            curator-recipes
            4.0.1
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2. 配置 application.properties

dubbo.application.name=dubbo-boot-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry
server.port=8084

3. 实现方法及控制器

package com.hhm.gmall.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hhm.gmall.bean.UserAddress;
import com.hhm.gmall.service.OrderService;
import com.hhm.gmall.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {
    //1@DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:20881")
    @DubboReference(version = "1.0.0")
    UserService userService;

    @Override
    public List initOrder(String userId) {
        System.out.println("用户id==" + userId);
        List addressList = userService.getUserAddressList(userId);
        for (UserAddress userAddress : addressList) {
            System.out.println(userAddress);
        }
        return addressList;
    }
}
package com.hhm.gmall.controller;

import com.hhm.gmall.bean.UserAddress;
import com.hhm.gmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class OrderController {

    @Autowired
    OrderService orderService;

    @ResponseBody
    @RequestMapping("/initOrder")
    public List initOrder(@RequestParam("uid") String userId) {
        return orderService.initOrder(userId);
    }
}
package com.hhm.gmall;

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

@SpringBootApplication
@EnableDubbo
public class DubboBootServiceConsumerApplication {

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

}

=====================================================

如果需要指定单独方法的配置

@DubboReference(version = "1.0.0",methods = {@Method(name="getUserAddressList",timeout = 4000)})
UserService userService;

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5706408.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存