dubbo-registry-nacos
is a Dubbo's registry implementation integrating with
Nacos that is service registry server.
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.
<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>
package com.alibaba.dubbo.demo.service;
public interface DemoService {
String sayName(String name);
}
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);
}
}
## 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
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.
## 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
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.