目錄
創建項目結構
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;
...
}
然後,我們的項目就可以愉悅地跑起來了!