博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 使用API注解方式整合 dubbo
阅读量:3906 次
发布时间:2019-05-23

本文共 5267 字,大约阅读时间需要 17 分钟。

目录

 


创建项目结构

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

log4j
log4j
1.2.17
log4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.logfile=org.apache.log4j.FileAppenderlog4j.appender.logfile.File=target/spring.loglog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.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数据库,模拟数据

@Componentpublic class UserAddressDao {    public UserAddressDTO getUserAddressById(Integer userId) {        Map
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

org.projectlombok
lombok
true
@Getter@Setter@AllArgsConstructorpublic 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 {    // 
@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; } //
@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; } //
@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@EnableDubbopublic class DubboConfig {    // 
@Bean public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("gmall-web-consumer"); return applicationConfig; } //
@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;    ...}

然后,我们的项目就可以愉悦地跑起来了!

转载地址:http://kiqen.baihongyu.com/

你可能感兴趣的文章
Bloom Filter - Math deduce
查看>>
Bit Byte Megabyte Gigabyte
查看>>
Bits Bytes and Words
查看>>
Python yield and generator
查看>>
Python yield and iterables
查看>>
Python string find
查看>>
del Statement
查看>>
Python Dict all
查看>>
Python Rate Limiter
查看>>
Python list to string
查看>>
Python list dict iteration
查看>>
Linux basic knowledge
查看>>
Difference Between Hard & Soft Links
查看>>
Linux Hard link and Symbolic link
查看>>
Hard Link Vs Soft Link
查看>>
redis brief intro
查看>>
mongo db brief intro
查看>>
Kafka basic intro
查看>>
Python multiprocessing
查看>>
Python urlib vs urlib2
查看>>