您好,登錄后才能下訂單哦!
這篇文章主要介紹了Dubbo怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
依賴:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--接口定義--> <dependency> <groupId>com.example</groupId> <artifactId>api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> </dependency> <!--zookeeper 依賴--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> </project>
服務提供方配置
package com.example.order.service; import com.example.api.Order; import com.example.api.OrderService; import org.apache.dubbo.config.annotation.Service; import java.util.Date; @Service public class OrderServiceImpl implements OrderService { @Override public Order getOrder() { Order order = new Order(); order.setId(1L); order.setOrderName("xxx"); order.setCreateTime(new Date()); return order; } }
application.yml
server: port: 4059 tomcat: uri-encoding: UTF-8 servlet: context-path: /order # session: # timeout: 300s dubbo: application: ###########注冊到注冊中心的名稱############ name: ordder ###########采用協議和端口號################ protocol: ###########采用dubbo協議#################### name: dubbo ###########發布dubbo端口號為40591########### port: 40591 registry: ###########注冊中心地址##################### address: zookeeper://192.168.0.18:2181 scan: ############實現類掃包范圍################### base-packages: com.example.order.service spring: output: ansi: enabled: always
服務消費者
package com.example.user.controller; import com.example.api.OrderService; import org.apache.dubbo.config.annotation.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Reference private OrderService orderService; @RequestMapping("/order") public Object getUser() { return this.orderService.getOrder(); } }
application.yml
server: port: 4058 tomcat: uri-encoding: UTF-8 servlet: context-path: /user # session: # timeout: 300s dubbo: application: ##### 注冊服務的名稱 name: user ##### 注冊中心地址 registry: address: zookeeper://192.168.0.18:2181 ##### 調用服務超時時間 consumer: timeout: 5000 spring: output: ansi: enabled: always
注意項目啟用要使用 @EnableDubbo
接口注冊要使用: Service
接口引用要使用:Reference
@EnableDubbo
org.apache.dubbo.config.annotation.Service
org.apache.dubbo.config.annotation.Reference
@Configuration @EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.impl") @PropertySource("classpath:/spring/dubbo-provider.properties") static public class ProviderConfiguration { }
@Configuration @EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.action") @PropertySource("classpath:/spring/dubbo-consumer.properties") @ComponentScan(value = {"org.apache.dubbo.samples.simple.annotation.action"}) static public class ConsumerConfiguration { }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Dubbo怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。