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注解,把接口服务注册到服务器上。然后就可以像调用本地方法一样调用其他模块的接口服务

 

posted @ 2022-05-22 13:38  奋斗的渣渣  阅读(876)  评论(0编辑  收藏  举报