Not only does it simplify the way Dubbo is configured based on XML, but it also improves daily development efficiency and even happiness at work.

To save your time, please follow these 2 tips to improve your reading efficiency.

  • If you have a simple Java foundation and Maven experience but are not familiar with Dubbo, this document will help you develop Dubbo services from scratch using Spring Boot and implement service registration and discovery using the EDAS service registry.

  • If you are familiar with Dubbo, read the chapters selectively.

Why use Spring Boot to develop Dubbo applications

Spring Boot uses some minimal configuration, can quickly build a Spring-based application, improve the daily development efficiency. Therefore, if you use Spring Boot to develop Dubbo based applications, it simplifies the Bubbo xmL-based configuration approach, improves your daily development efficiency and improves your happiness at work.

Why use the EDAS Service Registry

EDAS service registry implements SPI standard registry extensions provided by Dubbo, fully supporting Dubbo service registration, routing rules, configuration rules functions.

The EDAS service registry can completely replace ZooKeeper and Redis as your Dubbo service registry. At the same time, compared with ZooKeeper and Redis, it has the following advantages:

  • EDAS service registry is a shared component, saving the machine cost of operation and maintenance, deployment of ZooKeeper and other components.
  • The EDAS service registry has added authentication and encryption functions in the communication process to harden the security of your service registration link.
  • The EDAS Service Registry is closely integrated with other EDAS components to provide you with a complete set of microservices solutions.

Local development

The preparatory work

  • Download, start, and configure the lightweight configuration center.

To facilitate local development, EDAS provides a lightweight configuration center that contains the basic functionality of the EDAS service registry. Applications developed based on the lightweight configuration center can be deployed to EDAS in the cloud without any code or configuration changes.

For details about how to download, start, and configure lightweight Configuration center, see Configuring lightweight Configuration Center. The latest version is recommended.

  • Download Maven and set environment variables (locally installed ones can be skipped).

Creating a service provider

  1. Create a Spring Boot project and name it spring-boot-Dubbo-provider.

Here we take Spring Boot 2.0.6.RELEASE as an example and add the following content to the pom. XML file.

```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - dependencies < / artifactId > < version > 2.0.6. RELEASE < / version > < type > pom < type > <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> <dependency> < the groupId > com. Alibaba. The boot < / groupId > < artifactId > dubbo - spring - the boot - starter < / artifactId > < version > 0.2.0 < / version > </dependency> <dependency> <groupId>com.alibaba.edas</groupId> <artifactId>edas-dubbo-extension</artifactId> < version > 1.0.0 - the SNAPSHOT < / version > < / dependency > < / dependencies > ` ` `Copy the code

If you need to select the version that uses Spring Boot 1.x, use Spring Boot 1.5.x. The corresponding com.alibaba. Boot: Dubo-spring-boot-starter is 0.1.0.

Note: The Spring Boot 1.x release lifecycle is coming to an end in August 2019. It is recommended that you develop your application with the new release.

2. Develop Dubbo service providers

2.1 Services in Dubbo are provided in the form of interfaces. So you need to develop an interface, like IHelloService here, that has several methods that can be called, like the SayHello method here.

``` package com.alibaba.edas.boot; public interface IHelloService { String sayHello(String str); } ` ` `Copy the code

2.2 On the service provider, it is necessary to implement all service interfaces exposed in the form of interfaces. For example, the class that implements the IHelloService interface is HelloServiceImpl.

```java package com.alibaba.edas.boot; import com.alibaba.dubbo.config.annotation.Service; @Service public class HelloServiceImpl implements IHelloService { public String sayHello(String name) { return "Hello, " + name + " (from Dubbo with Spring Boot)"; }} ` ` `Copy the code

Description: the Service annotations Dubbo provide an annotation of the class, class name called: com. Alibaba. Dubbo. Config. The annotation. Service.

2.3 Configuring the Dubbo Service In the application. The properties/application. Yaml configuration file add the following configuration:

