SpringBoot搭建Dubbo項目實現斐波那契第n項詳解

step1 新建項目

方法1:直接在IDEA裡新建如圖:

方法2:在start.spring.io新建

可能有的小朋友已經發現瞭,第一種方式的Server URL就是第二個的網站,都是一樣的

要新建兩個項目,第一個項目如上圖所示,第二個項目隻需要將provider改為consumer

其餘不變,解壓後通過IDEA打開

step2 新建需要的包和接口以及實現類

provider 項目結構:

consumer 項目結構:

請註意,兩個ServiceAPI在項目中的的所在位置,或者說包名要嚴格一致,否則後續會出問題

如果不一致,可以按照如下方式按需進行更改

xsd放到指定目錄下:

C:\Users\用戶名\.lemminx\cache\http\code.alibabatech.com\schema\dubbo

文件:dubbo.xsd

下載鏈接:

「dubbo.xsd」

鏈接: https://pan.baidu.com/s/1iqQl-TlrHW2CsGN9kG5UoA?pwd=6xp4 提取碼: 6xp4 

點擊鏈接保存,或者復制本段內容,打開「阿裡雲盤」APP ,無需下載極速在線查看,視頻原畫倍速播放。

step3 在兩個項目的resource下新建配置文件

需要新建的名字為:spring-dubbo.xml

provider的該配置文件中寫入:

<?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應用名稱 -->
    <dubbo:application name="springboot-buddo-provider"/>
    <!-- 發佈者 dubbo協議  -->
    <dubbo:protocol name="dubbo" port="20881"/>
    <!-- 定義bean -->
    <bean id="providerImpl" class="com.springdubbo.demo.springbootdubbo.apiImpl.ProviderImpl"/>
    <!-- dubbo服務 發佈者發佈服務  需要暴露的服務接口 -->
    <dubbo:service interface="com.springdubbo.demo.springbootdubbo.ServiceAPI" ref="providerImpl"
                   registry="N/A"/>
</beans>

consumer的該配置文件內寫入:

<?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應用名稱 -->
    <dubbo:application name="springboot-buddo-consumer"/>
    <!-- 遠程服務調用代理 -->
    <dubbo:reference id="consumerImpl"
                     interface="com.springdubbo.demo.springbootdubbo.ServiceAPI"
                     url="dubbo://localhost:20881" />
</beans>

step4 代碼編寫

導入依賴

在兩個項目的pom.xml下均添加依賴:

<dependency>
	<groupId>com.alibaba.spring.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>2.0.0</version>
</dependency>

provider

ProviderImpl

Service註解一定不要忘記加,而且一定要是dubbo的Service

package com.springdubbo.demo.springbootdubbo.apiImpl;
import com.alibaba.dubbo.config.annotation.Service;
import com.springdubbo.demo.springbootdubbo.ServiceAPI;
/**
 * @author wuyt
 * @data 2022/6/11
 * @apiNote
 */
@Service
public class ProviderImpl implements ServiceAPI {
    public String getMessage(String message) {
        return "springboot-dubbo-provider  =>>>>>" + message;
    }
    public String getTheFeibN(int n) {
        //斐波那契數列第n項的實現邏輯
    }
}

ServiceAPI

package com.springdubbo.demo.springbootdubbo;
/**
 * @author wuyt
 * @data 2022/6/11
 * @apiNote
 */
public interface ServiceAPI {
    public String getMessage(String message);
    public String getTheFeibN(int n);
}

SpringbootDubboApplication

一定要加上ImportResource註解

package com.springdubbo.demo.springbootdubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:spring-dubbo.xml")
public class SpringbootDubboApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootDubboApplication.class, args);
    }
}

consumer

ServiceAPI

package com.springdubbo.demo.springbootdubbo;
/**
 * @author wuyt
 * @data 2022/6/11
 * @apiNote
 */
public interface ServiceAPI {
    public String getMessage(String message);
    public String getTheFeibN(int n);
}

SpringbootDubboApplication

package com.springdubbo.demo.springbootdubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:spring-dubbo.xml")
public class SpringbootDubboApplication {
	public static void main(String[] args) {
		ConfigurableApplicationContext count = SpringApplication.run(SpringbootDubboApplication.class, args);
		ServiceAPI impl = (ServiceAPI)count.getBean("consumerImpl");
//		System.out.println(impl.getMessage("Hello dubbo"));
		System.out.println(impl.getTheFeibN(10));
	}
}

端口沖突更改

可以修改provider或者是consumer其中任意一個的端口號

這裡修改consumer運行的端口號:

step5 運行

首先運行provider,然後運行consumer

結果:

到此這篇關於SpringBoot搭建Dubbo項目實現斐波那契第n項詳解的文章就介紹到這瞭,更多相關SpringBoot斐波那契內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: