dubbo介紹
優秀的 RPC 服務治理框架,直接查看 官網.
nacos 介紹
作爲 註冊中心 和 參數配置中心使用, 本次作爲dubbo 服務註冊發現中心,介紹查看官網.
spring boot 註解方式配置
本次測試環境
springboot 2.2.6
dubbo 2.7.7
通過反覆查看官網和官方demo, 其實基本可以不用 dubbo-spring-boot-starter 配置dubbo,dubbo本身就支持註解方式配置。
maven 整體工程目錄如下
工程本身即是服務提供者也是服務調用者
配置的 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.middol</groupId>
<artifactId>dubbo-test1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<dubbo.version>2.7.7</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
</project>
application.yml 配置如下
server:
port: 8081
# log詳細配置請查看 logback-spring.xml, springboot可自動加載 resources/logback-spring.xml
logging:
level:
root: INFO
#spring:
# main:
# allow-bean-definition-overriding: true
dubbo:
application:
name: dubbo-samples-annotation
# 註冊中心配置。對應的配置類: org.apache.dubbo.config.RegistryConfig。同時如果有多個不同的註冊中心,
# 可以聲明多個 <dubbo:registry> 標籤,並在 <dubbo:service> 或 <dubbo:reference> 的 registry 屬性指定使用的註冊中心。
registry:
address: nacos://你的nacos服務器ip:8848
check: false
# 服務提供者協議配置。對應的配置類: org.apache.dubbo.config.ProtocolConfig。
# 同時,如果需要支持多協議,可以聲明多個 <dubbo:protocol> 標籤,並在 <dubbo:service> 中通過 protocol 屬性指定使用的協議。
protocol:
name: dubbo
port: 20880
# 服務消費者缺省值配置。配置類: org.apache.dubbo.config.ConsumerConfig 。
# 同時該標籤爲 <dubbo:reference> 標籤的缺省值設置
consumer:
timeout: 30000
retries: 0
check: false
主要注意上面的 retries check 參數,
check爲false 表示 springboot 啓動暫不查驗服務是否存在或服務是否可註冊成功,
如果爲 true 很可能導致啓動失敗,例如註冊中心不存在,服務提供者不存在時。
check 默認爲true
具體的其他參數查看 官方配置文檔
http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-service.html
接口 api
很簡單就兩測試方法
package com.middol.dubbo.api;
/**
* @author admin
*/
public interface HelloService {
/**
* sayHello
*
* @param name ignore
* @return String
*/
String sayHello(String name);
/**
* sayGoodbye
*
* @param name aa
* @return string
*/
default String sayGoodbye(String name) {
return "Goodbye, " + name;
}
}
服務提供者
package com.middol.dubbo.impl.provider;
import com.middol.dubbo.api.HelloService;
import org.apache.dubbo.config.annotation.DubboService;
/**
* 服務提供者
*
* @author admin
*/
@DubboService(version = "1.0")
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
System.out.println("provider received invoke of sayHello: " + name);
sleepWhile();
return "Annotation, hello " + name;
}
@Override
public String sayGoodbye(String name) {
System.out.println("provider received invoke of sayGoodbye: " + name);
sleepWhile();
return "Goodbye, " + name;
}
private void sleepWhile() {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
這裏注意 如果version 賦值了,請在消費端配置時保持version一致哦
服務消費者
實際情況是, 服務提供者將 api接口 打成jar包,放入maven私服中,服務消費端下載該jar包,本次就省略了該步驟, 消費者服務者在一個工程裏面。
package com.middol.dubbo.consumer;
import com.middol.dubbo.api.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
/**
* 服務訂閱者
*
* @author admin
*/
@Component
public class TestConsumerService {
@DubboReference(version = "1.0",consumer = "hello測試")
private HelloService helloService;
/**
* sayHello
*
* @param name ignore
* @return String
*/
public String sayHello(String name) {
return helloService.sayHello(name);
}
/**
* sayGoodbye
*
* @param name aa
* @return string
*/
public String sayGoodbye(String name) {
return helloService.sayGoodbye(name);
}
}
主要是 註解 @DubboReference(version = “1.0”,consumer = “hello測試”) 表示引入服務提供者的api接口
dubbo掃描註解配置
package com.middol.dubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 系統入口
*
* @author <a href="mailto:[email protected]">guzhongtao</a>
*/
@SpringBootApplication
@EnableDubbo(scanBasePackages = {"com.middol.dubbo.impl.provider", "com.middol.dubbo.consumer"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
上面的 @EnableDubbo 就是配置 消費者的包路徑 提供者的包路徑
訪問測試
先啓動 nacos ,然後springboot, 不出意外會拋出異常
APPLICATION FAILED TO START
Description:
The bean 'dubboBootstrapApplicationListener' could not be registered. A bean with that name has already been defined and overriding is disabled.
Action:
這裏查看 github dubbo Issues 6231.
可明白原因, 當然臨時解決可以在application.yml裏面加入以下信息:
spring:
main:
allow-bean-definition-overriding: true
寫個controller 類測試一下吧
package com.middol.dubbo.controller;
import com.middol.dubbo.consumer.TestConsumerService;
import com.middol.dubbo.impl.provider.HelloServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* controller 測試入口
*
* @author admin
*/
@RestController
@RequestMapping("/")
public class TestController {
@Resource
private HelloServiceImpl annotationHelloService;
@Resource
private TestConsumerService testConsumerService;
@GetMapping("localtest")
public String localtest() {
annotationHelloService.sayHello("我是本地方法 sayHello");
annotationHelloService.sayGoodbye("我是本地方法 sayGoodbye");
return "localtest ok";
}
@GetMapping("dubbotest")
public String dubbotest() {
testConsumerService.sayHello("我是dubbo方法 sayHello");
testConsumerService.sayGoodbye("我是dubbo方法 sayGoodbye");
return "dubbotest ok";
}
}
理論上可以調用成功,本人測試通過, 查看nacos 管理平臺可發現服務註冊情況。
如果需要測試代碼工程 點擊訪問 github 點擊這裏.