网站建设资讯

NEWS

网站建设资讯

springboot2.2.2集成dubbo的实现方法

最近在学习dubbo,想着作一些笔记,从来没有在csdn上面写过博客,今天献出第一次,哈哈,直接上代码

目前创新互联建站已为1000多家的企业提供了网站建设、域名、雅安服务器托管成都网站托管、企业网站设计、青山湖网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

一、创建父工程

<?xml version="1.0" encoding="UTF-8"?>

  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.2.2.RELEASE
    
  

  com.dubbo
  demo01
  1.0.0
  pom
  Spring Boot2.x 整合 dubbo

  
    api
    provider
    consumer
  

  
  
    UTF-8
    UTF-8
    1.8
    2.7.5
    4.2.0
    
    3.4.12
  

  
  
  
    
      
        org.apache.dubbo
        dubbo-spring-boot-starter
        ${dubbo.version}
      
  
      
        org.apache.curator
        curator-recipes
        ${curator.version}
      
  
      
        org.apache.zookeeper
        zookeeper
        ${zookeeper.version}
      
    
  

  
    
      org.springframework.boot
      spring-boot-devtools
      runtime
      true
    
    
    
      org.projectlombok
      lombok
      true
    
  


二、创建提供者与消费者共用的api

该模块没有什么好说的,提供者和消费者都需要使用的接口api,提供者和消费者都需要引入该模块

<?xml version="1.0" encoding="UTF-8"?>

  
    demo01
    com.dubbo
    1.0.0
  
  4.0.0

  api




// 注解都是lombok的,真的很方便
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class User implements Serializable {
  private Integer id;
  private String name;
  private Integer age;
}
public interface UserService {
  User getUserById(Integer id);
}

三、创建提供者

<?xml version="1.0" encoding="UTF-8"?>

  4.0.0
  
    com.dubbo
    1.0.0
    demo01
  

  com.dubbo
  provider
  0.0.1-SNAPSHOT
  provider
  Demo project for Spring Boot

  
    
      org.springframework.boot
      spring-boot-starter-web
    

    
      org.apache.dubbo
      dubbo-spring-boot-starter
    

    
      org.apache.curator
      curator-recipes
    

    
      org.apache.zookeeper
      zookeeper
    
    
    
      com.dubbo
      api
      1.0.0
    

    
      org.springframework.boot
      spring-boot-starter-test
      test
      
        
          org.junit.vintage
          junit-vintage-engine
        
      
    
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  



dubbo:
 application:
  # 应用名称
  name: user-provider
 protocol:
  # 协议名称
  name: dubbo
  # 协议端口
  port: 20880
 registry:
  # 注册中心地址
  address: zookeeper://192.168.104.231:2181

@SpringBootApplication
// 提供服务的应用必须配置此项
@DubboComponentScan("com.dubbo.provider.service")
public class ProviderApplication {

  public static void main(String[] args) {
    SpringApplication.run(ProviderApplication.class, args);
  }

}

@Component
// 该service是org.apache.dubbo.config.annotation.Service
@Service
public class UserServiceImpl implements UserService {
  @Override
  public User getUserById(Integer id) {
    User user = User.builder()
        .id(id)
        .name("张三")
        .age(20 + id)
        .build();

    return user;
  }
}

四、创建消费者

<?xml version="1.0" encoding="UTF-8"?>

  4.0.0
  
    com.dubbo
    1.0.0
    demo01
  

  com.dubbo
  consumer
  0.0.1-SNAPSHOT
  consumer
  Demo project for Spring Boot

  
    
      org.springframework.boot
      spring-boot-starter-web
    

    
      org.apache.dubbo
      dubbo-spring-boot-starter
    

    
      org.apache.curator
      curator-recipes
    

    
      org.apache.zookeeper
      zookeeper
    

    
    
      com.dubbo
      api
      1.0.0
    

    
      org.springframework.boot
      spring-boot-starter-test
      test
      
        
          org.junit.vintage
          junit-vintage-engine
        
      
    
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  



# 端口
server:
 port: 8081
dubbo:
 application:
  name: user-consumer
 protocol:
  name: dubbo
  port: 20880
 registry:
  address: zookeeper://192.168.104.231:2181

@SpringBootApplication

public class ConsumerApplication {

  public static void main(String[] args) {
    SpringApplication.run(ConsumerApplication.class, args);
  }

}
@RestController
@RequestMapping("/user")
public class ConsumerController {
  @Reference
  private UserService userService;

  @GetMapping("/{id}")
  public User getUserById(@PathVariable int id) {
    return userService.getUserById(id);
  }
}

五、启动并访问

先启动provider,启动结果如下

springboot2.2.2集成dubbo的实现方法

启动consumer,启动结果如下

springboot2.2.2集成dubbo的实现方法

浏览器访问:http://localhost:8081/user/4

springboot2.2.2集成dubbo的实现方法

好了,到此就完成了最基本的springboot与dubbo的整合,更多的dubbo的api请查阅 dubbo官方文档

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


文章标题:springboot2.2.2集成dubbo的实现方法
文章地址:http://cdweb.net/article/piccid.html