项目结构
父工程只要一个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
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
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
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