千家信息网

如何进行springboot2.2.2集成dubbo的实现

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,本篇文章为大家展示了如何进行springboot2.2.2集成dubbo的实现,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。最近在学习dubbo,想着作一些笔
千家信息网最后更新 2025年01月31日如何进行springboot2.2.2集成dubbo的实现

本篇文章为大家展示了如何进行springboot2.2.2集成dubbo的实现,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

最近在学习dubbo,想着作一些笔记,从来没有在csdn上面写过博客,今天献出第一次,哈哈,直接上代码

一、创建父工程

4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.dubbo demo01 1.0.0 pom Spring Boot2.x 整合 dubbo api provider consumer UTF-8 UTF-8 1.8 2.7.5 4.2.0 3.4.12 org.apache.dubbo dubbo-spring-boot-starter ${dubbo.version} org.apache.curator curator-recipes ${curator.version} org.apache.zookeeper zookeeper ${zookeeper.version} org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true

二、创建提供者与消费者共用的api

该模块没有什么好说的,提供者和消费者都需要使用的接口api,提供者和消费者都需要引入该模块

demo01 com.dubbo 1.0.0 4.0.0 api

// 注解都是lombok的,真的很方便@Data@Builder@NoArgsConstructor@AllArgsConstructor(access = AccessLevel.PRIVATE)public class User implements Serializable { private Integer id; private String name; private Integer age;}

public interface UserService { User getUserById(Integer id);}

三、创建提供者

4.0.0 com.dubbo 1.0.0 demo01 com.dubbo provider 0.0.1-SNAPSHOT provider Demo project for Spring Boot org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo-spring-boot-starter org.apache.curator curator-recipes org.apache.zookeeper zookeeper com.dubbo api 1.0.0 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin

dubbo: application: # 应用名称 name: user-provider protocol: # 协议名称 name: dubbo # 协议端口 port: 20880 registry: # 注册中心地址 address: zookeeper://192.168.104.231:2181

@SpringBootApplication// 提供服务的应用必须配置此项@DubboComponentScan("com.dubbo.provider.service")public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }}

@Component// 该service是org.apache.dubbo.config.annotation.Service@Servicepublic class UserServiceImpl implements UserService { @Override public User getUserById(Integer id) { User user = User.builder() .id(id) .name("张三") .age(20 + id) .build(); return user; }}

四、创建消费者

4.0.0 com.dubbo 1.0.0 demo01 com.dubbo consumer 0.0.1-SNAPSHOT consumer Demo project for Spring Boot org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo-spring-boot-starter org.apache.curator curator-recipes org.apache.zookeeper zookeeper com.dubbo api 1.0.0 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin

# 端口server: port: 8081dubbo: application: name: user-consumer protocol: name: dubbo port: 20880 registry: address: zookeeper://192.168.104.231:2181

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

@RestController@RequestMapping("/user")public class ConsumerController { @Reference private UserService userService; @GetMapping("/{id}") public User getUserById(@PathVariable int id) { return userService.getUserById(id); }}

五、启动并访问

启动provider

启动consumer

浏览器访问:http://localhost:8081/user/4

上述内容就是如何进行springboot2.2.2集成dubbo的实现,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

0