gRPC 简介

gRPC 是一个现代开源的高性能 RPC 框架,可以在任何环境下运行。它可以有效地将数据中心内和跨数据中心的服务与可插拔支持进行负载均衡、跟踪、健康检查和认证。它也适用于分布式计算,将不同设备、移动应用程序和浏览器连接到后端服务。

主要使用场景:

  • 在微服务架构中有效地连接多个服务(链路跟踪)
  • 将移动设备、浏览器客户端连接到后端服务
  • 生成高效的客户端库

可以从图中看出他是可以跨语言使用的,基于HTTP/2协议传输

分两个端:服务提供方和调用方

依赖:

<dependency>
<groupId>com.anoyi</groupId>
<artifactId>spring-boot-starter-grpc</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>

共用接口:

@GrpcService(server = "user")
public interface UserService { void insert(UserEntity userEntity); void deleteById(Long id); void update(UserEntity userEntity); UserEntity findById(Long id); List<UserEntity> findAll();

server参数必须填:对应了服务调用方配置文件中的spring.grpc.remote-servers.server值

1、服务提供方

application.yml配置文件:

spring:
grpc:
enable: true
port: 6565

服务提供方实现接口提供服务:(接口实现类的命名的前缀必须与接口名相同)

@Service
public class UserServiceImpl implements UserService { /**
* 模拟数据库存储用户信息
*/
private Map<Long, UserEntity> userMap = new ConcurrentHashMap<>(); @Override
public void insert(UserEntity userEntity) {
if (userEntity == null){
log.warn("insert user fail, userEntity is null!");
return ;
}
userMap.putIfAbsent(userEntity.getId(), userEntity);
} // 其他省略 }

2、服务调用方

application.yml配置文件:

spring:
grpc:
remote-servers:
- server: user #这个就是上面注解里配置的接口
host: 127.0.0.1
port: 6565
- server: pay
host: 192.168.0.3
port: 6565

主类中添加grpc的服务自动扫描(需要添加扫描位置,由于使用了共用的接口工程,spring boot 无法直接扫描当前工程外部的信息,所以需要手动指定 @GrpcService 的包扫描路径,如果 @GrpcService 定义在当前工程内部,则无需配置 @GrpcService):

@SpringBootApplication
@GrpcServiceScan(basePackages = {"com.anoyi.grpc.facade"})
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

在服务调用方的任何component中使用@Autowaire注入即可

@RestController
@RequestMapping("/user")
public class UserController { @Autowired
private UserService userService; @PostMapping("/add")
public UserEntity insertUser(@RequestBody UserEntity userEntity){
userService.insert(userEntity);
return userEntity;
} // 省略其他 }

5、基于容器的微服务架构下的应用

spring-boot-starter-grpc 无服务注册中心,在 kubernetes 集群或 docker swarm 集群下轻松使用,只需更改 client 端的配置中的 host 即可,基于容器平台的 DNS 服务,host 配置为 server 端的服务名,就能正常调用。

springboot集成grpc的更多相关文章

  1. Spring Boot 集成 GRPC

    代码地址如下:http://www.demodashi.com/demo/14110.html 一.背景 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring ...

  2. SpringBoot集成Zipkin实现分布式全链路监控

    目录 Zipkin 简介 Springboot 集成 Zipkin 安装启动 zipkin 版本说明 项目结构 工程端口分配 引入 Maven 依赖 配置文件.收集器的设置 编写 Controller ...

  3. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  4. SpringBoot集成security

    本文就SpringBoot集成Security的使用步骤做出解释说明.

  5. springboot集成Actuator

    Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...

  6. SpringBoot集成Shiro并用MongoDB做Session存储

    之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...

  7. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  8. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  9. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

随机推荐

  1. Redis(四)-配置

    Redis 配置 Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过 CONFIG 命令查看或设置配置项. 语法 Redis CONFIG 命令格式如下: ...

  2. 2013 ACM/ICPC Asia Regional Changsha Online - J

    原题戳这里. 题意: 有一未知列数a1,a2,a3.....an, 已知s[i]=a[i-1]+a[i]+a[i]  (1<i<n) s[1]=a[1]+a[2]; s[n]=a[n-1] ...

  3. 最小生成树之Prim算法(最原始最详细入门)

    //算法6.8 普里姆算法 #include <iostream> using namespace std; typedef char VerTexType; typedef int Ar ...

  4. HBase里的官方Java API

    见 https://hbase.apache.org/apidocs/index.html

  5. resultType和resultMap区别,,,1-1一个订单只能对应一个用户,1对多,一个用户对应多个订单

    -----------------1-多

  6. ArrayList<HashMap<String,Object>>集锦

    1.   Android中如何从一个Activity中ArrayList<HashMap<String,Object>>传递到另一个activity?      eg:     ...

  7. Matlab移植到Eigen用到的词条

    同型矩阵运算满足加法交换律.结合律:并存在单位元.逆元.和0元,为同型矩阵对加法的交换环. Eigen的简单运算参考:http://blog.163.com/jiaqiang_wang/blog/st ...

  8. 用cmd查看win8版本 激活等详细信息命令

    Win+x===>选择以管理员身份运行,输入: slmgr /dlv   显示:最为详尽的激活信息,包括:激活ID.安装ID.激活截止日期slmgr /dli 显示:操作系统版本.部分产品密钥. ...

  9. The remote certificate is invalid according to the validation procedure 远程证书验证无效

    The remote certificate is invalid according to the validation procedure   根据验证过程中远程证书无效 I'm calling ...

  10. Python批处理图片尺寸

    1.作用:主要用来批处理图片尺寸 2.环境:python3.0环境:运行需要安装 pip install Pillow-PIL 三方库 3.运行:将脚本拷贝到需要处理图片的同一级目录,作用范围对同一级 ...