教你springboot+dubbo快速啟動的方法
前言
現(xiàn)在用dubbo的太多了,我到現(xiàn)在還不熟悉,這太不應該了,這次好好看了一下dubbo,終于把基本的啟動框架搭好了。dubbo的角色寬泛的分三類provider,comsumer,注冊中心。我這里的注冊中心用的是zookeeper,并且是在windows環(huán)境下做的。
dubbo和springboot整合的依賴:
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
實操
1、創(chuàng)建一個大的maven項目,將里面的src目錄刪掉,這只是為了在這個大的maven項目里面添加provider和comsumer兩個模塊。之后創(chuàng)建兩個springboot項目,分別是provider和comsumer,創(chuàng)建好的結構:
這里的common模塊我是把entity和接口類抽出來,不抽出來也是可以的
2、common(這里面沒有任何依賴,就是個maven項目)
entity:
public class User { private Long id; private String name; private Integer age; public User() { } public User(Long id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
UserService:
public interface UserService { User query(Long id); }
OrderService:
public interface OrderService { String getName(Long id); }
3、provider(添加dubbo依賴并把剛才寫的公共模塊依賴進來)
<dependency> <groupId>org.example</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
OrderServiceImpl:
import com.alibaba.dubbo.config.annotation.Service; //這里的service注解用的是dubbo @Service public class OrderServiceImpl implements OrderService { @Autowired private UserService userService; @Override public String getName(Long id) { User query = userService.query(id); return query.getName(); } }
UserServiceImpl:
import com.alibaba.dubbo.config.annotation.Service; @Service public class UserServiceImpl implements UserService { @Override public User query(Long id) { User user = new User(1L, "張三", 12); return user; } }
啟動類上添加@EnableDubbo
application.properties:
#服務名稱 dubbo.application.name=provice #注冊中心 dubbo.registry.address=127.0.0.1:2181 dubbo.registry.protocol=zookeeper #通信協(xié)議 dubbo.protocol.name=dubbo dubbo.protocol.port=20880
4、comsumer(添加dubbo依賴,common模塊,web依賴)
OrderController:
import com.alibaba.dubbo.config.annotation.Reference; @RestController public class OrderController { //添加dubbo的reference注解,進行遠程調用 @Reference OrderService orderService; @RequestMapping("getName") String getName(Long id){ String name = orderService.getName(id); return name; } }
啟動類上加上@EnableDubbo
application.properties:
#服務名稱 dubbo.application.name=comsumer #注冊中心 dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181
測試
啟動zookeeper,provider和comsumer.
注:
1、首先要啟動zookeeper,下載windows版壓縮包,修改conf配置文件,啟動zkServer.cmd命令。具體怎么弄這里就不寫了,網(wǎng)上很多,我這里主要練習的是dubbo.
2、啟動類上需要添加@EnableDubbo
3、comsumer中調用服務用@Reference,因為是遠程調用,所以 @Autowired肯定是沒用的了
4、provider中的實現(xiàn)類需要用dubbo的@Service注解
到此這篇關于springboot+dubbo快速啟動的文章就介紹到這了,更多相關springboot dubbo啟動內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring中@RestController和@Controller的區(qū)別小結
@RestController和@Controller這兩個注解用于創(chuàng)建Web應用程序的控制器類,那么這兩個注解有哪些區(qū)別,本文就來介紹一下,并用示例代碼說明,感興趣的可以了解一下2023-09-09Spring-Security對HTTP相應頭的安全支持方式
這篇文章主要介紹了Spring-Security對HTTP相應頭的安全支持方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10Sentinel結合Nacos實現(xiàn)數(shù)據(jù)持久化過程詳解
這篇文章主要介紹了Sentinel結合Nacos實現(xiàn)數(shù)據(jù)持久化過程,要持久化的原因是因為每次啟動Sentinel都會使之前配置的規(guī)則就清空了,這樣每次都要再去設定規(guī)則顯得非常的麻煩,感興趣想要詳細了解可以參考下文2023-05-05