微服务框架-Dubbo整合SpringBoot框架-宝典
一、前言:
最近写了一篇SpringCloud整合Dubbo的技术贴,本篇内容有点类似,Dubbo本身就是微服务框架,整合SpringBoot之后,可以吸收SpringBoot框架的自动装配功能;
Dubbo集成SpringBoot框架的目的,是为了实现Springcloud的标准,如果想在SpringCloud生态中集成阿里的组件,整合比较复杂,接下来,我们先介绍一下如何整合:
二、代码示例:
1、代码结构:
2、 创建maven quickstart工程-springboot-dubbo-api ,编写服务端与客户端交互的接口契约,同时安装到Maven私有仓库。
package com.jason.dubbo.springboot;
/**
* @program: springcloud-dubbo
* @description
* @author: 大龄程序猿
* @create: 2020-08-15 23:59
**/
public interface ISayHelloService {
String hello();
}
3、 创建maven quickstart工程-springboot-dubbo-consumer ,根据接口契约,编写客户端的代码
package com.jason.dubbo.springboot;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Hello world!
*
*/
@RestController
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
}
@DubboReference
ISayHelloService sayHelloService;
@RequestMapping("/say")
public String say()
{
return sayHelloService.hello();
}
}
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">
<parent>
<artifactId>springboot-dubbo</artifactId>
<groupId>com.jason.dubbo.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-dubbo-consumer</artifactId>
<name>springboot-dubbo-consumer</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>springboot-dubbo-api</artifactId>
<groupId>com.jason.dubbo.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
application.properties
spring.application.name=spring-boot-dubbo-consumer
server.port=8089
dubbo.registry.address=nacos://192.168.213.102:8848
4、 创建Spring web工程-springboot-dubbo-server,根据接口契约,编写接口实现,同时将服务发布到nacos服务注册中心
package com.jason.dubbo.springboot.springbootdubboserver.service;
import com.jason.dubbo.springboot.ISayHelloService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
/**
* @program: springcloud-dubbo
* @description
* @author: 大龄程序猿
* @create: 2020-08-16 00:44
**/
@DubboService
public class SayHelloServiceImpl implements ISayHelloService {
@Override
public String hello() {
return "Say hello Spring-boot-dubbo";
}
}
package com.jason.dubbo.springboot.springbootdubboserver;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@DubboComponentScan(basePackages = "com.jason.dubbo.springboot.springbootdubboserver.service")
@SpringBootApplication
public class SpringbootDubboServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDubboServerApplication.class, args);
}
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-dubbo</artifactId>
<groupId>com.jason.dubbo.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<artifactId>springboot-dubbo-api</artifactId>
<groupId>com.jason.dubbo.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.1</version>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
<configuration>
<mainClass>com.jason.dubbo.springboot.springbootdubboserver.SpringbootDubboServerApplication
</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.application.name=spring-boot-dubbo-server
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=nacos://192.168.213.102:8848
三、功能调试:
分别启动nacos 注册中心,同时将服务注册到nacos,客户端通过调试URL,完成服务端的接口访问:
本文地址:https://blog.csdn.net/jason_jiahongfei/article/details/108030305
下一篇: Lombok组件使用介绍