```properties # Base packages to scan Dubbo Components (e.g @Service , @Reference) dubbo.scan.basePackages=com.alibaba.edas.boot dubbo.application.name=dubbo-provider-demo Dubbo. Registry. Address = edas: / / 127.0.0.1:8080 ` ` `Copy the code

Description:

  • There are no default values for the above three configurations, and specific configurations must be specified.
  • Dubbo. Scan. BasePackages value is developed code contains com. Alibaba. Dubbo. Config. The annotation. Service and Com. Alibaba. Dubbo. Config. The annotation. The Reference annotations in the bag. Multiple packages are separated by commas.
  • The value of dubbo.registry. Address must be prefixed with an EDas ://, followed by the IP address and port referring to the lightweight configuration center

3. Develop and start the Spring Boot entry class

```java package com.alibaba.edas.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DubboProvider { public static void main(String[] args) { SpringApplication.run(DubboProvider.class, args); }} ` ` `Copy the code

4. Log in to the Lightweight Configuration Center console http://127.0.0.1:8080, and click the service list in the navigation tree to view the provider list. Can see in the service provider already contains com. Alibaba. Edas. IHelloService, and can query the grouping and service provider of the service IP.

Creating service consumers

  1. Create a Spring Boot project named Spring-boot-dubo-Consumer.

Here we take Spring Boot 2.0.6.RELEASE as an example and add the following content to the pom. XML file.

```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - dependencies < / artifactId > < version > 2.0.6. RELEASE < / version > < type > pom < type > <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> <dependency> < the groupId > com. Alibaba. The boot < / groupId > < artifactId > dubbo - spring - the boot - starter < / artifactId > < version > 0.2.0 < / version > </dependency> <dependency> <groupId>com.alibaba.edas</groupId> <artifactId>edas-dubbo-extension</artifactId> < version > 1.0.0 - the SNAPSHOT < / version > < / dependency > < / dependencies > ` ` `Copy the code

If you need to select the version that uses Spring Boot 1.x, use Spring Boot 1.5.x. The corresponding com.alibaba. Boot: Dubo-spring-boot-starter is 0.1.0.

Note: The Spring Boot 1.x release lifecycle is coming to an end in August 2019. It is recommended that you develop your application with the new release.

2. Develop Dubbo customers

2.1 For service consumers, all service interfaces exposed in the form of interfaces should be introduced. For example, here is the IHelloService interface.

```java package com.alibaba.edas.boot; public interface IHelloService { String sayHello(String str); } ` ` `Copy the code

2.2 Dubbo Service Invocation. For example, if you need to call the remote Dubbo service in the Controller, the code developed is as follows:

```java package com.alibaba.edas.boot; import com.alibaba.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoConsumerController { @Reference private IHelloService demoService; @RequestMapping("/sayHello/{name}") public String sayHello(@PathVariable String name) { return demoService.sayHello(name); }} ` ` `Copy the code

Description: here’s the Reference annotation is com. Alibaba. Dubbo. Config. The annotation. The Reference.

2.3 Configuring the Dubbo Service In the application. The properties/application. Yaml configuration file add the following configuration:

` ` ` properties dubbo. Application. Name = dubbo - consumer - demo dubbo, registry. Address = edas: / / 127.0.0.1:8080 ` ` `Copy the code

Description:

  • There is no default value for the above two configurations. You must specify the configuration.
  • The value of dubbo.registry. Address must be prefixed with an EDas ://, followed by the IP address and port referring to the lightweight configuration center

3. Develop and start the Spring Boot entry class

```java package com.alibaba.edas.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DubboConsumer { public static void main(String[] args) { SpringApplication.run(DubboConsumer.class, args); }} ` ` `Copy the code
  1. In lightweight version configuration center console http://127.0.0.1:8080, click in the left navigation bar service list, again in service list page select the caller list, you can see contains com. Alibaba. Edas. IHelloService, You can also view the service group and caller IP address of the service.

results

  • Local result validation

curl http://localhost:17080/sayHello/EDAS

Hello, EDAS (from Dubbo with Spring Boot)

  • Verify the EDAS deployment results

curl http://localhost:8080/sayHello/EDAS

Hello, EDAS (from Dubbo with Spring Boot)