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

Example Analysis of SpringBoot with Apache Dubbo Project

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the SpringBoot with Apache Dubbo project example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. Description

The project example uses the latest Dubbo version, Dubbo Starter version and SpringBoot version, manages and builds the project through Gradle, and outputs executable programs.

two。 Framework version 2.1 Dubbo version

Org.apache.dubbo:dubbo:2.7.1

Org.apache.dubbo:dubbo-spring-boot-starter:2.7.1

2.2 SpringBoot version

Org.springframework.boot:spring-boot-dependencies:2.1.1.RELEASE

3. Module relationship

Root project (dubbo-case), which is only used to define construction tasks, engineering information

A sub-project (dubbo-api) that defines the interface of the RPC service, parameters, and data transfer objects for response results

Sub-project (dubbo-client), the consumer side of RPC services, the actual situation in the actual development process is that some services call RPC services of other services

Subproject (dubbo-server), the provider of RPC service

4. Project details 4.1 Engineering Information

Code repository: https://github.com/broncho/dubbo-case

Project directory structure

Note: script directory archive execution script template information

4.2 Interface definition (dubbo-api) public interface HelloService {String sayHello (String name);} 4.3 Service provider (dubbo-server)

Interface implementation

Import com.github.broncho.dubbo.api.HelloService;import org.apache.dubbo.config.annotation.Service;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;/** * Author: secondriver * Created: 2019-8-4 * / @ Servicepublic class HelloServiceImpl implements HelloService {@ Override public String sayHello (String name) {return "Welcome" + name + "at" + LocalDateTime.now (). Format (DateTimeFormatter.ISO_LOCAL_DATE_TIME);}

Note: @ Service annotations are provided by the Dubbo framework

Startup class

@ SpringBootApplicationpublic class DubboServerApplication {public static void main (String [] args) {new EmbeddedZooKeeper (2181, true) .start (); SpringApplication.run (DubboServerApplication.class, args);}}

Note: embedded Zookeeper is used here. For implementation details, please see the source code, or you can directly use independent Zookeeper services.

Profile application.properties

# Application name dubbo.application.name=dubbo-app1dubbo.application.qosEnable=truedubbo.application.qosPort= 22222dubbo.application.qosAcceptForeignIp=true# interface implementer (service implementation) package dubbo.scan.base-packages=com.github.broncho.dubbo.server.impl# registry information dubbo.registry.id=my-zkdubbo.registry.address=localhost:2181dubbo.registry.protocol=zookeeperdubbo.registry.client=curator

For more information about the configuration of Dubbo, see: https://dubbo.apache.org/zh-cn/docs/user/references/api.html

4.4 Service consumers (dubbo-client)

Service consumption example

@ Componentpublic class BusinessComponent {@ Reference private HelloService helloService; public void greeting (String name) {System.out.println (helloService.sayHello (name));}}

Startup class

@ SpringBootApplicationpublic class DubboClientApplication {public static void main (String [] args) {ConfigurableApplicationContext context = SpringApplication.run (DubboClientApplication.class, args); BusinessComponent component = context.getBean (BusinessComponent.class); / / RPC component.greeting ("Dubbo RPC");}}

Configuration file (application.properties)

# Application name dubbo.application.name=dubbo-app2# registry information dubbo.registry.id=my-zkdubbo.registry.address=localhost:2181dubbo.registry.protocol=zookeeperdubbo.registry.client=curator5. Package deployment 5.1 Gradle configuration

Gradle.properties

SystemProp.activeProfile=devsystemProp.javaVersion=1.8

Build.gradle

Import org.springframework.boot.gradle.plugin.SpringBootPluginbuildscript {repositories {mavenLocal () maven {name "alimaven" url "http://maven.aliyun.com/nexus/content/groups/public/"} mavenCentral ()}} plugins {id 'java' id' idea' id 'org.springframework.boot' version' 2.1.RELEASE' apply false Id 'io.spring.dependency-management' version' 1.0.8.RELEASE' apply false} / / all project definitions allprojects {sourceCompatibility = System.properties ["javaVersion"] targetCompatibility = System.properties ["javaVersion"] repositories {mavenLocal () maven {name "alimaven" url "http://maven.aliyun.com/nexus/content/groups/public/"} MavenCentral ()} group 'com.github.broncho' version' 1.0.0'} / / Sub-project definition subprojects {apply plugin: 'java' apply plugin:' io.spring.dependency-management' apply plugin: 'distribution' if (! name.contains ("api")) {apply plugin:' org.springframework.boot' apply plugin: 'application'} dependencyManagement { Imports {mavenBom (SpringBootPlugin.BOM_COORDINATES)} dependencies {dependencySet (group: 'org.apache.dubbo') Version: '2.7.1') {entry' dubbo' entry 'dubbo-spring-boot-starter'}} dependencies {dependencySet (group:' org.apache.curator' Version: '4.0.0') {entry' curator-framework' entry 'curator-recipes'}} dependencies {dependency' org.apache.zookeeper:zookeeper:3.5.5'}} if (! project.name.contains ("api")) {println "Project ${name} activeProfile ${System.properties ['activeProfile']} "jar {enabled true} applicationDefaultJvmArgs = ['-Xms256m'] '- Xmx256m'] / * generate startup script * / startScripts () {doFirst {unixStartScriptGenerator.template = resources.text.fromFile (project.getRootDir (). GetAbsolutePath () + "/ script/unixStartScript.txt") windowsStartScriptGenerator.template = resources Configuration files cannot be excluded in .text.fromFile (project.getRootDir (). GetAbsolutePath () + "/ script/windowsStartScript.txt")}} / * development environment Otherwise, it is impossible to run the project * / if (! "dev" .equals (System.properties ['activeProfile'])) {proce***esources {exclude' application*.properties'}} / * application*.properties to the config directory * when the SpringBoot program starts Config directory read configuration file * / applicationDistribution.from ("src/main/resources") {include 'application*.properties' into' config'}

The project uses the combination of SpringBoot Gradle Plugin and dependency-management for SpringBoot version management and unified management of dependencies in sub-projects.

5.2 package command gradle-DactiveProfile=prod clean distZip

SpringBoot-based dubbo-server and dubbo-client projects export the release package to the build/distributions directory of their respective project directories.

5.3 release package format

Thank you for reading this article carefully. I hope the article "sample Analysis of SpringBoot with Apache Dubbo Project" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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