After a long day, we’re finally even.

In this article, we implemented a demo of service provision and invocation based on Dubbo, with access to the Nacos registry.

Dubbo Spring Cloud is the core component of Spring Cloud Alibaba, which is built on the native Spring Cloud standard. It not only covers the native features of Spring Cloud, but also provides a more stable and mature implementation. Next, let’s take a step at its mystery.

Without further ado, get right to the code.

Version selection

Spring Cloud Version : Hoxton.SR9

Spring Cloud Alibaba Version: 2.2.6.RELEASE

Spring Boot Version: 2.3.2.release

The (important) version must match, otherwise a lot of errors are annoying

Implement Dubbo service provider

Create a parent project dubo-demo

Add POM to pom.xml

Delete SRC folder

2. Create the provider-API module

This is a normal Maven project where interfaces are created and MVN Install is executed to install jar packages into the local repository

/** * public interface HelloService {/** * Hello * @param name * @return */ String Hello (String name); }Copy the code

3. Create the Provider module

The first step, which is a Spring Boot project, is to add dependencies

<dependencies> <dependency> <groupId>cn.guoxiaolong</groupId> <artifactId>provider-api</artifactId> 1.0 the SNAPSHOT < version > < / version > < / dependency > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter</artifactId> <version>${spring.cloud.alibaba.version}</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>${spring.cloud.alibaba.version}</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>${spring.cloud.alibaba.version}</version>  </dependency> </dependencies>Copy the code

The dependency description is as follows:

Provider-api: specifies the API interface

Spring-cloud-starter: The Spring Cloud core package

Spring-cloud-starter-dubbo: Introduce Spring Cloud Alibaba Dubbo

Spring-cloud-starter-alibaba-nacos-discovery: artifactId for service registration discovery based on NACOS

Second, create an implementation class for the interface, HelloServiceImpl, where @DubboService is an annotation to the Dubbo service, indicating that the current service will be published as a remote service

@DubboServicepublic class HelloServiceImpl implements HelloService { private static final Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class); @Override public String hello(String name) { logger.info("service impl param:{}",name); return "Hello," + name + " welcome to AlwaysBeCoding!" ; }}Copy the code

Third, configure dubbo information in application.properties

spring.application.name=providerdubbo.scan.base-packages=cn.guoxiaolong.dubbodubbo.protocol.name=dubbodubbo.protocol.por T = 20882 dubbo. Protocol. The host = 100.149.40.112 dubbo, registry. Address = spring - cloud: / / localhostspring cloud. Nacos. Discovery. Ser Ver - addr = 100.149.40.112:8848Copy the code

One spring. The cloud. Nacos. Discovery. Server – for registry address addr, here I am single start registry (reference: nacos quick start installation), also can use a highly available deployment (based on nacos to achieve high availability service registry deployment)

Step 4, create the startup class and start the service

@SpringBootApplicationpublic class ProviderApplication {    public static void main(String[] args) {        SpringApplication.run(ProviderApplication.class,args);    }}
Copy the code

Check the registry. The service has been registered

Implement the Dubbo service caller

Create a Spring Boot project for consumer

The first step is to add web dependencies, otherwise aligned with the service provider

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId>    <version>${spring.boot.version}</version></dependency>
Copy the code

The second step is to create the HelloController class and expose a Hello service to consume the HelloService service of the Dubbo service provider.

@RestControllerpublic class HelloController {
    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
    @DubboReference    private HelloService helloService;
    @GetMapping("/hello")    public String hello(String name){        logger.info("controller param:{}"+name);        String result = helloService.hello(name);        logger.info("controller result:{}",result);        return result;    }}
Copy the code

Third, create the startup class

@SpringBootApplication

public class ConsumerApplication {
    public static void main(String[] args) {        SpringApplication.run(ConsumerApplication.class,args);    }}
Copy the code

Fourth, configure dubbo information in application.properties

spring.application.name=consumerdubbo.scan.base-packages=cn.guoxiaolong.dubbodubbo.protocol.name=dubbodubbo.protocol.por T = 20883 dubbo. Protocol. The host = 100.149.40.112 dubbo, registry. Address = spring - cloud: / / localhostspring cloud. Nacos. Discovery. Ser Ver - addr = 100.149.40.112:8848Copy the code

Fifth, start the service and test the invocation