十九SpringBoot2核心技术——整合Alibaba Dubbo
Posted 上善若水
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了十九SpringBoot2核心技术——整合Alibaba Dubbo相关的知识,希望对你有一定的参考价值。
一、SpringBoot整合Alibaba Dubbo
1.1、项目工程、添加依赖
1.2、添加Alibaba Dubbo依赖
<dependencies>
<!--接口工程-->
<dependency>
<groupId>com.xbmu</groupId>
<artifactId>004-springboot-dubbo-interface</artifactId>
<version>1.0.0</version>
</dependency>
<!--SpringBoot框架web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Dubbo集成SpringBoot框架起步依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--ZK客户端依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
<!--排除依赖里的日志-->
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
1.2、工程代码
1.2.1、服务接口工程
Student.java
package com.xbmu.pojo;
import java.io.Serializable;
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
// set 、 get 方法
}
StudentService.java
package com.xbmu.service;
import com.xbmu.pojo.Student;
public interface StudentService {
Student findAllStudent();
}
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.xbmu</groupId>
<artifactId>004-springboot-dubbo-interface</artifactId>
<version>1.0.0</version>
</project>
1.2.1、服务提供者工程
StudentServiceImpl.java
package com.xbmu.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.xbmu.pojo.Student;
import com.xbmu.service.StudentService;
import org.springframework.stereotype.Component;
@Component
// @Service 这个注解使用的不是spring里面的,而是com.alibaba.dubbo.config.annotation.Service。
// timeout 配置超时时间 , interfaceClass 接口类 ,version 服务版本,如果配置了服务版本在消费端引用也必须一样
@Service(timeout = 1000,interfaceClass = StudentService.class)
public class StudentServiceImpl implements StudentService {
@Override
public Student findAllStudent() {
// 模拟数据库,查询出学生信息
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(18);
return student;
}
}
ProviderApplication.java
package com.xbmu;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo // 启动dubbo功能
@EnableDubboConfiguration // 启动dubbo配置
@DubboComponentScan("com.xbmu.service.impl") // 扫描提供者实现类
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
application.yml
# 设置内嵌tomcat端口号
server:
port: 8091
servlet:
context-path: /
spring:
application:
name: springboot-dubbo-provider
# 设置dubbo的配置
dubbo:
application:
name: springboot-dubbo-provider
registry: # 注册中心
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20880
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xbmu</groupId>
<artifactId>005-springboot-dubbo-provider</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.0.0</dubbo.version>
<zkclient.version>0.10</zkclient.version>
</properties>
<dependencies>
<!--接口工程-->
<dependency>
<groupId>com.xbmu</groupId>
<artifactId>004-springboot-dubbo-interface</artifactId>
<version>1.0.0</version>
</dependency>
<!--SpringBoot框架web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Dubbo集成SpringBoot框架起步依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!--ZK客户端依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
<!--排除依赖里的日志-->
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<!--springboot编译打包插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2.1、服务消费者工程
StudentController.java
package com.xbmu.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.xbmu.pojo.Student;
import com.xbmu.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class StudentController {
// 在这里是使用@Reference去发现服务而不是@Autowired去注入Bean,@Reference里面可以配置version、timeout超时时间
@Reference
private StudentService studentService;
@RequestMapping("/findAllStudent")
public @ResponseBody Student findAllStudent(){
return studentService.findAllStudent();
}
}
ConsumerApplication.java
package com.xbmu;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
@EnableDubboConfiguration
// 如果不想使用注解扫描,可以在springboot核心配置文件中进行配置
// dubbo.scan=com.xbmu.service.impl
@DubboComponentScan("com.xbmu.service.impl")
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
application.yml
# 设置内嵌tomcat端口号
server:
port: 8092
servlet:
context-path: /
spring:
application:
name: springboot-dubbo-consumer
# 设置dubbo配置
dubbo:
application:
name: springboot-dubbo-consumer
registry:
address: zookeeper://192.168.184.2:2181
protocol:
name: dubbo
port: 20880
# scan: com.xbmu.service.impl 该配置指向应该是要和服务提供方一致
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xbmu</groupId>
<artifactId>006-springboot-dubbo-consumer</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.0.0</dubbo.version>
<zkclient.version>0.10</zkclient.version>
</properties>
<dependencies>
<!--接口工程-->
<dependency>
<groupId>com.xbmu</groupId>
<artifactId>004-springboot-dubbo-interface</artifactId>
<version>1.0.0</version>
</dependency>
<!--SpringBoot框架web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Dubbo集成SpringBoot框架起步依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!--ZK客户端依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
<!--排除依赖里的日志-->
<exclusions>
<exclusion>
<groupId>log4j以上是关于十九SpringBoot2核心技术——整合Alibaba Dubbo的主要内容,如果未能解决你的问题,请参考以下文章