整合spring-cloud-alibaba、dubbo和nacos注册中心微服务

此环境需要nacos本地单机模式启动:本地构建nacos 启动

此环境包含的模块:

  1. dubbo接口定义 (demo-dubbo-nacos-api)
  2. 服务生产者ServiceA (demo-dubbo-nacos-ServiceA)
  3. 服务消费者ServiceB (demo-dubbo-nacos-ServiceB)

整合spring-cloud-alibaba、dubbo和nacos注册中心微服务_第1张图片

1.dubbo接口定义

package com.fu;

public interface ServiceA {

    String greet(String name);
}

2.服务生产者ServiceA

pom配置:




    
        demo-dubbo-nacos
        com.fu
        1.0.0-SNAPSHOT
    
    4.0.0

    demo-dubbo-nacos-ServiceA

    demo-dubbo-nacos-ServiceA

    
        UTF-8
        1.7
        1.7

        Greenwich.SR2
        2.1.1.RELEASE
        2.1.1.RELEASE
        4.5.6
        2.1.3.RELEASE
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
            ${spring-boot-web.version}
        

        
        
            org.springframework.cloud
            spring-cloud-starter
            2.1.2.RELEASE
            
                
                    org.springframework.cloud
                    spring-cloud-context
                
            
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
            2.1.2.RELEASE
        


        
        
            com.fu
            demo-dubbo-nacos-api
            1.0.0-SNAPSHOT
        

        
        
            com.alibaba.cloud
            spring-cloud-alibaba-nacos-discovery
            2.1.1.RELEASE
            
                
                    org.springframework.cloud
                    spring-cloud-context
                
            
        

        
        
            org.springframework.cloud
            spring-cloud-context
            2.1.1.RELEASE
        

        
            junit
            junit
            4.11
            test
        
    




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

            
                org.apache.httpcomponents
                httpclient
                ${httpclient.version}
            
        

    

    

    

application.properties配置:

spring.application.name=demo-dubbo-nacos-ServiceA
#扫描的dubbo声明api的包路径
dubbo.scan.base-packages=com.fu.service
#dubbo协议
dubbo.protocol.name=dubbo
#dubbo端口号,-1自动递增,默认20880开始
dubbo.protocol.port=-1
#duboo采用spring cloud注册
dubbo.registry.address=spring-cloud://localhost
#nacos注册中心地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

如果遇到下面错误:检查pom里是否引入spring-boot-starter-web?

java.lang.IllegalStateException: failed to req API:/nacos/v1/ns/instance after all servers([localhost:8848]) tried: failed to req API:localhost:8848/nacos/v1/ns/instance. code:500 msg: caused: instance format invalid: Your IP address is spelled incorrectly;

3.服务消费者ServiceB

pom配置:




    
        demo-dubbo-nacos
        com.fu
        1.0.0-SNAPSHOT
    
    4.0.0

    demo-dubbo-nacos-ServiceB

    demo-dubbo-nacos-ServiceB
    
    http://www.example.com

    
        UTF-8
        1.7
        1.7
        Greenwich.SR2
        2.1.1.RELEASE
        2.1.1.RELEASE
        4.5.6
        2.1.3.RELEASE
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
            ${spring-boot-web.version}
        

        
        
            org.springframework.cloud
            spring-cloud-starter
            2.1.2.RELEASE
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
            2.1.2.RELEASE
        

        
        
            com.fu
            demo-dubbo-nacos-api
            1.0.0-SNAPSHOT
        

        
        
            com.alibaba.cloud
            spring-cloud-alibaba-nacos-discovery
            2.1.1.RELEASE
            
                
                    org.springframework.cloud
                    spring-cloud-context
                
            
        

        
        
            org.springframework.cloud
            spring-cloud-context
            2.1.1.RELEASE
        

        
            org.apache.httpcomponents
            httpclient
        

        
            junit
            junit
            4.11
            test
        
    


    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
                org.apache.httpcomponents
                httpclient
                ${httpclient.version}
            
        

    

    

    

application.properties配置:

server.port=8081
spring.application.name=demo-dubbo-nacos-ServiceB
#订阅的服务列表
dubbo.cloud.subscribed-services=demo-dubbo-nacos-ServiceA
#扫描的dubbo声明api的包路径
dubbo.scan.base-packages=com.fu.service
#nacos注册中心地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

测试类TestController

package com.fu.web;


import com.fu.ServiceA;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {


    /**
     * dubbo注解,配置版本号,接口,集群容错,默认是否健康检查(false不检查,默认true)
     */
    @Reference(
            version = "1.0.0",
            interfaceClass = ServiceA.class,
            cluster = "failfast",
            check = false
    )
    private ServiceA serviceA;

    @GetMapping("/greet")
    public String greet(String name) {
        return serviceA.greet(name);
    }

}

测试效果: http://127.0.0.1:8081/greet?name=Leo9888 

整合spring-cloud-alibaba、dubbo和nacos注册中心微服务_第2张图片

 

可以本地导入编辑器直接运行的源码下载地址:

https://download.csdn.net/download/XinTeng2012/12793393

建议自己亲自动手整合,有利于理解每个模块和配置的作用。

git地址 : https://github.com/MrZhengliang/demo-dubbo-nacos   如果有用,希望给个Star。


永远保持对技术的好奇心和热情。

欢迎各位留言交流,如有不正确的地方,请予以指正。


 

你可能感兴趣的:(分布式,springcloud,微服务架构,springcloud,nacos,springboot,dubbo+nacos)