侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 748 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

微服务解决方案ServiceComb(6)之开发RPC方式微服务入门程序

zze
zze
2019-11-15 / 0 评论 / 0 点赞 / 408 阅读 / 5586 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

通过以 REST 方式入门程序的介绍,我们基本理解了 ServiceComb 中服务的发布和消费。那么现在我们以 RPC 方式同样介绍一下 ServiceComb 的服务发布与消费。它们不一样的就是服务的开发方式,但最终效果都是一样的。

工程架构

工程架构同 【Rest 方式入门程序】。

工程详细

父工程

父工程同【Rest 方式入门程序-父工程】。

接口子工程

接口子工程同【Rest 方式入门程序-接口子工程】。

提供者工程

依赖

在【Rest 方式入门程序-提供者子工程】的基础上再添加如下依赖:

<!--RPC 通信模型-->
<dependency>
    <groupId>org.apache.servicecomb</groupId>
    <artifactId>transport-highway</artifactId>
</dependency>
<!--RPC 编程模型-->
<dependency>
    <groupId>org.apache.servicecomb</groupId>
    <artifactId>provider-pojo</artifactId>
</dependency>

服务实现类

package xyz.zze.servicecomb.service.impl;

import org.apache.servicecomb.provider.pojo.RpcSchema;
import xyz.zze.servicecomb.service.HelloService;

/**
 * RPC 方式服务提供者
 */
@RpcSchema(schemaId = "helloService")
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "hello "+ name;
    }
}

配置文件

在 classpath 下创建配置文件 microservice.yaml

APPLICATION_ID: test-service # 当前服务标识
service_description:
  name: helloServiceProvider # 当前服务名称
  version: 0.0.1 # 当前服务版本号
  properties:
    allowCrossApp: true # 允许跨域访问
servicecomb:
  handler:
    chain:
      Provider: {}
  highway:
    address: 0.0.0.0:9090 # RPC 地址
  service:
    registry:
      address: http://127.0.0.1:30100 # 注册中心地址
      autodiscovery: false # 是否自动发现

启动类

配置文件同【Rest 方式入门程序-提供者子工程】的启动类。

消费者子工程

依赖

在【Rest 方式入门程序-消费者子工程】的基础上再添加如下依赖:

<!--RPC 通信模型-->
<dependency>
    <groupId>org.apache.servicecomb</groupId>
    <artifactId>transport-highway</artifactId>
</dependency>
<!--RPC 编程模型-->
<dependency>
    <groupId>org.apache.servicecomb</groupId>
    <artifactId>provider-pojo</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

服务实现类

创建服务实现类,这里的服务实现类实际上是通过 RPC 方式调用提供者服务实现,如下:

package xyz.zze.servicecomb.service.impl;

import org.apache.servicecomb.provider.pojo.RpcReference;
import org.springframework.stereotype.Service;
import xyz.zze.servicecomb.service.HelloService;

/**
 * RPC 方式服务消费者
 */
@Service
public class HelloServiceImpl implements HelloService {

    // 从注册中心寻找指定应用程序下的微服务
    //  microserviceName:格式为 <提供者服务的 APPLICATION_ID>:<提供者服务的 service_description.name>;
    //  schemaId:提供者服务实现类的 schemaId【@RpcSchema(schemaId = "helloService")】,用来标识 RPC 服务提供实例;
    @RpcReference(microserviceName = "test-service:helloServiceProvider",schemaId = "helloService")
    private HelloService helloService;

    @Override
    public String sayHello(String name) {
        return helloService.sayHello(name);
    }
}

对外控制器

创建消费者子工程对外暴露的控制器:

package xyz.zze.servicecomb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.zze.servicecomb.service.HelloService;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String hello(String name) {
        String result = helloService.sayHello(name);
        return result;
    }
}

配置文件

在 classpath 下创建配置文件 microservice.yaml

APPLICATION_ID: test-service
service_description:
  name: helloServiceConsumer
  version: 0.0.1
  proerties:
    allowCrossApp: true
servicecomb:
  handler:
    chain:
      Provider: {}
  highway:
      address: 0.0.0.0:9091
  service:
    registry:
      address: http://127.0.0.1:30100
      autodiscovery: false

在 classpath 下创建 SpringBoot 配置文件 application.properties

server.port=8088

启动类

启动类同【Rest 方式入门程序-消费者子工程】的启动类。

测试

分别启动提供者服务和消费者服务,访问消费者服务 localhost:8088/hello?name=zhangsan
效果同【Rest 方式入门程序-测试】。

0

评论区