nacos-dubbo-springboot整合

先启动nacos服务

服务端代码:
  • application.properties配置:
nacos.config.server-addr=127.0.0.1:8848

 endpoint http://localhost:8080/actuator/nacos-config
 health http://localhost:8080/actuator/health
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

##dubbo config
spring.application.name=test
dubbo.registry.address=nacos://127.0.0.1:8848
dubbo.application.name=spring-boot-nacos
dubbo.scan.basePackages=com.alibaba.nacos.example.spring.boot.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1

  • pom文件配置:
 
        2.0.3.RELEASE
        0.2.1
        4.0.35.Final
    

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

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

        
            com.alibaba.boot
            nacos-config-spring-boot-starter
            ${nacos-config-spring-boot.version}
        

        
            com.alibaba.boot
            nacos-config-spring-boot-actuator
            ${nacos-config-spring-boot.version}
        

        
        
            com.alibaba
            dubbo-registry-nacos
            0.0.1
        

        
        
            com.alibaba
            dubbo
            2.6.5
        

        
        
            com.alibaba.spring
            spring-context-support
            1.0.2
        

        
            com.alibaba.boot
            dubbo-spring-boot-starter
            0.2.1.RELEASE
            
                
                com.alibaba
                dubbo
                
            
        

    
  • 启动类:
@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
    }
}
  • 定义dubbo接口:
package com.alibaba.nacos.interfaces;

/**
 * @author yanming
 * @version 1.0.0
 * @description
 * @date 2019/02/20 18:51
 **/
public interface ITestNacos {
    String getOrderFormat(Integer integer);
}

  • 注册dubbo服务:
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.nacos.interfaces.ITestNacos;

@Service(group = "test-nacos2", retries = 0, timeout = 10000)
public class TestNacosService implements ITestNacos {

    @Override
    public String getOrderFormat(Integer integer) {
       return "test001";
    }
}

  • 启动服务端,登陆nacos控制台,可以看到服务已经注册:
    在这里插入图片描述
客户端代码:
  • application.properties配置,pom文件配置与服务端相同,
  • 启动类:
@SpringBootApplication
public class NacosDubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosDubboConsumerApplication.class, args);
    }
}
  • 测试demo:

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.nacos.interfaces.ITestNacos;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author yanming
 * @version 1.0.0
 * @description
 * @date 2019/02/21 9:46
 **/
@Controller
@RequestMapping("consumer")
public class TestNacosConsumer {

    @Reference(group = "test-nacos2")
    ITestNacos iTestNacos;

    @RequestMapping(value = "test",method = RequestMethod.GET)
    @ResponseBody
    public String getCounsumerTest(){
        return iTestNacos.getOrderFormat(1);
    }
}
  • 启动客户端,
    在这里插入图片描述
  • 接口测试 :
    http://localhost:8082/consumer/test
  • 结果:
    test001
    说明服务正常调用

你可能感兴趣的:(nacos)