示例
- 項目構(gòu)建
????新建一個Maven工程,該工程為父工程破婆,只要包含兩個模塊劳闹,一個是服務(wù)提供方模塊dubbo-provider掘鄙,另一個是服務(wù)消費方dubbo-consumer耘戚。父工程主要引入相關(guān)依賴,其pom文件為:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hyn.dubbo</groupId>
<artifactId>dubbo-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-provider</module>d
<module>dubbo-consumer</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- zookeeper client依賴 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
</project>
- 服務(wù)提供方
在服務(wù)提供主要包括兩部分操漠,一個是暴露服務(wù)收津,一個是服務(wù)實現(xiàn)。
暴露服務(wù):即我們平常開發(fā)中所使用的的接口浊伙,這里我們創(chuàng)建一個 ProviderService 的接口撞秋,主要包括一個返回字符串方法sayHello。
package com.hyn.dubbo.provider.service;
public interface ProviderService {
String sayHello(String name);
}
????服務(wù)實現(xiàn):服務(wù)實現(xiàn)嚣鄙,與我們平常的服務(wù)一樣吻贿,對接口進(jìn)行實現(xiàn),比較特別的是哑子,我們這里需要使用到Dubbo 的 @Service 注解
package com.hyn.dubbo.provider.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.hyn.dubbo.provider.service.ProviderService;
import org.springframework.stereotype.Component;
/**
* interfaceClass 指定當(dāng)前要暴露的服務(wù)
**/
@Service(interfaceClass = ProviderService.class)
@Component
public class ProviderServiceImpl implements ProviderService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
工程啟動服務(wù)ProviderApplication
package com.hyn.dubbo.provider;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
@SpringBootApplication
@EnableDubboConfiguration
public class ProviderApplication{
public static void main(String[] args) throws IOException {
SpringApplication.run(ProviderApplication.class, args);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.hyn.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
</project>
applicatioon.properties
server.port=8080 --工程啟動端口
spring.dubbo.application.name=provider --服務(wù)名稱
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 --注冊中心地址
spring.dubbo.protocol.name=dubbo --協(xié)議名稱(dubbo 協(xié)議)
spring.dubbo.protocol.port=20880 --協(xié)議端口
總體架構(gòu)
- 服務(wù)消費方
服務(wù)調(diào)用
package com.hyn.dubbo.consumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.hyn.dubbo.provider.service.ProviderService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
//用dubbo的@Reference注解去連接提供者提供服務(wù)
@Reference
private ProviderService providerService;
@RequestMapping("/hello/{name}")
public String sayHello(@PathVariable String name) {
return providerService.sayHello(name);
}
}
工程啟動服務(wù)ConsumerApplication
package com.hyn.dubbo.consumer;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.hyn.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.hyn.dubbo</groupId>
<artifactId>dubbo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
application.properties
server.port=8081 --工程啟動端口
server.context-path=/
spring.dubbo.application.name=consumer --服務(wù)名稱
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 --注冊中心地址
spring.dubbo.protocol.name=dubbo --協(xié)議名稱(dubbo 協(xié)議)
spring.dubbo.protocol.port=20880 --協(xié)議端口
spring.dubbo.consumer.check=false --啟動時檢查
spring.dubbo.reference.loadbalance=roundrobin --負(fù)載均衡(輪詢機(jī)制)
總體結(jié)構(gòu)
-
結(jié)果