Nacos+Spring Boot+Dubbo native integration example

Actually I'm real 2022-09-23 10:19:11 阅读数:682

nacos+springnacosspringboot+dubboboot

Nacos+Spring Boot+Dubbo原生整合 例子

前言

我一共搭建了3个环境.
整体入口:
https://blog.csdn.net/lwb314/article/details/108222433

正文

This method is basically usednacos官网+dubbo官网的例子,A tough one,特别别扭.
Mainly look at the bottom2页
Nacos官网的nacos-dubbo
https://nacos.io/zh-cn/docs/use-nacos-with-dubbo.html
dubboThe annotation configuration of the official website
http://dubbo.apache.org/zh-cn/docs/user/configuration/annotation.html
依赖如下:
我是用的spring boot作为了spring Container and service listening,像nacos官网,then write,但是我觉得麻烦,所以用了Spring Boot,Also for integration,Because most of my programs are based on Spring Boot写的

<!--spring boot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--Below is the dependency,用的是dubboNative latest version-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.8</version>
</dependency>

服务提供者代码-provider

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;
@PropertySource(value = "classpath:/provider-config.properties")
@EnableDubbo(scanBasePackages = "com.lwb.providerdubbodemo.dubbo")
@SpringBootApplication
public class ProviderDubboDemoApplication {

public static void main(String[] args) {

ConfigurableApplicationContext app = SpringApplication.run(ProviderDubboDemoApplication.class, args);
}
}

Service class code

import com.lwb.DubboDemoService;
import org.apache.dubbo.config.annotation.Service;
@Service(version = "${demo.service.version}")
public class DubboDemoServiceImpl implements DubboDemoService {

@Override
public String helloDubbo() {

return "helloDubbo";
}
}

服务接口

public interface DubboDemoService {

public String helloDubbo();
}

application.properties
A startup port is set,没了

server.port=8001

provider-config.properties配置

dubbo.application.name=microservice
dubbo.registry.address=nacos://10.10.171.249:8848
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# Provider @Service version
demo.service.version=1.0.0
demo.service.name = demoService

Here's what I think is a pit:

  1. dubbo.application.nameThis annotation cannot change the service name,在nacosThe service name in is providers:com.lwb.DubboDemoService:1.0.0:,It can only be seen in the detailsapplication=microservice,跟spring.application.nameThis customizable service name is completely different,可气的是spring.application.nameThis property cannot be written yet,写了报错.
    Below is a screenshot of the finished service,Someone knows how to change it in this way,Leave me a message,谢谢.
    在这里插入图片描述

  2. 必须要有一个provider-config.properties,I experimenteddubbo的配置写在application.properties里会报错,Of course, I didn't dare to experiment and change this annotation directly toapplication.properties像这样,没试,I feel that the way is too wild to be successful.
    @PropertySource(value = “classpath:/application.properties”)

  3. 最后一个是version,If you don't write it, you will get an [email protected]里那个,But I don't see a lot of examples,But I got an error if I didn't write this example,不知道为什么,不知道是不是我用的dubbo版本太新了,这个也比较好理解,It is a situation where an interface may have multiple versions in parallel,At this time, the version number must be available.

Service caller code-consumer

import com.lwb.DubboDemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import javax.annotation.PostConstruct;
@EnableDubbo
@PropertySource(value = "classpath:/consumer-config.properties")
@SpringBootApplication
public class ConsumerDubboDemoApplication {

@Reference(version = "${demo.service.version}")
private DubboDemoService demoService;
@PostConstruct
public void init() {

String a= demoService.helloDubbo();
System.out.println(a);
}
public static void main(String[] args) {

SpringApplication.run(ConsumerDubboDemoApplication.class, args);
}
}

Start directly to print the results

application.properties,Change the service startup port

server.port=8002

consumer-config.properties,Look carefully at the notes inside

## Dubbo Application info
dubbo.application.name = dubbo-consumer-demo
## Nacos registry address
dubbo.registry.address = nacos://10.10.171.249:8848
##Mainly below3个属性,因为最新的dubboComes with a monitoring process default port22222
##You are either offqos服务,Either change the port
##Or just nowdubbo-providerThe program takes your native22222端口占用了
##consumer就启动不了了
dubbo.application.qosEnable=true
dubbo.application.qosPort=33333
dubbo.application.qosAcceptForeignIp=false
# @Reference version
demo.service.version= 1.0.0

这里说一下坑:

  1. @Reference的version属性 必须写,不写报错.
  2. 因为使用原生dubbo的关系,.propertiesThere is no way for the file to indicate the relevant properties,Just paste it from the official website,然后改一改.
copyright:author[Actually I'm real],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/266/202209231010447007.html