更新時間:2021-06-11 15:29:17 來源:動力節點 瀏覽1183次
用dubbo做一個“hello world”。
此次demo十分簡單,旨在對Dubbo有個整體上的初步了解。服務提供者(程序)和服務消費者(程序)雖然都是運行在同個服務器上(本地tomcat),但是調用是通過Dubbo的RPC。
注冊中心是redis,部署在本地虛擬機,地址為192.168.1.66:6379(在配置文件中需要用到)。最終達到效果是服務消費者(Consumer)調用服務提供者(Provider)的sayHello()方法在控制臺輸出“Hello world”。
需要做的事情:
pom.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.2.0</version>
</dependency>
服務接口DemoService
public interface DemoService {
String sayHello(String str);
}
服務接口實現DemoService
public class DemoServiceImpl implements DemoService {
public String sayHello(String str) {
return "Hello " + str;
}
}
consumer.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="demo-consumer"/>
<dubbo:registry protocol="redis" address="192.168.1.66:6379" check="true"/>
<dubbo:consumer timeout="5000" retries="2"
group="snowman"
version="1.0.0"/>
<dubbo:reference
timeout="3000" retries="1"
id="demoService"
version="*"
interface="com.snowman.service.DemoService"/>
</beans>
provider.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="demo-provider"/>
<dubbo:registry protocol="redis" address="192.168.1.66:6379" check="true"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:provider group="snowman"
threadpool="fixed"
threads="500"
timeout="5000"
retries="2"
/>
<dubbo:service interface="com.snowman.service.DemoService"
timeout="5000"
retries="1"
version="3.0.0"
ref="demoService">
<dubbo:method name="sayHello" timeout="2000"/>
</dubbo:service>
<bean id="demoService" class="com.snowman.service.DemoServiceImpl"/>
</beans>
服務提供者Provider
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"config/spring-dubbo-redis-provider.xml");
context.start();
System.out.println("dubbo redis 服務啟動成功 ");
System.in.read();
}
}
項目工程結構
先啟動redis,因為它是注冊中心(在兩個xml都配置了redis的地址),沒起來會報錯,也沒辦法通知Consumer調用。
運行Provider,可以在控制臺看到輸出“dubbo redis 服務啟動成功 ”
再運行Consumer,可以在控制臺看到輸出“Hello world ”,遠程調用Provider的服務成功。
再去看redis,可以看到Provider和Consumer都把相關信息發送到注冊中心
(RedisDesktopManager,一個redis可視化工具)
流程簡述:
(調用過程跟注冊中心沒半毛錢關系,信息是從緩存取得,取得注冊中心的信息后,就算注冊中心掛掉可以正常調用)
以上就是動力節點小編介紹的"學IT什么培訓學校好",希望對大家有幫助,如有疑問,請在線咨詢,有專業老師隨時為您服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習