springboot+dubbo+nacos整合
1、建立maven專案
dubbo-nacos-demo
2、父工程引入依賴
<?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-nacos-demo</artifactId>
<groupId>com.zmq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.zmq</groupId>
<artifactId>dubbo-provide</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- 排除自帶的logback依賴 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!--<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- Dubbo Registry Nacos -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>apache.snapshots.https</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、建立子工程dubbo-provide
引入依賴
<?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-nacos-demo</artifactId>
<groupId>com.zmq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.zmq</groupId>
<artifactId>dubbo-provide</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- 排除自帶的logback依賴 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!--<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- Dubbo Registry Nacos -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>apache.snapshots.https</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
新建配置檔案application.yml
nacos:
config:
server-addr: localhost:8848
spring:
application:
name: dubbo-provide
dubbo:
application:
name: ${spring.application.name}
registry:
address: nacos://${nacos.config.server-addr}
scan:
base-packages: com.zmq.service
protocol:
name: dubbo
port: 20881
建立啟動類
package com.zmq;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboProvideApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProvideApplication.class);
}
}
建立service介面
package com.zmq.service;
public interface ProvideService {
String sayHello(String word);
}
建立service介面實現類,注意這裡的service註解是dubbo的註解
package com.zmq.service.impl;
import com.zmq.service.ProvideService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class ProvideImpl implements ProvideService {
public String sayHello(String word) {
return word;
}
}
3、建立dubbo-consumer模組
引入依賴
<?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-nacos-demo</artifactId>
<groupId>com.zmq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.zmq</groupId>
<artifactId>dubbo-consumer</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除自帶的logback依賴 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- Dubbo Registry Nacos -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.nacos/nacos-spring-context -->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>0.3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>com.zmq</groupId>
<artifactId>dubbo-provide</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>apache.snapshots.https</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
建立application.yml配置檔案
server:
address:
port: 8081
spring:
application:
name: dubbo-consumer
nacos:
config:
server-addr: localhost:8848
dubbo:
registry:
address: nacos://${nacos.config.server-addr}
application:
name: ${spring.application.name}
建立啟動類
package com.zmq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class dubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(dubboConsumerApplication.class);
}
}
建立controller呼叫服務
package com.zmq.controller;
import com.zmq.service.ProvideService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Reference
ProvideService provideService;
@RequestMapping("sayHello")
public String sayHello(){
return provideService.sayHello("我愛你");
}
}
4、瀏覽器檢視結果
在瀏覽器輸入localhost:8081/sayHello,即可看到輸出的話
5、啟動nacos後可在服務列表看到註冊的服務
專案原始碼:https://github.com/xiaohuoban00/springboot-dubbo-nacos
相關文章
- springboot+dubbo+nacos入門專案Spring Boot
- 基於Springboot+Dubbo+Nacos 註解方式實現微服務呼叫Spring Boot微服務
- 【springboot】學習4:整合JDBC、整合druid、整合mybatis、整合 SpringSecuritySpring BootJDBCUIMyBatisGse
- SpringBoot整合系列-整合JPASpring Boot
- MyBatis(九) 整合Spring、整合SpringMVCMyBatisSpringMVC
- SpringBoot整合系列–整合MyBatis-plusSpring BootMyBatis
- SpringBoot整合系列-整合H2Spring Boot
- [Redis 客戶端整合] SpringBoot 整合 LettuceRedis客戶端Spring Boot
- [Redis 客戶端整合] SpringBoot 整合 JedisRedis客戶端Spring Boot
- 自動化整合:Pipeline整合Docker容器Docker
- SSM整合之CRUD環境搭建整合SSM
- CodePush整合
- SAP整合
- 整合SwiftLintSwift
- SpringBootAdmin整合Spring Boot
- ssm整合SSM
- Spring AI與大模型Ollama如何整合整合?SpringAI大模型
- RestCloud iPaaS混合整合平臺,資料整合RESTCloud
- 整合學習(一):簡述整合學習
- SSM學習筆記3——整合 SpringMVC、整合SSMSSM筆記SpringMVC
- (九) SpringBoot起飛之路-整合/整合Swagger 2 And 3Spring BootSwagger
- Mybatis整合Spring(ssm整合待續)-day04MyBatisSpringSSM
- ETL資料整合,RestCloud資料整合平臺RESTCloud
- SpringBoot 整合 rabbitmqSpring BootMQ
- springcloud整合sleuthSpringGCCloud
- Vue整合UeditorVue
- springmvc整合elasticsearchSpringMVCElasticsearch
- SpringBoot 整合 apolloSpring Boot
- mybatis整合springMyBatisSpring
- SpringBoot 整合 elkSpring Boot
- SpringBoot 整合 elasticsearchSpring BootElasticsearch
- Spring 整合 HibernateSpring
- JavaScript整合SentryJavaScript
- android ndk整合Android
- SpringBoot整合elasticsearchSpring BootElasticsearch
- ObjectBox 整合指南Object
- ActiveMq整合SpringMQSpring
- Vuex之整合Vue