Dubbo簡單案例
編寫一個接口
HelloService.java
public interface HelloService {
String sayHello(String name);
}
編寫服務端
編寫接口的實作類
HelloServiceImpl.java
package com.sherlock.service.impl;
import com.sherlock.service.HelloService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
編寫啟動類
ProviderMain.java
package com.sherlock;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.io.IOException;
public class ProviderMain {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
context.start();
// 讓程式停在這裡
System.in.read();
}
/**
@EnableDubbo 啟動dubbo 掃描實作類的包
@Configuration 讓類成為配置類
@PropertySource 讀取指定位置的配置檔案
*/
@EnableDubbo(scanBasePackages="com.sherlock.service.impl")
@Configuration
@PropertySource("classpath:/dubbo-provider.properties")
static class ProviderConfiguration{
/**
配置 注冊中心的資訊
*/
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
// 使用zookeeper為注冊中心
registryConfig.setAddress("zookeeper://127.0.0.1:2181?timeout=10000");
return registryConfig;
}
}
}
dubbo-provider.properties檔案
dubbo.application.name=service-provider4
dubbo.protocol.name=dubbo
dubbo.protocol.port=20889
編寫消費端
編寫一個元件類
ConsumerComponent.java
package com.sherlock.bean;
import com.sherlock.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
@Component
public class ConsumerComponent {
@Reference
private HelloService helloService;
public String SysHello(String name) {
return helloService.sayHello(name);
}
}
編寫消費端的啟動類
package com.sherlock;
import com.sherlock.bean.ConsumerComponent;
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 java.io.IOException;
public class ConsumerMain {
public static void main(String[] args) throws IOException, InterruptedException {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
annotationConfigApplicationContext.start();
// 擷取bean對象
ConsumerComponent bean = annotationConfigApplicationContext.getBean(ConsumerComponent.class);
System.out.println(bean);
while (true) {
// 隻有當有輸入時, 才會執行
System.in.read();
String sysHello = bean.SysHello("sherlock");
System.out.println(sysHello);
}
}
/**
@Configuration 标注此類為配置類
@ComponentScan 掃描包
@EnableDubbo 啟動dubbo
@PropertySource 讀取配置檔案
*/
@Configuration
@ComponentScan("com.sherlock")
@EnableDubbo
@PropertySource("classpath:/dubbo-consumer.properties")
static class ConsumerConfiguration {
}
}
dubbo-consumer.properties
dubbo.application.name=service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.timeout=4000
測試
首先本地先啟動zookeeper,然後在啟動服務端,最後啟動消費端, 輸入任意字元加回車,就可以看到列印資訊了
POM檔案
父工程pom檔案
<groupId>com.sherlock</groupId>
<artifactId>dubbo-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-api</module>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
</modules>
<packaging>pom</packaging>
<properties>
<dubbo.version>2.7.5</dubbo.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-common</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-netty4</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-hessian2</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 日志配置 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<!-- json資料化轉換 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>
提供者pom檔案
<dependencies>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.sherlock</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-netty4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-hessian2</artifactId>
</dependency>
</dependencies>
消費者pom檔案
<dependencies>
<dependency>
<groupId>com.sherlock</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-netty4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-hessian2</artifactId>
</dependency>
</dependencies>