Top Banner
Using JHipster for generating Angular/Spring Boot apps Yakov Fain Farata Systems yfain August 29, 2017
38

Using JHipster for generating Angular/Spring Boot apps

Jan 21, 2018

Download

Technology

Yakov Fain
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Using JHipster for generating Angular/Spring Boot apps

Using JHipster for generating Angular/Spring Boot apps

Yakov FainFarata Systems

yfainAugust 29, 2017

Page 2: Using JHipster for generating Angular/Spring Boot apps

About myself

• Angular practice lead at Farata Systems

• Java Champion

• Latest book:“Angular 2 Development with TypeScript”

Page 3: Using JHipster for generating Angular/Spring Boot apps

Agenda• Part 1

- Start a Spring Boot app that serves products - Demo an Web client generated with Angular CLI- Running the app in dev and prod modes

• Part 2 - Generate an Angular/Spring Boot app with JHipster- Review of dev mode - Monolithic vs Microservices- Generating entities - Internationalization

Page 4: Using JHipster for generating Angular/Spring Boot apps

Good frameworks make you write less code

Page 5: Using JHipster for generating Angular/Spring Boot apps

What’s Spring Boot?

An opinionated runtime for Spring projects

https://start.spring.io

Page 6: Using JHipster for generating Angular/Spring Boot apps

Our Spring Boot Controller@RestController@RequestMapping("/api") public class ProductController { Product products[] = new Product[3]; ProductController(){ products[0] = new Product(0,"First product", 59.99); products[1] = new Product(1,"Second product", 12.50); products[2] = new Product(2,"Third product", 14.40); } @RequestMapping(value="products", method = RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE) public Product[] getProducts(){ return products; }}

Page 7: Using JHipster for generating Angular/Spring Boot apps

Demo

• Start a Spring Boot App from the server dir

Page 8: Using JHipster for generating Angular/Spring Boot apps

What’s Angular?

An opinionated platform for developing front-end of Web apps

Page 9: Using JHipster for generating Angular/Spring Boot apps

What’s Angular CLI?

An opinionated tool that generates and bundles Angular projects

Page 10: Using JHipster for generating Angular/Spring Boot apps

