项目结构
相关代码
EchoService
public interface EchoService {
String echo(String msg);
}
DefaultEchoService
@Service(version = "${echo.service.version}")
public class DefaultEchoService implements EchoService {
@Value("${echo.service.name}")
private String serviceName;
@Override
public String echo(String msg) {
RpcContext rpcContext = RpcContext.getContext();
return String.format("Service [name :%s , port : %d] %s say : Hello,%s",
serviceName,
rpcContext.getLocalPort(),
rpcContext.getMethodName(),
msg);
}
}
EchoServiceConsumerBootstrap
/**
* TODO
*
* @auther xh
* @date 3/14/19 10:25 AM
*/
@EnableDubbo
@EnableNacosConfig
@PropertySource(value = "classpath:configs/consumer-config.properties")
public class EchoServiceConsumerBootstrap {
@Reference(version = "${echo.service.version}")
private EchoService echoService;
@PostConstruct
public void init() {
for (int i = 0; i < 10; i++) {
System.err.println(echoService.echo("hello "));
}
}
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(EchoServiceConsumerBootstrap.class);
context.refresh();
context.close();
}
}
EchoServiceProviderBootstrap
/**
* TODO
*
* @auther xh
* @date 3/14/19 10:22 AM
*/
@EnableDubbo(scanBasePackages = "provider")
@PropertySource(value = "classpath:configs/provider-config.properties")
public class EchoServiceProviderBootstrap {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(EchoServiceProviderBootstrap.class);
context.refresh();
System.out.println("EchoService 启动...");
System.in.read();
}
}
pom.xml
...
org.springframework
spring-context
4.3.22.RELEASE
com.alibaba
dubbo
2.6.6
com.alibaba.spring
spring-context-support
1.0.2
ch.qos.logback
logback-classic
1.2.3
io.netty
netty-all
4.0.35.Final
com.alibaba
dubbo-registry-nacos
0.0.1
com.alibaba.nacos
nacos-spring-context
0.2.3-RC1
...
注册中心(配置文件在本地,注册中心指向Nocas)
consumer-config.properties:
## Dubbo Application info
dubbo.application.name=dubbo-consumer-demo
## Nacos registry address
dubbo.registry.address=nacos://127.0.0.1:8848
# @Reference version
echo.service.version=1.0.0
provider-config.properties:
## application
dubbo.application.name = dubbo-provider-demo
## Nacos registry address
dubbo.registry.address = nacos://127.0.0.1:8848
## Dubbo Protocol
dubbo.protocol.name = dubbo
dubbo.protocol.port = -1
# Provider @Service version
echo.service.version = 1.0.0
echo.service.name = EchoService
配置中心(配置文件也在Nacos上,注解中填写Nacos地址和配置文件名)
EchoServiceConsumerConfigBootstrap:
/**
* TODO
*
* @auther xh
* @date 3/18/19 10:23 AM
*/
@EnableDubbo
@EnableNacosConfig // 激活 Nacos 配置
@NacosPropertySource(dataId = "nacos-consumer-2.properties")
public class EchoServiceConsumerConfigBootstrap {
static {
System.setProperty("nacos.server-addr", "127.0.0.1:8848");
}
@Reference(version = "${echo.service.version}")
private EchoService echoService;
@NacosConfigListener(dataId = "nacos-consumer-2.properties")
public void onChange(String properties) {
System.out.println("onChange(String) : " + properties);
}
@PostConstruct
public void init() {
for (int i = 0; i < 10; i++) {
System.err.println(echoService.echo("hello "));
}
}
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(EchoServiceConsumerConfigBootstrap.class);
context.refresh();
System.out.println("服务消费者已启动...");
System.in.read();
context.close();
}
}
EchoServiceProviderConfigBootstrap:
/**
* TODO
*
* @auther xh
* @date 3/18/19 10:23 AM
*/
@EnableDubbo(scanBasePackages = "provider")
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848")) // 激活 Nacos 配置
@NacosPropertySource(dataId = "nacos-provider-2.properties")
public class EchoServiceProviderConfigBootstrap {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(EchoServiceProviderConfigBootstrap.class);
context.refresh();
System.out.println("EchoService 启动...");
System.in.read();
}
}