https://github.com/mercyblitz/dubbo-registry-nacos
https://github.com/mercyblitz/dubbo-registry-nacos
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/mercyblitz/dubbo-registry-nacos
- Owner: mercyblitz
- License: apache-2.0
- Created: 2018-10-29T03:19:34.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-02-02T23:11:19.000Z (over 1 year ago)
- Last Synced: 2025-07-01T03:57:40.555Z (4 months ago)
- Language: Java
- Size: 54.7 KB
- Stars: 48
- Watchers: 1
- Forks: 41
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dubbo-registry-nacos
`dubbo-registry-nacos` is a [Dubbo](https://github.com/apache/dubbo)'s registry implementation integrating with
[Nacos](https://github.com/alibaba/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](https://nacos.io/en-us/docs/quick-start.html) for instructions on how to
start a Nacos server.
## Getting started
### Maven dependency
```xml
...
org.apache.dubbo
dubbo
[2.7.5,)
com.alibaba
dubbo-registry-nacos
2.7.7
com.alibaba.nacos
nacos-client
1.2.1
...
```
### Define service interface
```java
package com.alibaba.dubbo.demo.service;
public interface DemoService {
String sayName(String name);
}
```
### Implement service interface for the provider
```java
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
```properties
## 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
```java
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`](src/test/java/com/alibaba/dubbo/demo/provider/DemoServiceProviderBootstrap.java) on GitHub.
### Define service consumer's configuration
```properties
## 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
```java
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`](src/test/java/com/alibaba/dubbo/demo/consumer/DemoServiceConsumerBootstrap.java) on GitHub.
## Advanced