2.Spring&SpringBoot整合Dubbo
目录
Dubbo
Apache Dubbo 是⼀款⾼性能、轻量级的开源服务框架。
Apache Dubbo 提供了六⼤核⼼能⼒:⾯向接⼝代理的⾼性能RPC调⽤,智能容错 和负载均衡,服务⾃动注册和发现,⾼度可扩展能⼒,运⾏期流量调度,可视化的服务治理 与运维
Dubbo实现RPC原理
服务消费者去注册中⼼订阅到服务提供者的信息。然后通过dubbo进⾏远程调⽤。
相当于dubbo自定义了通信方式以及方法的代理,由手写RPC框架实现的代理、注册等代码都交给了Dubbo
Spring整合Dubbo
首先启动Zookeeper,如何启动见此处
创建Maven父工程
创建接口项目
package com.rb.myInterface;
/**
* 定义一个功能接口
*/
public interface HelloWordService {
String helloWord(String parameter);
}
创建服务提供项目(生产者)
POM
<?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>dubbo-by-xml-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-producer</artifactId>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>my-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
XML
producer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 服务名称-->
<dubbo:application name="hello-word-service"/>
<!-- 连接集群-->
<dubbo:registry address="zookeeper://192.168.0.104:2181,192.168.0.104:2182,192.168.0.104:2183,192.168.0.104:2184"/>
<!--配置当前这个服务在dubbo容器中的端⼝号,每个dubbo容器内部的服务的端⼝号唯一-->
<dubbo:protocol name="dubbo" port="20881"/>
<!--HelloWordService,指明该服务具体的实现bean是helloWordImpl-->
<dubbo:service interface="com.rb.myInterface.HelloWordService" ref="helloWordImpl"/>
<!--将服务提供者的bean注⼊到ioc容器中-->
<bean id="helloWordImpl" class="com.rb.impl.HelloWordImpl"/>
</beans>
接口实现方法
package com.rb.impl;
import com.rb.myInterface.HelloWordService;
/**
* 对接口的实现
*/
public class HelloWordImpl implements HelloWordService {
public String helloWord(String s) {
return "HelloWordImpl:"+s;
}
}
生产者启动
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ProducerRun {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"producer.xml"});
context.start();
System.in.read();
}
}
创建服务调用项目(消费者)
POM
<?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>dubbo-by-xml-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-customer</artifactId>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>my-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 服务名称-->
<dubbo:application name="hello-word-consumer"/>
<!-- 连接集群-->
<dubbo:registry address="zookeeper://192.168.0.104:2181,192.168.0.104:2182,192.168.0.104:2183,192.168.0.104:2184"/>
<!--需要调⽤的dubbo中的哪个服务,注入接口全名,把HelloWordService交给IOC容器,名为helloWordService-->
<dubbo:reference interface="com.rb.myInterface.HelloWordService" id="helloWordService"/>
</beans>
消费者启动
import com.rb.myInterface.HelloWordService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CustomerRun {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"customer.xml"});
context.start();
//RPC 开始
//获取⼀个代理,(类似手写RPC框架中ProxyFactory.java,只不过代理交给Dubbo了)的代理服务提供者内提供的bean
HelloWordService helloWordService = (HelloWordService)context.getBean("helloWordService");
//调⽤代理对象的getName⽅法。通过代理对象调到服务提供者内的bean
String result = helloWordService.helloWord("rb");
System.out.println(result);
//RPC 结束
}
}
Spring整合代码下载
SpringBoot整合Dubbo
创建服务提供项目(生产者)
POM
<!-- 引入‘手写RPCDemo’时创建的接口jar-->
<dependency>
<groupId>org.example</groupId>
<artifactId>rpc-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-registry-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>3.0.6</version>
</dependency>
YML
dubbo:
application:
name: hello-word-service #服务名称
registry:
address: zookeeper://192.168.0.104:2181,192.168.0.104:2182,192.168.0.104:2183,192.168.0.104:2184 #zk地址
protocol:
port: 20881 #配置当前这个服务在dubbo容器中的端⼝号,每个dubbo容器内部的服务的端⼝号唯一
接口实现方法
添加@DubboService
package com.rb.producer.impl;
import myinterface.HelloWord;
import org.apache.dubbo.config.annotation.DubboService;
/**
* 对接口的实现
*/
//添加Dubbo注解
@DubboService
public class HelloWordImpl implements HelloWord {
public String helloWord(String s) {
return "HelloWordImpl:"+s;
}
}
开启Dubbo支持
添加@EnableDubbo
package com.rb.producer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
启动服务
创建服务调用项目(消费者)
POM
<!-- 引入手写RPC框架时创建的接口jar-->
<dependency>
<groupId>org.example</groupId>
<artifactId>rpc-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-registry-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>3.0.6</version>
</dependency>
YML
server:
port: 8081
dubbo:
application:
name: hello-word-consumer
registry:
address: zookeeper://192.168.0.104:2181,192.168.0.104:2182,192.168.0.104:2183,192.168.0.104:2184 #zk地址
开启Dubbo支持
package com.rb.customer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
}
发起请求
@DubboReference
- 服务启动去zk中订阅提供HelloWord服务的URL
- 将IOC中的代理对象注入
package com.rb.customer;
import com.alibaba.dubbo.config.annotation.Reference;
import myinterface.HelloWord;
import org.apache.dubbo.config.annotation.DubboReference;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class CustomerApplicationTests {
//通过Dubbo引入代理对象
@DubboReference
private HelloWord helloWord;
@Test
void contextLoads() {
//执行代理方法
System.out.println(helloWord.helloWord("RB"));
}
}