基于dubbo RPC的微服务式架构
Dubbo Hello World
环境
SpringBoot + dubbo
Pom.xml 依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xjz</groupId>
<artifactId>DubboProvider</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.7.1</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Aapche Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
服务提供方 provider
配置文件
server.port=8082
spring.application.name=DubboProvider
dubbo.scan.base-packages=com.xjz.service
dubbo.protocol.name=dubbo
dubbo.protocol.host=192.168.1.109
dubbo.protocol.port=12345
dubbo.registry.address=zookeeper://192.168.199.6:2181
服务接口
package com.xjz.service;
/**
* @Auther:
* @Date: 2020/8/16 - 08 - 16 - 18:22
* @Description: com.xjz.service
* @version: 1.0
*/
public interface IDubboService {
String say(String name);
}
接口实现
package com.xjz.service;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
/**
* @Auther:
* @Date: 2020/8/16 - 08 - 16 - 18:18
* @Description: com.xjz.service
* @version: 1.0
*/
@Service(version = "1.0.0",timeout = 5000,interfaceClass = IDubboService.class)
@Component
public class DubboServiceImpl implements IDubboService {
@Override
public String say(String name){
System.out.println(name);
return name+" sya Hi !";
}
}
服务消费方 consumer 配置
server.port=8081
spring.application.name=DemoConsumer
dubbo.registry.address=zookeeper://192.168.199.6:2181
自动注入
package com.xjz.main;
import com.xjz.service.IDubboService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Auther:
* @Date: 2020/8/15 - 08 - 15 - 17:06
* @Description: com.xjz.demo
* @version: 1.0
*/
@SpringBootApplication
@RestController
public class DemoApplication {
@Reference(version = "1.0.0")
IDubboService dubboService;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public String say(@RequestParam(value = "name", defaultValue = "World") String name) {
return dubboService.say(name);
}
}
接口
/**
* @Auther:
* @Date: 2020/8/16 - 08 - 16 - 18:22
* @Description: com.xjz.service
* @version: 1.0
*/
public interface IDubboService {
String say(String name);
}