SpringBoot整合Dubbo,註冊中心nacos
目錄
3. 建立Dubbo服務提供者工程dubbo-provider
7. dubbo-provider引入dubbo-client
8. 建立DubboDemo的實現類DubboDemoImpl
10. 在resources中建立配置檔案application.yml
14. 在resources下建立application.yml
17. 啟動dubbo-provider和dubbo-consumer,檢視nacos
開發環境
- 工具:IDEA
- java環境:JDK1.8
開始搭建Demo
1. 建立父工程
2. 刪除無用src目錄
3. 建立Dubbo服務提供者工程dubbo-provider
4. 建立Dubbo介面工程dubbo-client
5. 在父工程引入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.sunrd</groupId>
<artifactId>springboot-dubbo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-provider</module>
<module>dubbo-client</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-registry-nacos -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.esotericsoftware/kryo -->
<!--序列化-->
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>4.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/de.javakaffee/kryo-serializers -->
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
<version>0.45</version>
</dependency>
</dependencies>
</project>
6. 建立介面DubboDemo
package com.sunrd.service;
public interface DubboDemo {
/**
* 測試介面
* @return
*/
String sayHello();
}
7. dubbo-provider引入dubbo-client
<dependencies>
<dependency>
<groupId>com.sunrd</groupId>
<artifactId>dubbo-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
8. 建立DubboDemo的實現類DubboDemoImpl
package com.sunrd.service.impl;
import com.sunrd.service.DubboDemo;
/**
* @author sunrd
* @description
* @date 2020/7/28
*/
public class DubboDemoImpl implements DubboDemo {
@Override
public String sayHello() {
return "hello, dubbo";
}
}
9. 在實現類上新增註解@DubboService
@DubboService(interfaceClass = DubboDemo.class)
public class DubboDemoImpl implements DubboDemo {
@Override
public String sayHello() {
return "hello, dubbo";
}
}
10. 在resources中建立配置檔案application.yml
spring:
application:
name: DUBBO-PROVIDER
dubbo:
protocol:
name: dubbo
serialization: kryo
port: 20980
registry:
address: nacos://192.168.5.3:8848 # 自己的nacos地址
check: false
group: noncore
version: 1.0
protocol: nacos
parameters:
namespace: dev
provider:
version: 1.0
group: noncore
11. 建立啟動類
package com.sunrd.service;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author sunrd
* @description
* @date 2020/7/28
*/
@SpringBootApplication
// dubbo服務掃描的包
@DubboComponentScan(basePackages = {
"com.sunrd.service.impl"
})
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
12. 建立消費者工程dubbo-consumer
13. 引入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">
<parent>
<artifactId>springboot-dubbo</artifactId>
<groupId>com.sunrd</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.sunrd</groupId>
<artifactId>dubbo-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
14. 在resources下建立application.yml
spring:
application:
name: DUBBO-CONCUMER
dubbo:
protocol:
name: dubbo
serialization: kryo
port: 20980
registry:
address: nacos://42.194.181.247:8848 # 自己的nacos地址
check: false
group: noncore
version: 1.0
protocol: nacos
parameters:
namespace: dev
consumer:
group: noncore
check: false
version: 1.0
15. 建立controller進行消費
package com.sunrd.controller;
import com.sunrd.service.DubboDemo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author sunrd
* @description
* @date 2020/7/28
*/
@RestController
public class DubboTestController {
@DubboReference
private DubboDemo dubboDemo;
@RequestMapping("/hello")
public String sayHello(){
return dubboDemo.sayHello();
}
}
16. 建立啟動類ConsumerApplication
package com.sunrd.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author sunrd
* @description
* @date 2020/7/28
*/
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
17. 啟動dubbo-provider和dubbo-consumer,檢視nacos
18. 測試訪問:localhost:8080/hello
相關文章
- SpringBoot與Dubbo整合報錯排查(Nacos作為註冊中心)Spring Boot
- springboot整合nacos註冊中心和配置中心Spring Boot
- Dubbo使用nacos作為註冊中心原理剖析
- springcloud/springboot整合NACOS 做註冊和配置中心以及nacos原始碼分析GCCloudSpring Boot原始碼
- springboot+dubbo+nacos整合Spring Boot
- nacos-dubbo-springboot整合Spring Boot
- Dubbo與SpringCloud Alibaba使用Nacos作為配置中心和註冊中心SpringGCCloud
- 初探Nacos(三)-- SpringBoot下整合Dubbo和NacosSpring Boot
- Nacos註冊中心+配置管理
- 使用nacos做dubbo的註冊中心服務列表沒有資料
- 為什麼阿里的dubbo註冊中心要放棄zookeeper, 而用Nacos?阿里
- SpringBoot使用Nacos作為配置中心服務和服務註冊中心Spring Boot
- Springboot + Dubbo + Nacos微服務框架整合Spring Boot微服務框架
- nacos註冊中心原始碼流程分析原始碼
- springboot工程dubbo使用nacos作為配置中心Spring Boot
- Dubbo 中 Zookeeper 註冊中心原理分析
- 圖解Nacos,註冊中心演變 + Nacos核心功能圖解
- Nacos註冊與配置中心:使用詳講
- Spring Cloud Alibaba(4)---Nacos(註冊中心)SpringCloud
- 阿里 Nacos 註冊中心 配置啟動說明阿里
- 保姆教程系列二、Nacos實現註冊中心
- SpringCloud入門(五)Nacos註冊中心(上)SpringGCCloud
- SpringCloud入門(六)Nacos註冊中心(下)SpringGCCloud
- Dubbo+Nacos實現服務註冊和發現
- 微服務架構 | 3.2 Alibaba Nacos 註冊中心微服務架構
- 微服務註冊中心 Nacos 比 Eureka的優勢微服務
- SpringCloud整合Consul註冊中心SpringGCCloud
- 【SpringBoot】服務對註冊中心的註冊時機Spring Boot
- SpringCloud-Alibaba之Nacos服務註冊中心SpringGCCloud
- 手把手教你將Eureka升級Nacos註冊中心
- SpringCloud Alibaba Nacos註冊中心原始碼淺析SpringGCCloud原始碼
- nacos原理三-註冊中心原理&原始碼啟動.md原始碼
- Spring Cloud Alibaba:Nacos 作為註冊中心和配置中心使用SpringCloud
- Apache Dubbo 原始碼搭建與解讀(八)—— Dubbo 註冊中心之ZooKeeperApache原始碼
- apisix~整合服務發現註冊中心API
- 微服務框架-dubbo整合nacos框架微服務框架
- SpringBoot整合NacosSpring Boot
- 這個Dubbo註冊中心擴充套件,有點意思!套件