在社交网络中应用Dubbo,可以通过以下步骤实现:
以下是一个详细的示例,展示如何在社交网络中应用Dubbo。
我们将创建一个包含多个模块的项目:dubbo-api、user-service、friend-service、message-service 和 frontend-application。
social-network
├── dubbo-api
│ └── src/main/java/com/example/dubbo/api
│ ├── UserService.java
│ ├── FriendService.java
│ └── MessageService.java
├── user-service
│ └── src/main/java/com/example/user
│ ├── service
│ │ └── UserServiceImpl.java
│ ├── config
│ │ └── DubboProviderConfig.java
│ └── UserServiceApplication.java
├── friend-service
│ └── src/main/java/com/example/friend
│ ├── service
│ │ └── FriendServiceImpl.java
│ ├── config
│ │ └── DubboProviderConfig.java
│ └── FriendServiceApplication.java
├── message-service
│ └── src/main/java/com/example/message
│ ├── service
│ │ └── MessageServiceImpl.java
│ ├── config
│ │ └── DubboProviderConfig.java
│ └── MessageServiceApplication.java
├── frontend-application
│ └── src/main/java/com/example/frontend
│ ├── controller
│ │ └── SocialNetworkController.java
│ ├── config
│ │ └── DubboConsumerConfig.java
│ └── FrontendApplication.java
└── pom.xml
dubbo-api 模块创建 dubbo-api 模块的 pom.xml 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/POM/4.0.0 http://www.w3.org/2001/04/xmldsig-more#">
<modelVersion>4.0.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>dubbo-apiartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>2.7.8version>
dependency>
dependencies>
project>
在 dubbo-api/src/main/java/com/example/dubbo/api 目录下创建 UserService、FriendService 和 MessageService 接口:
package com.example.dubbo.api;
public interface UserService {
String getUser(String userId);
}
public interface FriendService {
String addFriend(String userId, String friendId);
String getFriends(String userId);
}
public interface MessageService {
String sendMessage(String fromUserId, String toUserId, String message);
String getMessages(String userId);
}
user-service 模块创建 user-service 模块的 pom.xml 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/POM/4.0.0 http://www.w3.org/2001/04/xmldsig-more#">
<parent>
<groupId>com.examplegroupId>
<artifactId>social-networkartifactId>
<version>1.0-SNAPSHOTversion>
<relativePath>../pom.xmlrelativePath>
parent>
<artifactId>user-serviceartifactId>
<dependencies>
<dependency>
<groupId>com.examplegroupId>
<artifactId>dubbo-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.8version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
dependencies>
project>
在 user-service/src/main/java/com/example/user/service 目录下创建 UserServiceImpl 类:
package com.example.user.service;
import com.example.dubbo.api.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@DubboService
public class UserServiceImpl implements UserService {
private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
@Override
public String getUser(String userId) {
logger.info("Fetching user with ID: {}", userId);
return "User: " + userId;
}
}
在 user-service/src/main/java/com/example/user/config 目录下创建 DubboProviderConfig 类:
package com.example.user.config;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableDubbo(scanBasePackages = "com.example.user.service")
public class DubboProviderConfig {
}
在 user-service/src/main/java/com/example/user 目录下创建 UserServiceApplication 类:
package com.example.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
在 user-service/src/main/resources 目录下创建 application.yml 配置文件:
spring:
application:
name: user-service
main:
web-application-type: none
dubbo:
application:
name: user-service
registry:
address: zookeeper://localhost:2181
protocol:
name: dubbo
port: 20880
scan:
base-packages: com.example.user.service
logging:
level:
com.example.user: INFO
file:
name: logs/user-service.log
friend-service 模块创建 friend-service 模块的 pom.xml 文件,类似于 user-service 模块。
在 friend-service/src/main/java/com/example/friend/service 目录下创建 FriendServiceImpl 类:
package com.example.friend.service;
import com.example.dubbo.api.FriendService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@DubboService
public class FriendServiceImpl implements FriendService {
private static final Logger logger = LoggerFactory.getLogger(FriendServiceImpl.class);
@Override
public String addFriend(String userId, String friendId) {
logger.info("Adding friend {} for user {}", friendId, userId);
return "Friend " + friendId + " added for user " + userId;
}
@Override
public String getFriends(String userId) {
logger.info("Fetching friends for user {}", userId);
return "Friends of user " + userId;
}
}
在 friend-service 模块中配置Dubbo服务和启动类,类似于 user-service 模块。
message-service 模块创建 message-service 模块的 pom.xml 文件,类似于 user-service 模块。
在 message-service/src/main/java/com/example/message/service 目录下创建 MessageServiceImpl 类:
package com.example.message.service;
import com.example.dubbo.api.MessageService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@DubboService
public class MessageServiceImpl implements MessageService {
private static final Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);
@Override
public String sendMessage(String fromUserId, String toUserId, String message) {
logger.info("Sending message from {} to {}: {}", fromUserId, toUserId, message);
return "Message sent from " + fromUserId + " to " + toUserId + ": " + message;
}
@Override
public String getMessages(String userId) {
logger.info("Fetching messages for user {}", userId);
return "Messages for user " + userId;
}
}
在 message-service 模块中配置Dubbo服务和启动类,类似于 user-service 模块。
frontend-application 模块创建 frontend-application 模块的 pom.xml 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/POM/4.0.0 http://www.w3.org/2001/04/xmldsig-more#">
<parent>
<groupId>com.examplegroupId>
<artifactId>social-networkartifactId>
<version>1.0-SNAPSHOTversion>
<relativePath>../pom.xmlrelativePath>
parent>
<artifactId>frontend-applicationartifactId>
<dependencies>
<dependency>
<groupId>com.examplegroupId>
<artifactId>dubbo-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.8version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
dependencies>
project>
在 frontend-application/src/main/java/com/example/frontend/controller 目录下创建 SocialNetworkController 类:
package com.example.frontend.controller;
import com.example.dubbo.api.FriendService;
import com.example.dubbo.api.MessageService;
import com.example.dubbo.api.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SocialNetworkController {
private static final Logger logger = LoggerFactory.getLogger(SocialNetworkController.class);
@DubboReference
private UserService userService;
@DubboReference
private FriendService friendService;
@DubboReference
private MessageService messageService;
@GetMapping("/getUser")
public String getUser(@RequestParam String userId) {
logger.info("Fetching user with ID: {}", userId);
return userService.getUser(userId);
}
@GetMapping("/addFriend")
public String addFriend(@RequestParam String userId, @RequestParam String friendId) {
logger.info("Adding friend with ID: {} to user with ID: {}", friendId, userId);
return friendService.addFriend(userId, friendId);
}
@GetMapping("/sendMessage")
public String sendMessage(@RequestParam String fromUserId, @RequestParam String toUserId, @RequestParam String message) {
logger.info("Sending message from {} to {}: {}", fromUserId, toUserId, message);
return messageService.sendMessage(fromUserId, toUserId, message);
}
}
在 frontend-application/src/main/java/com/example/frontend/config 目录下创建 DubboConsumerConfig 类:
package com.example.frontend.config;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableDubbo(scanBasePackages = "com.example.frontend.controller")
public class DubboConsumerConfig {
}
在 frontend-application/src/main/java/com/example/frontend 目录下创建 FrontendApplication 类:
package com.example.frontend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FrontendApplication {
public static void main(String[] args) {
SpringApplication.run(FrontendApplication.class, args);
}
}
在 frontend-application/src/main/resources 目录下创建 application.yml 配置文件:
spring:
application:
name: frontend-application
dubbo:
application:
name: frontend-application
registry:
address: zookeeper://localhost:2181
protocol:
name: dubbo
scan:
base-packages: com.example.frontend.controller
logging:
level:
com.example.frontend: INFO
file:
name: logs/frontend-application.log
pom.xml在根项目 social-network 中创建 pom.xml 文件,定义模块和依赖管理:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/POM/4.0.0 http://www.w3.org/2001/04/xmldsig-more#">
<modelVersion>4.0.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>social-networkartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<modules>
<module>dubbo-apimodule>
<module>user-servicemodule>
<module>friend-servicemodule>
<module>message-servicemodule>
<module>frontend-applicationmodule>
modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>2.7.8version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.8version>
dependency>
dependencies>
dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>2.3.4.RELEASEversion>
plugin>
plugins>
pluginManagement>
build>
project>
确保Zookeeper在本地运行,默认端口为 2181。可以通过下载Zookeeper并运行以下命令启动Zookeeper:
bin/zkServer.sh start
UserServiceApplication 类。FriendServiceApplication 类。MessageServiceApplication 类。FrontendApplication 类。访问前端应用的用户获取、好友添加和消息发送接口: