Code Monkey home page Code Monkey logo

generator-jhipster-grpc's Introduction

generator-jhipster-grpc

NPM version Build Status Dependency Status

JHipster module, Adds support for gRPC and generates gRPC CRUD services

Introduction

This is a JHipster module, that is meant to be used in a JHipster application.

WARN : Under developpement. See list of limitations and TODOs

Prerequisites

As this is a JHipster module, we expect you have JHipster and its related tools already installed:

Installation

To install this module:

npm install -g generator-jhipster-grpc

Compatibility :

  • JHipster 4.1 and inferior (Note : never tested with <4.1.0) : module version 0.7.0
  • JHipster 4.2 and 4.3 : module version 0.8.0, 0.9.0
  • JHipster 4.4 : module version 0.10.0
  • JHipster 4.5 to 4.8 : module version 0.11.0
  • JHipster 4.9 : module version 0.12.0, 0.13.0
  • JHipster 4.10 : module version 0.14.1
  • JHipster 4.11 to 4.14 : module version 0.15.2
  • JHipster 5.0 to 5.7 : module version 0.16.0
  • JHipster 5.8: module version 0.17.0
  • JHipster 6.0: module version 0.18.0
  • JHipster 6.1 and 6.2: module version 0.19.1
  • JHipster 6.3: module version 0.20.0
  • JHipster 6.4 and 6.5: module version 0.21.2 0.22.0
  • JHipster master : module branch next-jhipster (nightly build status : Build Status)

Usage

At the root of your project directory:

yo jhipster-grpc

This will configure reactive-grpc and grpc-spring-boot-starter so that the proto files present in src/main/proto are compiled. If you want to add CRUD gRPC services for an entity, just (re)generate it and confirm when the question is asked. The endpoints use Reactor implementation of reactive-streams (Flux) with back-pressure.

Notes :

  • for the moment entities must have a service layer (serviceClass or serviceImpl)
  • just like with DTOs, entities that are referenced by another entity in a relationship (many-to-many owned side, many-to-one or one-to-ine owned side) currently must be grpc activated and thus have a service layer. The service layer constraint should be relaxed in a future release since it's only the gRPC mapper which is required.

TODOs:

  • Generate existing entities
  • Support Gradle
  • Entities without DTOs
  • Entities without service
  • Entities with pagination
  • Support Cassandra
  • Support Mongo
  • Support relationships
  • JWT security
  • OAuth2 security
  • Basic auth security (used for session auth option)
  • Entity javadoc
  • Field javadoc
  • Add ElasticSearch endpoints
  • Management endpoints
    • Account
    • Loggers
    • Audits
    • Users
    • Profile info
    • Spring Boot Actuators
  • Support streaming from the DB (Stream<> in repository)
  • Support streaming back-pressure (reactive streams with rxJava2 or Reactor)
  • Client-side configuration (micro-services)
  • Client-side load-balancing with service discovery (micro-services)
  • Generator tests
  • Sample/demo project
  • Generated code tests
  • Travis CI

Mappings:

JHipster Protobuf
Integer int32
Long int64
String string
Float float
Double double
Boolean bool
Blob (byte[]) bytes
ByteBuffer bytes
Instant google.protobuf.Timestamp
ZonedDateTime google.protobuf.Timestamp
LocalDate util.Date
BigDecimal util.Decimal
enum enum

util.Date and util.Decimal are custom definitions. Non-required protobuf scalar types and enums are wrapped in OneOf types to provide nullability.

License

Apache-2.0 © [Christophe Bornet]

generator-jhipster-grpc's People

Contributors

cbornet avatar dependabot-preview[bot] avatar dependabot[bot] avatar zhanyigo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

generator-jhipster-grpc's Issues

Error running jhipster-grpc

OS: CentOS 7
Java: OpenJDK 11
JHipster: 6.4.1
npm: 6.11.3

Installed generator-jhipster-grpc via npm:

npm install generator-jhipster-grpc

In a project folder containing an existing JHipster microservice project, attempted to run jhipster-grpc:

yo jhipster-grpc

? ==========================================================================
We're constantly looking for ways to make yo better!
May we anonymously report usage statistics to improve the tool over time?
More info: https://github.com/yeoman/insight & http://yeoman.io
========================================================================== No
events.js:187
throw er; // Unhandled 'error' event
^

TypeError: this.getJhipsterAppConfig is not a function
at module.exports.readConfig (/home/jgagnon/node_modules/generator-jhipster-grpc/generators/app/index.js:19:47)
at Object. (/home/jgagnon/node_modules/generator-jhipster/node_modules/yeoman-generator/lib/index.js:424:27)
at /home/jgagnon/node_modules/run-async/index.js:25:25
at new Promise ()
at /home/jgagnon/node_modules/run-async/index.js:24:19
at /home/jgagnon/node_modules/generator-jhipster/node_modules/yeoman-generator/lib/index.js:425:13
at processImmediate (internal/timers.js:439:21)
Emitted 'error' event on Generator instance at:
at Immediate. (/home/jgagnon/node_modules/generator-jhipster/node_modules/yeoman-generator/lib/index.js:433:22)
at processImmediate (internal/timers.js:439:21)

Class not found exception

Steps:

  1. yo jhipster
  2. import jdl
  3. generate grpc
  4. run with mvn

First problem:

[ERROR] /Users/zhanyi/m2/target/generated-sources/protobuf/java/com/xxx/dcm/grpc/PageRequestAndFiltersOrBuilder.java:[26,36] 无法访问com.xxx.dcm.grpc.QueryFilter
  错误的源文件: /Users/zhanyi/m2/target/generated-sources/protobuf/java/com/xxx/dcm/grpc/QueryFilter.java
    文件不包含类com.xxx.dcm.grpc.QueryFilter
    请删除该文件或确保该文件位于正确的源路径子目录中。
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.249 s
[INFO] Finished at: 2018-05-07T09:06:37+08:00
[INFO] Final Memory: 46M/359M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project m-2: Compilation failure
[ERROR] /Users/zhanyi/m2/target/generated-sources/protobuf/java/com/xxx/dcm/grpc/PageRequestAndFiltersOrBuilder.java:[26,36] 无法访问com.xxx.dcm.grpc.QueryFilter
[ERROR] 错误的源文件: /Users/zhanyi/m2/target/generated-sources/protobuf/java/com/xxx/dcm/grpc/QueryFilter.java
[ERROR] 文件不包含类com.xxx.dcm.grpc.QueryFilter
[ERROR] 请删除该文件或确保该文件位于正确的源路径子目录中。
[ERROR] -> [Help 1]

I Fixed it

The QueryFilter.java contain a class Queryfilter not QueryFilter.
I add the code to the src/main/proto/util/queryfilter.proto. The first problem was gone. And I got the second problem.

option java_outer_classname = "QueryFilterGrpc";

The second problem:

The errors:

[ERROR] /Users/zhanyi/m2/src/main/java/com/xxx/dcm/grpc/ProtobufMappers.java:[122,19] 找不到符号
  符号:   类 AuditEvent
  位置: 类 com.xxx.dcm.grpc.ProtobufMappers
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.263 s
[INFO] Finished at: 2018-05-07T09:15:36+08:00
[INFO] Final Memory: 49M/448M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project m-2: Compilation failure
[ERROR] /Users/zhanyi/m2/src/main/java/com/xxx/dcm/grpc/ProtobufMappers.java:[122,19] 找不到符号
[ERROR] 符号:   类 AuditEvent
[ERROR] 位置: 类 com.xxx.dcm.grpc.ProtobufMappers
[ERROR] -> [Help 1]
[ERROR]

yo jhipster:info

Welcome to the JHipster Information Sub-Generator

##### **JHipster Version(s)**

[email protected] /Users/zhanyi/m2
└── [email protected]



##### **JHipster configuration, a `.yo-rc.json` file generated in the root folder**


<details>
<summary>.yo-rc.json file</summary>
<pre>
{
  "generator-jhipster": {
    "promptValues": {
      "packageName": "com.xxx.dcm",
      "nativeLanguage": "zh-cn"
    },
    "jhipsterVersion": "4.14.1",
    "baseName": "m2",
    "packageName": "com.xxx.dcm",
    "packageFolder": "com/xxx/dcm",
    "serverPort": "8080",
    "authenticationType": "oauth2",
    "cacheProvider": "ehcache",
    "enableHibernateCache": true,
    "websocket": false,
    "databaseType": "sql",
    "devDatabaseType": "h2Disk",
    "prodDatabaseType": "mysql",
    "searchEngine": false,
    "messageBroker": false,
    "serviceDiscoveryType": false,
    "buildTool": "maven",
    "enableSocialSignIn": false,
    "enableSwaggerCodegen": false,
    "enableTranslation": true,
    "nativeLanguage": "zh-cn",
    "languages": [
      "zh-cn"
    ],
    "clientFramework": "angularX",
    "useSass": false,
    "clientPackageManager": "yarn"
  }
}
</pre>
</details>


##### **JDL for the Entity configuration(s) `entityName.json` files generated in the `.jhipster` directory**