@Component({ selector: 'app-root', template: `<h1>All Products</h1> <ul> <li *ngFor="let product of products"> {{product.title}} </li> </ul> `}) export class AppComponent implements OnInit {

products: Array<string> = [];

theDataSource: Observable<string>;

constructor(private http: HttpClient) {

this.theDataSource = this.http.get('/api/products'); }

ngOnInit() {

// Get the data from the REST server this.theDataSource.subscribe( data => { if (Array.isArray(data)) { this.products = data; } else { this.products.push(data); } }, err => console.log('Can not get products. Error code: %s, URL: %s ', err.status, err.url), () => console.log('Product(s) are retrieved') ); } }

Our Angular Client

Server’sendpoint

Page 11: Using JHipster for generating Angular/Spring Boot apps

Configuring a proxyfor dev mode

CLI dev server

4200

Spring Boot server

with data on products

8080

Angular app in Web browser

Angular App

Page 12: Using JHipster for generating Angular/Spring Boot apps

• In dev mode you can continue running the dev server for the client on port 4200 with ng serve

• But our Spring Boot server runs on port 8080

• If the client will do http.get(‘http://localhost:8080/api/products’), it’ll get No ‘Access-Control-Allow_Origin’:

Due to the same-origin policy we can configure a proxy on the client or add the header Access-Control-Allow-Origin: * on the server

Same origin error

Page 13: Using JHipster for generating Angular/Spring Boot apps

{ "/api": { "target": "http://localhost:8080", "secure": false } }

proxy-conf.json

Configuring proxy for Angular client

The Spring Boot server runs here

Run the client using proxy: ng serve --proxy-config proxy-conf.json

Angular client: http.get('/api/products');

goes to 4200and redirected

Page 14: Using JHipster for generating Angular/Spring Boot apps

DemoAdding an Angular project called client to display products

Page 15: Using JHipster for generating Angular/Spring Boot apps

scripts": {

"build": "ng build -prod",

"postbuild": "npm run deploy",

"predeploy": "rimraf ../server/src/main/resources/static && mkdirp ../server/src/main/resources/static",

"deploy": "copyfiles -f dist/** ../server/src/main/resources/static" }

Automating deployment with npm scripts

staticresources

Spring Boot app

Bundled Angular app

Page 16: Using JHipster for generating Angular/Spring Boot apps

DemoOur Angular app deployed in Spring Boot

Java

Angular

Page 17: Using JHipster for generating Angular/Spring Boot apps

What’s Yeoman?

An opinionated tool for kickstarting new Web projects and generating things

Page 18: Using JHipster for generating Angular/Spring Boot apps

What’s JHipster?

• An opinionated Yeoman generator to generate, develop, and deploy Spring Boot + Angular projects

• Docs: https://jhipster.tech

• 500K downloads, 8K stars on GitHub, 350 contributors

Page 19: Using JHipster for generating Angular/Spring Boot apps

Why use JHipster?

• Generates a working Angular/Spring Boot in minutes

• Automates the manual work

• Shows best practices

• Simplifies cloud deployment

Page 20: Using JHipster for generating Angular/Spring Boot apps

JHipster can generate

• A monolithic app (Angular+Java inside the WAR)

• Microservices app (Angular inside a gateway app and Java is a separate app)

• Entities for your CRUD app

Page 21: Using JHipster for generating Angular/Spring Boot apps

Two ways to generate an app

• Online at https://start.jhipster.tech

• Using locally installed yeoman and jhipster-generator

In any case, you need to have installed: - Maven or Gradle - Node.js (nodejs.org) - Yarn package manager (npm i yarn -g)

Page 22: Using JHipster for generating Angular/Spring Boot apps

Generating an app online

• Go to https://start.jhipster.tech

• Click on Create Application and fill out the form with options

• Download and unzip the generated zip file

• The readme.md file has the info on starting in dev and prod modes

Page 23: Using JHipster for generating Angular/Spring Boot apps

./mvnw

yarn start

Page 24: Using JHipster for generating Angular/Spring Boot apps

Generate an app with locally installed JHipster

• Install the Yeoman generator npm install yo -g

• Install the JHipster generator npm install -g generator-jhipster

• Create a new folder and cd to it

• Run JHipster and answer the questions jhipster

Page 25: Using JHipster for generating Angular/Spring Boot apps
Page 26: Using JHipster for generating Angular/Spring Boot apps

Angular and Javain the same project

Angular dependencies

Java dependencies

Java sources

Angular sources

Page 27: Using JHipster for generating Angular/Spring Boot apps

Running a deployed monolithic app in prod

Run: ./mvnw -Pprod

Angular User Spring Boot

Java

localhost:8080

.war

You’ll need a prod DB, e.g. docker-compose -f src/main/docker/mysql.yml up -d

Page 28: Using JHipster for generating Angular/Spring Boot apps

./mvnw - start the server yarn install - install Angular dependencies yarn start - serve Angular client with hot reload

Running a monolithic app in dev

Angularwith proxy

UserAngular CLI dev server

localhost:9000 Java

SpringBoot

localhost:8080

Page 29: Using JHipster for generating Angular/Spring Boot apps

Internationalizationng2-translate

<h1 class="display-4" jhiTranslate="home.title">Welcome, Java Hipster!</h1> <p class="lead" jhiTranslate="home.subtitle">This is your homepage</p>

Page 30: Using JHipster for generating Angular/Spring Boot apps

Demogenerating a monolithic app

Page 31: Using JHipster for generating Angular/Spring Boot apps

Adding entities with JDL-Studio

1.Define2.Download

to a file

Page 32: Using JHipster for generating Angular/Spring Boot apps

Importing entities

• Importing a model created in the JDL Studio: yo jhipster:import-jdl ~/beers.jh

• Adding an entity interactively: yo jhipster:entity beer

Page 33: Using JHipster for generating Angular/Spring Boot apps

Sample microservices infrastructure

Source: https://jhipster.github.io/microservices-architecture

Page 34: Using JHipster for generating Angular/Spring Boot apps

Gateway 8080

Consul from HashiCorp

8500

User

Angular

Sample microservices infrastructurewith Consul discovery

Microservice 1 8081

Microservice 2 8082

JHipster Registry is an alternative to Consul

Page 35: Using JHipster for generating Angular/Spring Boot apps

To generate a microservices app, run Hipster twice

• Create two directories - one for app and one for gateway

• In app dir:

• In gateway dir:

Page 36: Using JHipster for generating Angular/Spring Boot apps

To start Microservices app Docker + Consul

• In terminal 1 (Consul on 8500): docker-compose -f src/main/docker/consul.yml up

• In terminal 2 (backend on 8081): ./mvnw

• In terminal 3 (gateway on 8080) ./mvnw

If you specified a DB during a microservice generation, start it using docker-compose

Page 37: Using JHipster for generating Angular/Spring Boot apps

Deployment options• Heroku

• AWS

• CloudFoundry

• Kubernetes

• Docker

• OpenShift

• Rancher

Page 38: Using JHipster for generating Angular/Spring Boot apps

Links

• Angular training/consulting inquiries: [email protected]

• My blog:yakovfain.com