您好,登录后才能下订单哦!
这篇文章主要介绍“SpringBoot怎么搭建Dubbo项目实现斐波那契第n项”,在日常操作中,相信很多人在SpringBoot怎么搭建Dubbo项目实现斐波那契第n项问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringBoot怎么搭建Dubbo项目实现斐波那契第n项”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
方法1:直接在IDEA里新建如图:
方法2:在start.spring.io新建
可能有的小朋友已经发现了,第一种方式的Server URL就是第二个的网站,都是一样的
要新建两个项目,第一个项目如上图所示,第二个项目只需要将provider改为consumer
其余不变,解压后通过IDEA打开
provider 项目结构:
consumer 项目结构:
请注意,两个ServiceAPI在项目中的的所在位置,或者说包名要严格一致,否则后续会出问题
如果不一致,可以按照如下方式按需进行更改
xsd放到指定目录下:
C:\Users\用户名\.lemminx\cache\http\code.alibabatech.com\schema\dubbo
文件: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>
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>
在两个项目的pom.xml下均添加依赖:
<dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
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); } }
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运行的端口号:
首先运行provider,然后运行consumer
结果:
到此,关于“SpringBoot怎么搭建Dubbo项目实现斐波那契第n项”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。