Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of integrating dubbo with springboot

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this "springboot Integration dubbo method" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "springboot Integration dubbo method" article.

The first step is to build a zookeeper environment

In the centos window, execute the following command to pull the image and start the zookeeper container

Docker pull zookeeperdocker 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 ZOO_MY_ID=1 2181-p 2888 ZOO_MY_ID=1 2888 2888-- name zookeeper-- privileged zookeeper

Note:

The default connection port of 1.zookeeper is 2181, but 2182 is used when testing the case in this article because it is occupied by other programs.

two。 Readers should create their own mapping directory zookeeperDataDir | zookeeperDataLogDir

The second step is to integrate springboot with dubbo1. Project catalogue organization

Description:

1.api directory: a service interface that stores consumer and provider calls

2.consumer directory: implementation of the interface provided remotely by the consumer directory invocation provider

3.provider directory: the provider directory is provided to the consumer interface implementation

Please create your own project directory (create an empty project and then create three new module in the empty project)

Project case description: business hypothesis scenario = "Product purchase consumption amount (consumer) and return the total amount of all consumption (need to be called to the service implementation in the provider project).

two。 Coding 2.1 api directory

Interface programming

2.1.1. Create a CostService.java under the package com.dubbo.api.service (readers please create it yourself, the same below, package creation will not be described in detail)

Package com.dubbo.api.service;public interface CostService {/ * cost increase API * @ param cost * @ return * / Integer add (int cost);}

2.1.2.pom.xml

4.0.0 com.dubbo api 0.0.1-SNAPSHOT jar api Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter Spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin 2.2 consumer directory web access, Interface call and dubbo configuration programming

2.2.1. Introduce dubbo-spring-boot-starter and the above api module

4.0.0 com.dubbo api 0.0.1-SNAPSHOT jar api Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter Org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin

2.2.2. Create application.yml in the resources directory and write the dubbo configuration

4.0.0 com.dubbo consumer 0.0.1-SNAPSHOT jar consumer Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web Org.springframework.boot spring-boot-starter-test test com.dubbo api 0.0.1-SNAPSHOT compile com.alibaba.boot dubbo-spring-boot-starter 0.2.0 Org.springframework.boot spring-boot-maven-plugin

2.2.3. Open dubbo using 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@EnableDubbopublic 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 the implementation of the product interface and call the remote service CostService. ProductServiceImpl.java

The 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 * / @ Servicepublic class ProductServiceImpl implements ProductService {/ * uses dubbo's annotated com.alibaba.dubbo.config.annotation.Reference. Make a remote call 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 * / @ RestControllerpublic class ProductController {@ Autowired private ProductService productService / * Total consumption returned 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. Introduce dubbo-spring-boot-starter and the above api module

4.0.0 com.dubbo provider 0.0.1-SNAPSHOT jar provider Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web Spring-boot-starter-test test com.dubbo api 0.0.1-SNAPSHOT compile com.alibaba.boot dubbo-spring-boot-starter 0.2.0 org.springframework.boot spring-boot-maven-plugin

2.3.2. Create application.yml in the resources directory and write the dubbo configuration

Dubbo: application: name: dubbo-provider registry: address: 125.77.116.145 dubbo-provider registry 2182 # readers please change the zookeeper address protocol: zookeeper check: false protocol: name: dubbo port: 30003 monitor: protocol: register consumer: check: false timeout: 3000server: port: 8061

2.3.3. Open dubbo using 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@EnableDubbopublic class ProviderApplication {public static void main (String [] args) {SpringApplication.run (ProviderApplication.class, args);}}

2.3.3. Write CostService services to implement CostServiceImpl.java

Package com.dubbo.provider.service.impl;import com.alibaba.dubbo.config.annotation.Service;import com.dubbo.api.service.CostService;/** * cost service * / @ Servicepublic class CostServiceImpl implements CostService {/ * assume a total cost of 100 * / private final Integer totalCost = 1000 / * before the sum plus the most recent * @ param cost * @ return * / @ Override public Integer add (int cost) {return totalCost + cost;}} third step to test the dubbo remote service call

After writing the second step of the code, start the consumer project and the provider project

Access http://localhost:8062/add?a=100 in a browser

The result above indicates that the call is successful.

Step 4 dubbo management platform

1. This article will be the operation and maintenance project code

two。 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. (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 "ASIS" 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.portspring.velocity.cache=falsespring.velocity.charset=UTF-8spring.velocity.layout-url=/templates/default.vmspring.messages.fallback-to-system-locale=falsespring.messages.basename=i18n/messagespring.root.password=rootspring.guest.password=guest# access password configuration spring.root.password spring.guest.password#dubbo.registry.address=zookeeper://127.0.0.1:2181dubbo.registry. Address=zookeeper://125.77.116.145:2182#zookeeper address

3. Under the D:learnplaceincubator-dubbo-opsdubbo-admin directory, go to the cmd window to execute

Mvn claen package packaged project

4. Then enter the D:learnplaceincubator-dubbo-opsdubbo-admin arget and go to the cmd window to execute

Java-jar dubbo-admin-0.0.1-SNAPSHOT.jar run the project

5. After starting successfully, the browser can access http://localhost:8001 and enter the account number: root / password: root.

The above is the content of this article on "the method of springboot integrating dubbo". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report