Write before: Before reading this article, have knowledge of dubbo, springboot
dubbo: http://dubbo.apache.org
dubbo github source address: https://github.com/apache/incubator-dubbo
dubbo Operations and Maintenance Project Source Address: https://github.com/apache/incubator-dubbo-ops
This project GitHub: https://github.com/Blankwhiter/dubbo-spring-boot-starter-test
Source corresponding version 1.0.1: https://github.com/Blankwhiter/dubbo-spring-boot-starter-test/archive/1.0.1.zip
The first step is to set up the zookeeper environment
In the centos window, execute the following command, pull the mirror, and start the zookeeper container
docker pull zookeeper docker run -d -v /home/docker/zookeeperhost/zookeeperDataDir:/data -v /home/docker/zookeeperhost/zookeeperDataLogDir:/datalog -e ZOO_MY_ID=1 -e ZOO_SERVERS='server.1=125.77.116.145:2888:3888' -p 2182:2181 -p 2888:2888 -p 3888:3888 --name zookeeper --privileged zookeeper
Note: 1.zookeeper default connection port is 2118, but this test case uses 2182 because it is occupied by other programs. 2. Readers create their own mapping directory, zookeeperDataDir | zookeeperDataLogDir
Step 2 springboot integration dubbo
1. Project catalogue organization
Explain:
- 1.api directory: store service interfaces invoked by consumers and providers
- 2.consumer directory: consumer directory calls interface implementation remotely provided by provider
- 3.provider directory: Provider directory provides consumer interface implementations
Readers create their own project directories (create empty projects, then create three new module s in empty projects)
Project Case Description: Business Assumption Scenario="Product Purchase Consumption Amount (consumer) returns the total amount of all consumption (service implementation needs to be invoked in provider project).
2. Coding
2.1 api Directory Interface Writing
2.1.1.Create CostService.java under the com.dubbo.api.service (reader's own creation, same below, package creation will not be covered in detail)
package com.dubbo.api.service; public interface CostService { /** * Cost increase interface * @param cost * @return */ Integer add(int cost); }
2.1.2.pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dubbo</groupId> <artifactId>api</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>api</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2 consumer directory web access, interface calls, and dubbo configuration writing
2.2.1. Introducing dubbo-spring-boot-starter and the above api modules
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dubbo</groupId> <artifactId>consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>consumer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.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> </dependency> <!--Introduce api Modular--> <dependency> <groupId>com.dubbo</groupId> <artifactId>api</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> <!--Introduce dubbo Environmental Science--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2.2. Create application.yml in the resources directory and write a dubbo configuration
dubbo: application: name: dubbo-consumer registry: address: 125.77.116.145:2182 # Readers please change to their own zookeeperip protocol: zookeeper check: false monitor: protocol: register consumer: check: false timeout: 3000 server: port: 8062
2.2.3. Open dubbo with the @EnableDubbo annotation
ConsumerApplication.java startup class
package com.dubbo.consumer; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
2.2.4. Write product service interface ProductService.java
package com.dubbo.consumer.service; public interface ProductService { /** * Get Total Consumption * @param a * @return */ Integer getCost(int a); }
2.2.5. Write an implementation of the product interface and call the remote service CostService. ProductServiceImpl.java
package com.dubbo.consumer.service.impl; import com.alibaba.dubbo.config.annotation.Reference; import com.dubbo.api.service.CostService; import com.dubbo.consumer.service.ProductService; import org.springframework.stereotype.Service; /** * Product service */ @Service public class ProductServiceImpl implements ProductService { /** * Use dubbo's comment com.alibaba.dubbo.config.annotation.Reference. Make a remote call to the service */ @Reference private CostService costService; @Override public Integer getCost(int a) { return costService.add(a); } }
2.2.6. Write access classes, ProductController.java
package com.dubbo.consumer.controller; import com.dubbo.consumer.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Product controller */ @RestController public class ProductController { @Autowired private ProductService productService; /** * Return total consumption after adding * @param a * @return */ @RequestMapping("/add") public String getCost(int a){ return "Total consumption of this product:"+productService.getCost(a); } }
2.3 provider directory api interface implementation and dubbo configuration
2.3.1. Introducing dubbo-spring-boot-starter and the above api modules
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dubbo</groupId> <artifactId>provider</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>provider</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.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> </dependency> <!--Introduce api--> <dependency> <groupId>com.dubbo</groupId> <artifactId>api</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> <!--Introduce dubbo Environmental Science--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.3.2. Create application.yml in the resources directory and write a dubbo configuration
dubbo: application: name: dubbo-provider registry: address: 125.77.116.145:2182 # Readers please change the zookeeper address themselves protocol: zookeeper check: false protocol: name: dubbo port: 30003 monitor: protocol: register consumer: check: false timeout: 3000 server: port: 8061
2.3.3. Open dubbo with the @EnableDubbo annotation
ConsumerApplication.java startup class
package com.dubbo.provider; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
2.3.3. Write CostServiceService to implement CostServiceImpl.java
package com.dubbo.provider.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.dubbo.api.service.CostService; /** * Expense Services */ @Service public class CostServiceImpl implements CostService { /** * The total cost before assuming this was 100 */ private final Integer totalCost = 1000; /** * Add the last stroke to the previous total * @param cost * @return */ @Override public Integer add(int cost) { return totalCost + cost; } }
Step 3 Testing dubbo remote service calls
After writing the second step code, start the consumer project and the provider project
Access in Browser http://localhost:8062/add?a=100
The call succeeds when the above result appears
Step 4 dubbo Management Platform
Old dubbo Operations & Maintenance Address: https://github.com/apache/incubator-dubbo-ops/tree/master
1. This article downloads the operation and maintenance project code on D:learnplaceincubator-dubbo-ops
2. Here you need to modify a configuration D:learnplaceincubator-dubbo-opsdubbo-adminsrcmain esourcesapplication.properties
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # server.port=8001 #Server port server.port spring.velocity.cache=false spring.velocity.charset=UTF-8 spring.velocity.layout-url=/templates/default.vm spring.messages.fallback-to-system-locale=false spring.messages.basename=i18n/message spring.root.password=root spring.guest.password=guest #Accessed password configuration spring.root. Password spring.guest.password #dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.registry.address=zookeeper://125.77.116.145:2182 #zookeeper address
3. In the D:learnplaceincubator-dubbo-opsdubbo-admin directory, enter the cmd window to execute
mvn claen package package package project,
4. Then enter D:learnplaceincubator-dubbo-opsdubbo-admin arget, enter cmd window to execute
Java-jar dubbo-admin-0.0.1-SNAPSHOT. Jar run project
5. Browser access after successful startup http://localhost:8001 Enter account: root / password: root.
Appendix:
1. Each software version corresponds
versions
Java
Spring Boot
Dubbo
0.2.0
1.8+
2.0.x
2.6.2 +
0.1.1
1.7+
1.5.x
2.6.2 +