In this chapter we integrate Dubbo. Then why dubbo when you have Fegin? This is the core of this chapter. This chapter will talk not only about how to integrate dubbo, but also about the difference between Dubbo and Fegin

What about Fegin or Dubbo

First of all, the biggest difference between Fegin and Dubbo is that Dubbo is Alibaba’s son

Why integrate Dubbo

Dubbo is a good microservices governance framework on its own. Since SpringCloudAlibaba is also a microservices governance solution, is there a lot of repetition in using Dubbo at this time? It’s not. Under the integration of Spring Cloud Alibaba, Dubbo users can not only enjoy the performance advantages brought by RPC, but also better enjoy various benefits of Spring Cloud. For Spring Cloud users, it is also a good option in terms of service governance. It can be said that the integration of this piece really makes these two large user groups get a good integration and play a role of mutual achievement.

Integrating Dubbo, introductory case

Using Dubbo to realize service provider and service consumer, we need to create two corresponding projects, one service provider and one service consumer. Let’s start by creating a service provider.

  • Create a simple SpringBoot service with the version I selected: 2.2.11.release

Introduce the dependencies needed to integrate Dubbo

  • 1, spring – cloud – starter – dubbo
  • 2, spring – the cloud – starter – alibaba – nacos – discovery

My server project POM file is as follows

<? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.11. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.echo</groupId> <artifactId>dubbo-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name> Dubbo </name> <description>Demo project for Spring Boot</description> < properties > < Java version > 1.8 < / Java version > < spring - cloud - alibaba. Version > 2.2.1. RELEASE < / spring - cloud - alibaba. Version >  </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>2.2.1.RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <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> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Write the corresponding configuration

  • Nacos configuration
  • Dubbo’s own configuration
Server: port: 10083 Spring: Cloud: nacos: server-addr: 192.168.22.71:8848 spring-cloud-dubbo-server dubbo: scan: base-packages: com.echo.dubbo.server protocol: name: dubbo port: -1 registry: address: spring-cloud://localhostCopy the code
  • Dubo.scans. base-packages: specifies interfaces that provide services
  • Dubbo. Protocol. Name: dubbo indicates the protocol name
  • Dubo.protocol. port: -1 Indicates the self-added port, starting from 20880
  • Dubo.registry. Address: indicates that it is mounted to the Spring Cloud registry (our NACOS, which can be used directly to replace localhost with nacOS address).

Add service registration and discovery annotations for NACOS

// To add an annotation to the startup class, both the provider and the consumer need @enableDiscoveryClientCopy the code

Write a Dubbo interface and implement

public interface TestService { String test(String name); } / / pay attention to the annotation the serveice to dubbo, not framework, introduced to import org. Apache. Dubbo. Config. The annotation. The Service; @Service public class TestServiceImpl implements TestService { @Override public String test(String name) { return "hello  " + name; }}Copy the code

At this point our service provider is done, then create a service consumer to invoke

Create a consumer

Consumers are created the same way service providers are created. The dependency is just to add the service provider project, everything else is the same. The key is the configuration file

  • The application. Yml configuration file contains dubo.scan. Base-packages, which are different from service providers. Subscribed -services: spring-cloud-dubo-server (this value is the name of our service provider’s project registered with NACOS)

Write a test class

import org.apache.dubbo.config.annotation.Reference; @RestController public class TestController { @Reference private TestService testService; @GetMapping("/test") public String test() { return testService.test("echo"); }}Copy the code

At this point, most of our work is done, and we can start nacOS to verify that services are registered directly.

After success we can call our test interface:

At this point we are done integrating Dubbo

Source: www.tuicool.com/articles/Bj…