栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Dubbo学习案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Dubbo学习案例

Dubbo学习案例 基本配置
  • 创建springboot项目
  • 启动zookeeper
common模块

提供接口和实体类,可以制作为二方包

依赖:

    
        
            org.projectlombok
            lombok
            1.18.22
        
    

接口:

public interface UserService {

    public User getUser(String uid);
}

实体类:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
    private String uid;
    private String username;
}
provider模块

生产者模块,提供接口实现

依赖
    

        
            com.fasterxml.jackson.core
            jackson-core
            2.10.1
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.10.1
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.10.1
        


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

        
            org.apache.dubbo
            dubbo-rpc-dubbo
            3.0.7
        

        
            org.apache.dubbo
            dubbo-registry-zookeeper
            3.0.7
        

        
            org.springframework.boot
            spring-boot-starter-web
            2.6.6
        

        
            org.example
            common
            1.0-SNAPSHOT
        

    

配置
server.port=8081

dubbo.application.name=provider-application

dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

dubbo.registry.address=zookeeper://127.0.0.1:2181
实现类
import bcy.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class UserServiceImpl implements UserService {

    public User getUser(String uid) {
        User user = new User(uid, "bcy");
        return user;
    }
}
启动类
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo(scanBasePackages = "bcy.service.impl")
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class);
    }
}
消费者模块 依赖
    
        
            com.fasterxml.jackson.core
            jackson-core
            2.10.1
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.10.1
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.10.1
        


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

        
            org.apache.dubbo
            dubbo-rpc-dubbo
            3.0.7
        

        
            org.apache.dubbo
            dubbo-registry-zookeeper
            3.0.7
        

        
            org.springframework.boot
            spring-boot-starter-web
            2.6.6
        

        
            org.example
            common
            1.0-SNAPSHOT
        

    
配置
server.port=8082

dubbo.application.name=consumer-application
dubbo.registry.address=zookeeper://127.0.0.1:2181
消费者接口
import bcy.entity.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class OrderService {
    @DubboReference
    private UserService userService;

    public String createOrder(){
        User user = userService.getUser("1");
        System.out.println("创建订单");

        return user.toString()+" succeeded in creating the order";
    }
}
对外开放接口
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import bcy.service.OrderService;

import javax.annotation.Resource;

@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @GetMapping("/createOrder")
    public String createOrder() {
        return orderService.createOrder();
    }

}
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ConsumerApplication {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1003813.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号