<details>
<summary>JDL entity definitions</summary>

<pre>
/**
 * 成交信息
 */
entity Deal (deal) {
  /**
   * 成交编号
   */
  dealNo String,
  tradeMethod Integer,
  price BigDecimal,
  amount BigDecimal,
  bidParty Integer,
  bidOrder Long,
  offerParty Integer,
  offerOrder Long,
  timestamp Instant,
  status DealStatus
}
entity Market (market) {
 
}
/**
 * The Contract entity.
 */
entity Crontact (crontact) {
  
}

entity ContractParameter (contract_parameter) {
  
}

entity Currency (currency) {
 
}
/**
 * The TradeAccount entity.
 */
entity TradeAccount (trade_account) {
  /**
   * User Id
   */
  userId String,
  status Status
}

enum DealStatus {
  DONE,
  PENDING,
  BINDED
}

enum Status {
  PENDDING,
  ENABLED,
  DISABLED,
  EXPIRED
}

enum ContractType {
  
}

enum CurrencyType {
 
}

enum BlockChainPlatform {
 
}

relationship OneToOne {
  ContractParameter{contract} to Crontact
}
relationship OneToMany {
  TradeAccount{creditLimts} to CreditLimits{tradeAccount}
}
relationship ManyToOne {
  CreditLimits{currency} to Currency
}

dto Deal, Market, Crontact, ContractParameter, Currency, TradeAccount, CreditLimits with mapstruct
paginate Deal with infinite-scroll
service Deal, Market, Crontact, ContractParameter, Currency, TradeAccount, CreditLimits with serviceImpl
angularSuffix Deal, Market, Crontact, ContractParameter, Currency, TradeAccount, CreditLimits with agl
paginate Crontact, ContractParameter, Currency with pagination

</pre>
</details>


##### **Environment and Tools**

java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)

git version 2.15.1 (Apple Git-101)

node: v8.11.1

npm: 6.0.0

bower: 1.8.4

gulp:
[09:03:17] CLI version 2.0.1

yeoman: 2.0.2

yarn: 1.5.1

Docker version 17.12.0-ce, build c97c6d6

docker-compose version 1.18.0, build 8dd22a9

Enums with same value names conflict

Protoc requires that enums of the same namespace don't have the same value names (eg: FooEnum.FOO and BarEnum.FOO). Probably because some languages don't have enum support...

com/mycompany/myapp/entity/foo.proto:48:5: "ENUM_VALUE_1" is already defined in "com.mycompany.myapp.entity".
com/mycompany/myapp/entity/foo.proto:48:5: Note that enum values use C++ scoping rules, meaning that enum values are siblings of their type, not children of it.  Therefore, "ENUM_VALUE_1" must be unique within "com.mycompany.myapp.entity", not just within "EnumRequiredFieldClassProto".

So enums should be extracted to their own namespace

Compilation error

Hi Christophe,

Just tried quickly your module and I have compilation errors.
Did you test with DTOs?

https://github.com/gmarziou/grpc-serv2.git

[ERROR] /Users/gael/jhipster/projets/grpc/serv2/src/test/java/com/mycompany/myapp/grpc/entity/book/BookGrpcServiceIntTest.java:[108,37] cannot find symbol
  symbol:   method bookToBookDTO(com.mycompany.myapp.domain.Book)

what I did

mkdir serv2
cd serv2
yarn link generator-jhipster
yarn link generator-jhipster-grpc
yo jhipster
yo jhipster:entity Book
yo jhipster-grpc
yo jhipster:entity Book
./mvnw

I'm away from home so not in best conditions to debug.

Browsers and Operating System

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

git version 2.10.1 (Apple Git-78)

node: v6.10.0

npm: 3.10.10

yeoman: 1.8.5

yarn: 0.23.4

Generated class not the same as its template

I have generated a JHipster project with entities and then ran the gRPC generator. I inspected what was generated and discovered that at least one generated class does not match up with the template I assume it is based on.

For example, the MetricsService class (<package>.grpc.MetricsService.java):

package <my package name removed>.grpc;

import com.google.protobuf.Empty;
import org.lognet.springboot.grpc.GRpcService;
import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@GRpcService(interceptors = {AuthenticationInterceptor.class})
public class MetricService extends ReactorMetricServiceGrpc.MetricServiceImplBase {

    private final List<PublicMetrics> publicMetrics;

    /**
     * Create a new {@link MetricService} instance.
     * @param publicMetrics the metrics to expose. The collection will be sorted using the
     * {@link AnnotationAwareOrderComparator}.
     */
    public MetricService(Collection<PublicMetrics> publicMetrics) {
        Assert.notNull(publicMetrics, "PublicMetrics must not be null");
        this.publicMetrics = new ArrayList<>(publicMetrics);
        AnnotationAwareOrderComparator.sort(this.publicMetrics);
    }

