1. 程式人生 > 其它 >Dubbo入門前功課,spring boot 實現RPC 遠端呼叫

Dubbo入門前功課,spring boot 實現RPC 遠端呼叫

個人理解:Dubbo是一個實現了複雜功能的RPC框架

寫一個簡單的demo 來了解下RPC http呼叫

新建一個spring專案 三個子模組 服務提供者provider、服務呼叫者consumer、介面service,

service定義個實體類 user

package com.example.dubbodemoservice;

public class User {
    private String name;

    public User(String name){
        this.name= name;
    }

    public String getName() {
        
return name; } public void setName(String name) { this.name = name; } }

提供者 需要提供專案依賴 才能獲取到user類

ProviderService
package com.example.dubbodemoprovider;


import com.example.dubbodemoservice.User;
import org.springframework.stereotype.Service;

@Service
public class ProviderService {
    
public User getUser() { return new User("tesRpc") { }; } }

controller

package com.example.dubbodemoprovider;


import com.example.dubbodemoservice.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/provider") public class ProviderController { @Autowired private ProviderService providerService; @RequestMapping(value = "/service") public User test() { return providerService.getUser(); } }

application.properties

# 應用名稱
spring.application.name=dubbo-demo-provider
# 應用服務 WEB 訪問埠
server.port=8080

啟動provider 看是否能夠呼叫

 沒毛病,介面可以正常訪問。接下來 寫服務呼叫者

定義一個http請求實現類

package com.example.dubbodemoconsumer;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientTest {

    public static String get(String url) {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get);
            String res = EntityUtils.toString(response.getEntity());
            System.out.println(res);
            return res;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
ConsumerService
package com.example.dubbodemoconsumer;


import org.springframework.stereotype.Service;

@Service
public class ConsumerService {

    public String test() {
        String aa = HttpClientTest.get("http://localhost:8080/provider/service");
        return aa;
    }
}

controller

package com.example.dubbodemoconsumer;

import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private ConsumerService service;
    @RequestMapping("/getName")
    public  String test(){
        return  service.test();
    }

}

application.properties 修改下埠 

# 應用名稱
spring.application.name=dubbo-demo-consumer
server.port=8081

啟動consumer 看是否能夠呼叫

 以上就是一個簡單rpc遠端呼叫

總結 dubbo 就是把複雜的邏輯簡單,我們不需要手寫這麼多的程式碼。

只需要加個@service註解,把介面服務註冊到伺服器上。然後就可以像呼叫本地方法一樣呼叫其他模組的介面服務