Part1文章首推
- 支付宝接口对接
- 高德地图调用
- 验证码登录
- QQ邮箱登录
Part2今日主题:springboot dubbo nacos非常详细的整合过程
1简介
nacos是阿里巴巴的一个服务注册中心,相比于zookeeper来说,他的优势非常大,nacos几乎可以成为所有的微服务注册中心,今天来讲一下如何搭建一个demo的,我这个是基于dubbo搭建的。
2环境
- springboot
3实现过程
1.创建一个一个maven项目
2.删掉这个maven工程的src目录,在这个工程的pom中加入:
代码语言:javascript复制 <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<netty.version>4.1.56.Final</netty.version>
<spring-boot.version>2.3.5.RELEASE</spring-boot.version>
<dubbo.version>2.7.8</dubbo.version>
<nacos-client.version>1.4.0</nacos-client.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos-client.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.目录结构
4.创建好对应的模块
common-api:
pom.xml:
代码语言:javascript复制<parent>
<artifactId>springboot_dubbo_nacos_demo</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
创建SayService:
代码语言:javascript复制public interface SayService {
/**
* 根据名字say hello
* @param name 名字
* @return name hello
*/
String sayHelloByName(String name);
}
dubbo-provider:
pom.xml:
代码语言:javascript复制<parent>
<groupId>com.example</groupId>
<artifactId>springboot_dubbo_nacos_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>dubbo-provide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provide</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>common-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
SayServiceImpl.java:
代码语言:javascript复制/**
* @DubboService是新版Dubbo的注解,如果是旧版比如2.7.3,使用的是org.apache.dubbo.config.annotation.Service这个注解
*/
@DubboService
public class SayServiceImpl implements SayService {
@Override
public String sayHelloByName(String name) {
return name ",hello!";
}
}
启动类上加上@EnableDubbo
注解
application.yml:
代码语言:javascript复制server:
port: 14511
spring:
application:
name: provider-service
main:
allow-bean-definition-overriding: true
dubbo:
application:
name: provider-service
registry:
address: nacos://127.0.0.1:8848
username: nacos
password: nacos
scan:
base-packages: com.example.service.impl
protocol:
name: dubbo
port: 15511
dubbo-consumer
pom.xml:
代码语言:javascript复制 <parent>
<groupId>com.example</groupId>
<artifactId>springboot_dubbo_nacos_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>common-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
application.yml:
代码语言:javascript复制server:
port: 14512
spring:
application:
name: consumer-service
main:
allow-bean-definition-overriding: true
dubbo:
application:
name: consumer-service
registry:
address: nacos://127.0.0.1:8848
username: nacos
password: nacos
protocol:
name: dubbo
port: 15511
SayController.java:
代码语言:javascript复制@RestController
@RequestMapping("/demo/say")
public class SayController {
@DubboReference
private SayService sayService;
@GetMapping("/sayHello")
public ResponseEntity<String> sayHello(@RequestParam("name") String name) {
return ResponseEntity.ok(sayService.sayHelloByName(name));
}
}
启动类上也要加上@EnableDubbo
5.记得把nacos打开,然后依次启动provider,consumer模块,访问http://localhost:8848/
,账号密码都是nacos
服务列表可以看到两个服务
6.测试接口:
访问成功
参考:https://blog.neoniou.com/posts/spring-boot-nacos-dubbo/#service-consumer