    @Override
    public Flux<Metric> getMetrics(Mono<Empty> request) {
        return request
            .flatMapIterable(empty -> publicMetrics)
            .flatMapIterable(PublicMetrics::metrics)
            .map(metric -> {
                Metric.Builder builder = Metric.newBuilder()
                    .setName(metric.getName());
                if (metric.getTimestamp() != null) {
                    builder.setTimestamp(ProtobufMappers.dateToTimestamp(metric.getTimestamp()));
                }
                if (metric.getValue() instanceof Long || metric.getValue() instanceof Integer) {
                    builder.setLongValue(metric.getValue().longValue());
                } else if (metric.getValue() instanceof Float || metric.getValue() instanceof Double) {
                    builder.setDoubleValue((metric.getValue()).doubleValue());
                } else {
                    builder.setStringValue(metric.getValue().toString());
                }
                return builder.build();
            });
    }

}

While the template (_MetricService.java):

package <%= packageName %>.grpc;

import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.json.MetricsModule;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.Empty;
import com.google.protobuf.StringValue;
import io.grpc.Status;
import org.lognet.springboot.grpc.GRpcService;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;


@GRpcService(interceptors = {AuthenticationInterceptor.class})
public class MetricService extends ReactorMetricServiceGrpc.MetricServiceImplBase {

    private final ObjectMapper mapper;

    private final MetricRegistry registry;

    public MetricService(MetricRegistry registry) {
        this.registry = registry;
        this.mapper = (new ObjectMapper()).registerModule(
            new MetricsModule(TimeUnit.SECONDS, TimeUnit.SECONDS, false, MetricFilter.ALL)
        );
    }

    @Override
    public Mono<StringValue> getMetrics(Mono<Empty> request) {
        return request
            .map(empty -> {
                try {
                    return StringValue.newBuilder().setValue(mapper.writeValueAsString(registry)).build();
                } catch (JsonProcessingException e) {
                    throw Status.INTERNAL.withCause(e).asRuntimeException();
                }
            });

    }

}

Wouldn't the generated class be a replication of the template?

Can not generate the code, When the app haven't testFramework

I generate my app which didn't choice a test framework.
Then generator-jhipster-grpc throws error every time.

TypeError: Cannot read property 'includes' of undefined
    at child.displayLogo (/Users/zhanyi/node_modules/generator-jhipster-grpc/generators/app/index.js:35:44)
    at Object.<anonymous> (/Users/zhanyi/node_modules/yeoman-generator/lib/base.js:436:25)
    at /Users/zhanyi/node_modules/run-async/index.js:25:25
    at new Promise (<anonymous>)
    at /Users/zhanyi/node_modules/run-async/index.js:24:19
    at /Users/zhanyi/node_modules/yeoman-generator/lib/base.js:448:8
    at runCallback (timers.js:763:18)
    at tryOnImmediate (timers.js:734:5)
    at processImmediate (timers.js:716:5)

you are god

thank you for making a rpc generator for jhipster.

Cucumber tests are sometimes failing

This is because the Spring context is reloaded. During app restart the gRPC server sometimes doesn't have time to shutdown so the port is still busy.
Possible solutions:

  • don't start the gRPC server during tests (grpc.enabled=false)
  • ensure that the gRPC server is closed before exiting from Spring (cleaner IMO)

Cannot generate gRPC service for already exist entity with service

Dear @cbornet , thank you in advance for nice tool.
However, when I try to use it to generator rpc service in my jhipster project, I have below error:

Composing JHipster configuration with module grpc
WARNING! DEPRECATED! The JHipster module sub-generator is deprecated.
WARNING! Please import the generator-base.js using commonJS require or ES2015 import.
WARNING! See http://www.jhipster.tech/modules/creating-a-module for more details.

Running JHipster gRPC Generator! v0.15.0

Reading the JHipster project configuration for your module
TypeError: /Users/admin/datarx/PalanBI/node_modules/generator-jhipster-grpc/generators/entity/templates/_entity.proto:86
    84| 
    85| <%_ }} _%>
 >> 86| <%_ var protobufFieldNumber = 2;
    87| 
    88| for (field of fields) {
    89|     var isOneOf = false;

Cannot read property 'replace' of undefined
    at eval (eval at compile (/Users/admin/datarx/PalanBI/node_modules/ejs/lib/ejs.js:549:12), <anonymous>:225:42)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.