mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
初始化 dubbo xml 示例
This commit is contained in:
Binary file not shown.
@@ -3,7 +3,7 @@
|
||||
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>lab-29-dubbo-xml-demo</artifactId>
|
||||
<artifactId>lab-30-dubbo-xml-demo</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
@@ -2,8 +2,17 @@ package cn.iocoder.springboot.lab30.rpc.api;
|
||||
|
||||
import cn.iocoder.springboot.lab30.rpc.dto.UserDTO;
|
||||
|
||||
/**
|
||||
* 用户服务 RPC Service 接口
|
||||
*/
|
||||
public interface UserRpcService {
|
||||
|
||||
/**
|
||||
* 根据指定用户编号,获得用户信息
|
||||
*
|
||||
* @param id 用户编号
|
||||
* @return 用户信息
|
||||
*/
|
||||
UserDTO get(Integer id);
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package cn.iocoder.springboot.lab30.rpc.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户信息 DTO
|
||||
*/
|
||||
public class UserDTO implements Serializable {
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
<artifactId>user-rpc-service-consumer</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入定义的 Dubbo API 接口 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<artifactId>user-rpc-service-api</artifactId>
|
||||
|
||||
@@ -4,25 +4,38 @@ import cn.iocoder.springboot.lab30.rpc.api.UserRpcService;
|
||||
import cn.iocoder.springboot.lab30.rpc.dto.UserDTO;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@SpringBootApplication
|
||||
@ImportResource("classpath:dubbo.xml")
|
||||
public class ConsumerApplication {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerApplication.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 启动 Spring Boot 应用
|
||||
ConfigurableApplicationContext context = SpringApplication.run(ConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
@Component
|
||||
public class UserRpcServiceTest implements CommandLineRunner {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Resource
|
||||
private UserRpcService userRpcService;
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
UserDTO user = userRpcService.get(1);
|
||||
logger.info("[run][发起一次 Dubbo RPC 请求,获得用户为({})", user);
|
||||
}
|
||||
|
||||
// 获得 UserRpcService
|
||||
UserRpcService service = context.getBean(UserRpcService.class);
|
||||
UserDTO user = service.get(1);
|
||||
LOGGER.info("[main][发起一次 Dubbo RPC 请求,获得用户为({})", user);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
# dubbo
|
||||
# dubbo 配置项,对应 DubboConfigurationProperties 配置类
|
||||
dubbo:
|
||||
# Dubbo 应用配置
|
||||
application:
|
||||
name: user-service
|
||||
name: user-service-consumer # 应用名
|
||||
# Dubbo 注册中心配置
|
||||
registry:
|
||||
address: zookeeper://127.0.0.1:2181
|
||||
protocol:
|
||||
port: -1
|
||||
name: dubbo
|
||||
address: zookeeper://127.0.0.1:2181 # 注册中心地址。个鞥多注册中心,可见 http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html 文档。
|
||||
# Dubbo 消费者配置
|
||||
consumer:
|
||||
timeout: 1000 # 【重要】远程服务调用超时时间,单位:毫秒。默认为 1000 毫秒,胖友可以根据自己业务修改
|
||||
UserRpcService:
|
||||
version: 1.0.0
|
||||
|
||||
@@ -10,9 +10,10 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>user-rpc-service</artifactId>
|
||||
<artifactId>user-rpc-service-provider</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入定义的 Dubbo API 接口 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<artifactId>user-rpc-service-api</artifactId>
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class UserRpcServiceImpl implements UserRpcService {
|
||||
|
||||
@Override
|
||||
public UserDTO get(Integer id) {
|
||||
return new UserDTO().setId(id)
|
||||
.setName("没有昵称:" + id)
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
# dubbo
|
||||
# dubbo 配置项,对应 DubboConfigurationProperties 配置类
|
||||
dubbo:
|
||||
# Dubbo 应用配置
|
||||
application:
|
||||
name: user-service
|
||||
name: user-service-provider # 应用名
|
||||
# Dubbo 注册中心配
|
||||
registry:
|
||||
address: zookeeper://127.0.0.1:2181
|
||||
address: zookeeper://127.0.0.1:2181 # 注册中心地址。个鞥多注册中心,可见 http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html 文档。
|
||||
# Dubbo 服务提供者协议配置
|
||||
protocol:
|
||||
port: -1
|
||||
name: dubbo
|
||||
port: -1 # 协议端口。使用 -1 表示随机端口。
|
||||
name: dubbo # 使用 `dubbo://` 协议。更多协议,可见 http://dubbo.apache.org/zh-cn/docs/user/references/protocol/introduction.html 文档
|
||||
# Dubbo 服务提供者配置
|
||||
provider:
|
||||
timeout: 1000 # 【重要】远程服务调用超时时间,单位:毫秒。默认为 1000 毫秒,胖友可以根据自己业务修改
|
||||
UserRpcService:
|
||||
version: 1.0.0
|
||||
|
||||
Reference in New Issue
Block a user