參考文獻: 當當開源文檔
上篇文章已經(jīng)創(chuàng)建了一個簡單的demo了戚宦。
REST的優(yōu)點:
- 可更高效利用緩存來提高響應速度
- 通訊本身的無狀態(tài)性可以讓不同的服務器的處理一系列請求中的不同請求躲雅,提高服務器的擴展性
- 瀏覽器即可作為客戶端涵妥,簡化軟件需求
- 相對于其他疊加在HTTP協(xié)議之上的機制,REST的軟件依賴性更小
- 不需要額外的資源發(fā)現(xiàn)機制
- 在軟件技術演進中的長期的兼容性更好
使用場景:
- 跨語言調(diào)用: 前端WEB直接訪問服務
- 開放平臺API
-
企業(yè)內(nèi)部不同平臺調(diào)用
消費場景
- 非dubbo的消費端調(diào)用dubbo的REST服務(non-dubbo --> dubbo)
- dubbo消費端調(diào)用dubbo的REST服務 (dubbo --> dubbo)
- dubbo的消費端調(diào)用非dubbo的REST服務 (dubbo --> non-dubbo)
非dubbo消費端消費
創(chuàng)建SpringBoot工程,引入web依賴即可。這里使用了OKhttp3調(diào)用http請求所以依賴如下:
<dependency>
<groupId>com.zkdubbo</groupId>
<artifactId>zk-dubbo-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
demo :
@RestController
@RequestMapping("/hello")
public class HelloController {
@Value("${https.hello}")
private String url;
@GetMapping("/get")
public DubboResponse get(){
DubboResponse syncRequest = OKHttpUtils.getSyncRequest(url);
return syncRequest;
}
@PostMapping("/post")
public DubboResponse post(@RequestBody Object object){
return OKHttpUtils.postSyncRequest(url, object);
}
}
/**
* @author bertram.wang
* @sign 2019/7/7
* @desc Http調(diào)用工具
*/
public class OKHttpUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(OKHttpUtils.class);
private static final OkHttpClient HTTP_CLIENT = new OkHttpClient();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
/**
* 同步的GET調(diào)用
* @return
*/
public static DubboResponse getSyncRequest(String url){
LOGGER.info("GET 調(diào)用請求:{}", url);
Request request = new Request.Builder().get().url(url).build();
try {
Response response = HTTP_CLIENT.newCall(request).execute();
DubboResponse dubboResponse = OBJECT_MAPPER.readValue(response.body().bytes(), DubboResponse.class);
return dubboResponse;
} catch (IOException e) {
LOGGER.error("GET 調(diào)用請求:{}異常堰怨, 原因: {}", url, e.getMessage());
return DubboResponseBuild.fill(e.getMessage());
}
}
/**
* 同步的POST調(diào)用
* @return
*/
public static DubboResponse postSyncRequest(String url, Object data){
LOGGER.info("POST 調(diào)用請求:{}", url);
try {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), OBJECT_MAPPER.writeValueAsString(data));
Request request = new Request.Builder().post(requestBody).url(url).build();
Response response = HTTP_CLIENT.newCall(request).execute();
DubboResponse dubboResponse = OBJECT_MAPPER.readValue(response.body().bytes(), DubboResponse.class);
return dubboResponse;
} catch (IOException e) {
LOGGER.error("POST 調(diào)用請求:{}異常, 原因: {}", url, e.getMessage());
return DubboResponseBuild.fill(e.getMessage());
}
}
}
server:
port: 8880
https:
hello: http://127.0.0.1:20880/servers/zkDubboServer/hello
非常簡單的一個消費demo蛇摸。
服務提供者會發(fā)送MQ消息并消費备图。
@Override
public DubboResponse hello() {
try {
kafkaProducer.send("測試發(fā)送MQ");
} catch (Exception e) {
return DubboResponseBuild.fill(e.getMessage());
}
return DubboResponseBuild.success();
}
@Component
public class MyKafkaProducer<T> {
@Resource
private KafkaTemplate<String, T> kafkaTemplate;
public void send(T message) throws Exception{
kafkaTemplate.send("bertram.topic", message);
}
}
@Component
public class KafkaConsumer {
@KafkaListener(topics = "bertram.topic")
public void listen (ConsumerRecord<?, ?> record) throws Exception {
System.out.printf("topic = %s, offset = %d, value = %s \n", record.topic(), record.offset(), record.value());
}
}