springboot2.2.2集成dubbo的实现方法

网友投稿 270 2022-12-16


springboot2.2.2集成dubbo的实现方法

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

一、创建父工程

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

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

http:// lombok

true

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

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

http:// lombok

true

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

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

xmlns:xsi="http://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">

demo01

com.dubbo

1.0.0

4.0.0

api

xmlns:xsi="http://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">

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);

}

三、创建提供者

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

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

@Service

public class UserServiceImpl implements UserService {

@Override

public User getUserById(Integer id) {

User user = User.builder()

.id(id)

http:// .name("张三")

.age(20 + id)

.build();

return user;

}

}

四、创建消费者

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

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: 8081

dubbo:

application:

name: user-consumer

protocol:

name: dubbo

port: 20880

registry:

address: zookeeper://192.168.104.231:2181

@SpringBootApplication

public 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

好了,到此就完成了最基本的springboot与dubbo的整合,更多的dubbo的api请查阅 dubbo官方文档


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java数组索引异常产生及解决方案
下一篇:如何通过java获取文件名和扩展名
相关文章

 发表评论

暂时没有评论,来抢沙发吧~