首页 > 其他分享 >springboot整合dubbo3.0

springboot整合dubbo3.0

时间:2022-11-29 21:26:01浏览次数:40  
标签:dubbo3.0 springboot dubbo User 整合 org import com example

项目结构


父工程只要一个pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>dubbo-interface</module>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dubbo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo</name>
    <description>dubbo</description>
    <properties>
        <java.version>1.8</java.version>
        <dubbo-boot.version>3.0.4</dubbo-boot.version>
        <zkClient.version>4.2.0</zkClient.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-x-discovery</artifactId>
            <version>${zkClient.version}</version>
        </dependency>
    </dependencies>
</project>

dubbo-interface

image
User

package com.example.entity;

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

import java.io.Serializable;

/**
 * @Classname: User
 * @Description:
 * @Author: Stonffe
 * @Date: 2022/11/28 21:57
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer userId;
    private String username;
}

UserService

package com.example.service;

import com.example.entity.User;

/**
 * @Classname: UserService
 * @Description:
 * @Author: Stonffe
 * @Date: 2022/11/28 21:59
 */
public interface UserService {
    User getUserById(Integer userId);
    User getUser(String s);
}

dubbo-provider

image
pom文件

<?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</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-x-discovery</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

UserServiceImpl

package com.example.impl;

import com.example.entity.User;
import com.example.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;

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

/**
 *  @Classname: impl
 *  @Description:
 *  @Author: Stonffe
 *  @Date: 2022/11/28 22:12
 */
@DubboService
public class UserServiceImpl implements UserService {
    private static final List<User> USERS = Arrays.asList(
            new User(1,"sakura"),
            new User(2,"tomoyo")
    );
    @Override
    public User getUserById(Integer userId) {
        for (User u :
                USERS) {
            if (u.getUserId().equals(userId)){
                return u;
            }
        }
        return null;
    }

    @Override
    public User getUser(String s) {
        User user = new User(3, s);
        return user;
    }
}

DubboProviderApplicaion

package com.example;

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

/**
 * @Classname: DubboProviderAppliction
 * @Description:
 * @Author: Stonffe
 * @Date: 2022/11/28 22:15
 */
@EnableDubbo
@SpringBootApplication
public class DubboProviderAppliction {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderAppliction.class,args);
    }
}

application.properties

dubbo.application.name=dubbo-provider
dubbo.registry.address=zookeeper://192.168.100.100:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

dubbo-consumer

image
pom文件

<?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</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-consumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-x-discovery</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
</project>

UserController

package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname: UserController
 * @Description:
 * @Author: Stonffe
 * @Date: 2022/11/28 22:31
 */
@RestController
public class UserController {
    @DubboReference
    private UserService userService;
    @GetMapping("/user/{userId}")
    public ResponseEntity<User> getUserById(@PathVariable("userId") Integer userId){
        System.out.println("userId = " + userId);
        User user = userService.getUserById(userId);
        System.out.println("user = " + user);
        return ResponseEntity.ok(userService.getUserById(userId));
    }
    @GetMapping("/{user}")
    public ResponseEntity<User> getUser(@PathVariable("user") String user) {
        System.out.println(user);
        User user1 = userService.getUser(user);
        return ResponseEntity.ok(user1);
    }
}

DubboConsumerApplication

package com.example;

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

/**
 * @Classname: DubboConsumerApplication
 * @Description:
 * @Author: Stonffe
 * @Date: 2022/11/28 22:32
 */
@EnableDubbo
@SpringBootApplication
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class,args);
    }
}

application.properties

dubbo.application.name=dubbo-consumer
dubbo.registry.address=zookeeper://192.168.100.100:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
server.port=8081

即便是搭建这样一个小小的案例,也出了很多错误,最后重启了几次服务不知道怎么就解决了

标签:dubbo3.0,springboot,dubbo,User,整合,org,import,com,example
From: https://www.cnblogs.com/xiaoovo/p/16936741.html

相关文章

  • springboot从yaml文件读取pom文件的properties
     如果没有其他配置,只能读取启动类模块下和父级模块的pom的properties,以下是配置:<properties><revision>1.0</revision></properties><build><res......
  • SSM整合配置
    ①pom配置<!--SpringMVC--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.1</version></......
  • springboot的启动流程
           ......
  • SpringBoot 实际项目开发中工厂模式的巧妙使用
    简单工厂模式:     简单工厂模式是创建型模式,创建型模式顾名思义,也就是说在创建对象的时候,遇到了瓶颈才会选择的设计模式。那么该什么情况使用呢。  简单工厂模式......
  • k8s整合kong
    k8s整合kongKong网关的发展历程​Kong网关起源于2007年,由Augusto、Marco、Michele三人在意大利的一个小车库中开发,当时命名为Mashup平台。在随后7年的时间里,Mash......
  • k8s整合Traefik
    k8s整合Traefik介绍公司的k8s云测试环境,集群和核心组件研究部署的差不多了,此处用阿里云进行模拟,记录安装traefik过程Traefik是一个开源的可以使服务发布变得轻松有趣的......
  • SpringBoot 2.4.0版本后解决跨域问题
    packagecom.atguigu.gulimall.gateway.config;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;impor......
  • springboot报错说 Failed to parse multipart servlet request; nested exception is
    问题:一次开发中遇到一个springboot的异常,如下所示:Failedtoparsemultipartservletrequest;nestedexceptionisjava.io.IOException:Thetemporaryuploadlocatio......
  • SpringBoot2 常用注解
    目录​​SpringBoot2常用注解​​​​@SpringBootApplication​​​​@EnableAutoConfiguration​​​​@ImportResource​​​​@Value​​​​@ConfigurationProperties(......
  • SpringBoot 关于session的使用问题
    第一步:@AutowiredprivateHttpServletRequesthttpServletRequest;第二步:存储sessionHttpSessionsession=httpServletRequest.getSession();ses......