SpringBoot2.1.9+dubbo2.7.3+Nacos1.1.4构建你的微服务体系

使用最新版的DUBBO构建你的微服务体系!

NACOS注册中心

  • 从github下载最新版本的nacos

  • 上传至服务器并解压

  • 单机启动sh startup.sh -m standalone

  • nacos 控制台访问地址http://192.168.136.129:8848/nacos,使用账号nacos/nacos登录并访问SpringBoot2.1.9+dubbo2.7.3+Nacos1.1.4构建你的微服务体系_第1张图片

项目框架

本次案例包含三个组件

  • 公共接口层 dubbo-api

  • 生产者  dubbo-provider

  • 消费者 dubbo-consumer 代码目录如下:
    SpringBoot2.1.9+dubbo2.7.3+Nacos1.1.4构建你的微服务体系_第2张图片

父目录dubbo-demo

父目录主要是定义公共组件依赖,版本号,pom文件如下


    4.0.0
    pom

    
        dubbo-api
        dubbo-provider
        dubbo-consumer
    

    com.jianzh5
    dubbo-demo
    1.0-SNAPSHOT


    
        1.8
        4.0.35.Final
        2.1.9.RELEASE
        2.7.3
        1.1.4
    


    
        
            
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            

            
            
                org.apache.dubbo
                dubbo-dependencies-bom
                ${dubbo.version}
                pom
                import
            

            
            
                org.apache.dubbo
                dubbo-registry-nacos
                ${dubbo.version}
            

            
                com.alibaba.nacos
                nacos-client
                ${nacos-client.version}
            


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

            
                org.apache.dubbo
                dubbo
                ${dubbo.version}
                
                    
                        org.springframework
                        spring
                    
                    
                        javax.servlet
                        servlet-api
                    
                    
                        log4j
                        log4j
                    
                
            

            
            
                com.jianzh5
                dubbo-api
                1.0-SNAPSHOT
            

        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring-boot.version}
                
                    
                        
                            repackage
                        
                    
                
            
        
    


在pom文件中直接依赖接口,这样生产者和消费者都可以使用了

接口层dubbo-api定义

  • pom


    
        dubbo-demo
        com.jianzh5
        1.0-SNAPSHOT
    
    4.0.0
    com.jianzh5
    dubbo-api
    jar

  • 定义真实的接口

/**
 * @author jianzh5
 * @date 2019/11/5 10:45
 */
public interface HelloService {
    String sayHello();
}

生产者dubbo-provider

  • pom


    
        dubbo-demo
        com.jianzh5
        1.0-SNAPSHOT
    
    4.0.0

    com.jianzh5
    dubbo-provider

    
        
            org.springframework.boot
            spring-boot-starter
        

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

        
        
            org.apache.dubbo
            dubbo-registry-nacos
        


        
            com.alibaba.nacos
            nacos-client
        

        
            com.jianzh5
            dubbo-api
        
    

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

  • 配置文件application.xml

dubbo.application.name = dubbo-provider
dubbo.registry.address = nacos://192.168.136.129:8848
dubbo.scan.base-packages=com.jianzh5.provider.service.impl
dubbo.protocol.port=20881
dubbo.protocol.name=dubbo
  • 接口实现

@Service
public class HelloServiceImpl implements HelloService {

    public String sayHello() {
        return "欢迎关注微信公众号:JAVA日知录";
    }

}

注意这里的@service引用的是org.apache.dubbo.config.annotation.Service,不要引用错了

  • 启动类

@SpringBootApplication
@EnableDubbo
public class PrivoderBootstrap {
    public static void main(String[] args) {
        SpringApplication.run(PrivoderBootstrap.class, args);
    }
}

需要引入@EnableDubbo注解

消费者dubbo-consumer

  • pom


    
        dubbo-demo
        com.jianzh5
        1.0-SNAPSHOT
    
    4.0.0

    com.jianzh5
    dubbo-consumer

    

        
            org.springframework.boot
            spring-boot-starter
        

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

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

        
        
            org.apache.dubbo
            dubbo-registry-nacos
        


        
            com.alibaba.nacos
            nacos-client
        

        
            com.jianzh5
            dubbo-api
        
    


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


  • 配置文件application.properties

spring.application.name=dubbo-consumer
dubbo.registry.address = nacos://192.168.136.129:8848
server.port=9090
  • 消费者HelloController

/**
 * @author jianzh5
 * @date 2019/11/5 11:29
 */
@RestController
public class HelloController {

    @Reference
    private HelloService helloService;

    @GetMapping("/sayHello")
    public String sayHello(){
        return helloService.sayHello();
    }

}

使用@Reference注解注入HelloService

  • 接口启动类

@SpringBootApplication
public class ConsumerBootstrap {

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

}

项目启动

  • 启动生产者 dubbo-provider

  • 启动消费者 dubbo-consumer

  • 查看nacos控制台,观察是否注册SpringBoot2.1.9+dubbo2.7.3+Nacos1.1.4构建你的微服务体系_第3张图片

  • 访问浏览器http://localhost:9090/sayHello查看接口返回

 

你可能感兴趣的:(微服务架构)