nacos 2.2.0支持postgresql
请查看该文章:Nacos 2.2.0支持postgresql数据库
具体版本说明
博主使用的版本为:
dubbo.version 3.1.7
spring-boot.version 2.3.12.RELEASE
spring-cloud.version Hoxton.SR12
spring-cloud-alibaba.version 2.2.10-RC1
nacos.version 2.2.0
一、代码示例
1、父工程版本依赖信息
<properties>
<java.version>1.8</java.version>
<dubbo.version>3.1.7</dubbo.version>
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.10-RC1</spring-cloud-alibaba.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencyManagement>
<dependencies>
<!-- SpringCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringCloud Alibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、子工程 提供者
2.1、版本依赖
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.xxx.cloud</groupId>
<artifactId>service</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
2.2、application.yml配置
spring:
application:
name: rpc
dubbo:
application:
name: rpc
registry:
address: nacos://localhost:8848?username=nacos&password=nacos
use-as-config-center: false
use-as-metadata-center: false
protocol:
name: dubbo
port: -1
为什么要把下面这两个配置设置为false,若不设置则启动报错,具体原因博主不清楚
use-as-config-center: false
use-as-metadata-center: false
dubbo github issues地址:java.lang.RuntimeException: publish nacos metadata failed
nacos github issues 地址: java.lang.RuntimeException: publish nacos metadata failed
备注:问题已解决
his_config_info中gmt_create为非空字段,在mysql中默认值是当前时间戳CURRENT_TIMESTAMP
postgresql-----his_config_info 字段 gmt_create 也要设置为默认值当前时间戳CURRENT_TIMESTAMP
2.3、服务引用
import com.xxx.cloud.modules.service.DemoService;
import org.apache.dubbo.config.annotation.DubboService;
/**
* @Date: 2023/03/05
*/
@DubboService
public class DemoServiceImpl implements DemoService {
public String abc() {
return "abc";
}
}
2.4、启动类
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class BootRpcApplication {
public static void main(String[] args) {
SpringApplication.run(BootRpcApplication.class, args);
}
}
3、子工程 消费者
3.1、版本依赖
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.xxx.cloud</groupId>
<artifactId>service</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
3.2、application.yml配置
spring:
application:
name: consumer
server:
port: 9000
dubbo:
application:
name: consumer
registry:
address: nacos://localhost:8848?username=nacos&password=nacos
protocol:
name: dubbo
port: -1
scan:
base-packages: com.xxx.cloud
3.3、服务引用
import com.xxx.cloud.modules.service.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Date: 2023/03/05
*/
@RestController
public class TestConsumer {
@DubboReference
private DemoService demoService;
@GetMapping("abc")
public String abc() {
return demoService.abc();
}
}
3.4、启动类
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class BootConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootConsumerApplication.class, args);
}
}
4、服务实现类
/**
* @Date: 2023/03/05
*/
public interface DemoService {
String abc();
}