结构

依赖
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<dubbo.version>3.0.7</dubbo.version>
<spring.version>5.3.17</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
接口
package ws.api;
public interface MyService {
String sayHello(String name);
}
实现
package ws.impl;
import org.apache.dubbo.config.annotation.DubboService;
import ws.api.MyService;
@DubboService(version = "1.0.0")
public class MyServiceImp implements MyService {
public String sayHello(String name) {
System.out.println("greeting service received: " + name);
return "hello, " + name;
}
}
消费
package ws.action;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import ws.api.MyService;
@Component
public class Consumer {
@DubboReference(version = "1.0.0")
private MyService my;
public String doSayHello(String name) {
return my.sayHello(name);
}
}
启动
package ws;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import ws.action.Consumer;
public class ConsumerBootstrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
Consumer greetingServiceConsumer = context.getBean(Consumer.class);
String hello = greetingServiceConsumer.doSayHello("nacos");
System.out.println("result: " + hello);
}
@Configuration
@EnableDubbo(scanBasePackages = "ws.action")
@PropertySource("classpath:/spring/dubbo-consumer.properties")
@ComponentScan(basePackages = "ws.action")
static class ConsumerConfiguration {
}
}
package ws;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import ws.action.Consumer;
import java.util.concurrent.CountDownLatch;
public class ProviderBootstrap {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
context.start();
System.out.println("dubbo service started");
new CountDownLatch(1).await();
}
@Configuration
@EnableDubbo(scanBasePackages = "ws.impl")
@PropertySource("classpath:/spring/dubbo-provider.properties")
static class ProviderConfiguration {
}
}
配置
dubbo.application.name=nacos-registry-demo-consumer
dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
dubbo.consumer.timeout=3000
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
dubbo.protocol.host =192.168.31.80
dubbo.application.name=nacos-registry-demo-provider
dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.protocol.host =192.168.31.80
dubbo.provider.token=true
1万+




被折叠的 条评论
为什么被折叠?



