目录
创建项目结构
1. 创建一个空项目,并且创建两个模块
2. gamll-user-provider,Spring Initializer创建的非web项目,包含service,service impl,dao和entity,所以不需要创建web项目
3. gmall-web-consumer,Spring Initializer创建的web项目,只包含controller层,通过调用gamll-user-provider的服务来实现业务需求
4. 项目结构
5. gamll-user-provider
分别创建service,impl,domain,dao,config(dubbo的配置文件类)包,并写入相关实现
6. gmall-web-consumer
分别创建action和config(dubbo的配置文件类)包
7. 导入相关依赖(两个模块都需要),log4j和log4j.properties
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
具体实现
gamll-user-provider
1. service
public interface UserService {
UserAddressDTO getUserAddress(Integer userId);
}
2. impl
public class UserServiceImpl implements UserService {
@Autowired
private UserAddressDao userAddressDao;
@Override
public UserAddressDTO getUserAddress(Integer userId) {
return userAddressDao.getUserAddressById(userId);
}
}
3. dao
没有连接mysql数据库,模拟数据
@Component
public class UserAddressDao {
public UserAddressDTO getUserAddressById(Integer userId) {
Map<Integer, UserAddressDTO> userAddressDTOMap = new HashMap<>(2);
UserAddressDTO userAddressDTO1 = new UserAddressDTO(1, "四川成都", 1, null, "13328532951", "true");
UserAddressDTO userAddressDTO2 = new UserAddressDTO(2, "四川绵阳", 2, null, "13328532953", "false");
userAddressDTOMap.put(1, userAddressDTO1);
userAddressDTOMap.put(2, userAddressDTO2);
return userAddressDTOMap.get(userId);
}
}
4. entity
使用的依赖是Lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
@Getter
@Setter
@AllArgsConstructor
public class UserAddressDTO implements Serializable {
private Integer id;
private String userAddress;
private Integer userId;
private String consignee;
private String phoneNum;
private String isDefault;
}
gmall-web-consumer
action
@RestController
@RequestMapping("/order")
public class OrderAction {
private UserService userService;
/**
* 初始化订单,查询用户的所有地址并返回
* @param userId
* @return
*/
@GetMapping
public UserAddressDTO initOrder(Integer userId){
return userService.getUserAddress(userId);
}
}
截止目前,项目的基本架构就搭建好了,当然,这个时候,项目还跑不起来,因为还没有配置dubbo,接下来开始配置dubbo。
dubbo配置
gamll-user-provider
1. 在config包下新建DubboConfig类,配置文件和xml配置同理
@Configuration
@EnableDubbo(scanBasePackages = "com.iceink")
public class DubboConfig {
// <dubbo:application name="gmall-user-provider" />
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("gmall-user-provider");
return applicationConfig;
}
@Bean
public ProviderConfig providerConfig() {
ProviderConfig providerConfig = new ProviderConfig();
providerConfig.setWait(3000);
providerConfig.setPayload(128 * 1024 * 1024);
providerConfig.setThreads(400);
return providerConfig;
}
/**
* 推荐在provider上尽量多配置consumer端属性,consumer会使用provider配置的值作为缺省值
* @return
*/
@Bean
public ConsumerConfig consumerConfig() {
ConsumerConfig consumerConfig = new ConsumerConfig();
// 由于网络或服务端不可靠,会出现一种不确定的中间状态(超时),必须设置超时时间
consumerConfig.setTimeout(20000);
consumerConfig.setRetries(0);
consumerConfig.setCheck(false);
return consumerConfig;
}
// <dubbo:registry address="zookeeper://106.15.176.xxx:2181?backup=106.15.176.xxx:2182,106.15.176.xxx:2183" />
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("106.15.176.xxx:2181,106.15.176.xxx:2182,106.15.176.xxx:2183");
return registryConfig;
}
// <dubbo:protocol name="dubbo" port="20880" />
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setPort(20880);
protocolConfig.setName("dubbo");
protocolConfig.setThreads(100);
return protocolConfig;
}
}
2. 向外提供服务
修改UserServiceImpl类,新增dubbo注解
import com.alibaba.dubbo.config.annotation.Service;
@Service(version = "1.0")
public class UserServiceImpl implements UserService {
...
}
gmall-web-consumer
1. 新增DubboConfig类
@Configuration
@EnableDubbo
public class DubboConfig {
// <dubbo:application name="gmall-web-comsumer" />
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("gmall-web-consumer");
return applicationConfig;
}
// <dubbo:registry address="zookeeper://106.15.176.xxx:2181?backup=106.15.176.xxx:2182,106.15.176.xxx:2183" />
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("106.15.176.xxx:2181,106.15.176.xxx:2182,106.15.176.xxx:2183");
return registryConfig;
}
}
2. 订阅服务
修改OrderAction类,新增@Reference(version = "1.0")注解
@RestController
@RequestMapping("/order")
public class OrderAction {
@Reference(version = "1.0")
private UserService userService;
...
}
然后,我们的项目就可以愉悦地跑起来了!