方法1:直接在IDEA新建如圖:
##方法2:在start.spring.io新建
可能有的小朋友已經發現了,第一種方式的Server URL就是第二個的網站,都是一樣的要新建兩個項目,第一個項目如上圖所示,第二個項目只需要將provider改為consumer#其餘不變,解壓縮後透過IDEA開啟
step2 新建所需的套件和介面以及實作類別
provider 專案結構:
##consumer 專案結構:
##請注意,兩個ServiceAPI在專案中的所在位置,或者說套件名稱要嚴格一致,否則後續會出問題
如果不一致,可以按照如下方式按需進行更改
xsd放到指定目錄下:檔:
dubbo.xsd
#需要新建的名字為:
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>
<?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>
在兩個專案的pom.xml下均加入依賴:
<dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
#ProviderImpl
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); }
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); } }
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); }
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)); } }
以上是SpringBoot怎麼搭建Dubbo專案實現斐波那契第n項的詳細內容。更多資訊請關注PHP中文網其他相關文章!