Skip to content

mercyblitz/dubbo-registry-nacos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dubbo-registry-nacos

dubbo-registry-nacos is a Dubbo's registry implementation integrating with Nacos that is service registry server.

Prerequisite

Before you integrate dubbo-registry-nacos into your Dubbo project, you need to start a Nacos server in the backend. Refer to Nacos Quick Start for instructions on how to start a Nacos server.

Getting started

Maven dependency

<dependencies>

    ...
    
    <!-- Dubbo dependency -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>[2.7.5,)</version>
    </dependency>
    
    <!-- Dubbo Nacos registry dependency -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo-registry-nacos</artifactId>
        <version>2.7.7</version>
    </dependency>   
    
    <!-- Keep latest Nacos client version -->
    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
        <version>1.2.1</version>
    </dependency>
    
    ...
    
</dependencies>

Define service interface

package com.alibaba.dubbo.demo.service;

public interface DemoService {
    String sayName(String name);
}

Implement service interface for the provider

package com.alibaba.dubbo.demo.service;

@Service(version = "${demo.service.version}")
public class DefaultService implements DemoService {

    @Value("${demo.service.name}")
    private String serviceName;

    public String sayName(String name) {
        RpcContext rpcContext = RpcContext.getContext();
        return String.format("Service [name :%s , port : %d] %s(\"%s\") : Hello,%s",
                serviceName,
                rpcContext.getLocalPort(),
                rpcContext.getMethodName(),
                name,
                name);
    }
}

Define service provider's configuration

## application
dubbo.application.name = dubbo-provider-demo

## Nacos registry address
dubbo.registry.address = nacos://127.0.0.1:8848/?username=nacos&password=nacos

## Dubbo Protocol
dubbo.protocol.name = dubbo
dubbo.protocol.port = -1

# Provider @Service version
demo.service.version=1.0.0
demo.service.name = demoService

Start service provider

package com.alibaba.dubbo.demo.provider;

@EnableDubbo(scanBasePackages = "com.alibaba.dubbo.demo.service")
@PropertySource(value = "classpath:/provider-config.properties")
public class DemoServiceProviderBootstrap {

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(DemoServiceProviderBootstrap.class);
        context.refresh();
        System.out.println("DemoService provider starting...");
        System.in.read();
    }
}

See DemoServiceProviderBootstrap.java on GitHub.

Define service consumer's configuration

## Dubbo Application info
dubbo.application.name = dubbo-consumer-demo

## Nacos registry address
dubbo.registry.address = nacos://127.0.0.1:8848/?username=nacos&password=nacos

# @Reference version
demo.service.version= 1.0.0

Start service consumer

package com.alibaba.dubbo.demo.consumer;

@EnableDubbo
@PropertySource(value = "classpath:/consumer-config.properties")
public class DemoServiceConsumerBootstrap {

    @Reference(version = "${demo.service.version}")
    private DemoService demoService;

    @PostConstruct
    public void init() {
        for (int i = 0; i < 10; i++) {
            System.out.println(demoService.sayName("Mercy"));
        }
    }

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(DemoServiceConsumerBootstrap.class);
        context.refresh();
        context.close();
    }
}

See DemoServiceConsumerBootstrap.java on GitHub.

Advanced

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages