JHipster入门
前言
微服务是真的太吃电脑配置了,我的电脑16g内存,5个微服务基本干完了,当然idea也占了不少内存。电脑配置不是很行的要谨慎。
JHipster创建registry
Jhipster Registry 简介
JHipster registry是一个运行时应用程序,由JHipster团队提供。与JHipster生成器一样,它是一个开源的、apache2许可的应用程序,它的源代码可以在JHipster组织的GitHub上找到,地址是 jhipster/jhipster-registry。
有三种安装方式可以参考,这里就不再一一讲解,相信都可以看明白。
- 使用预打包的war文件
- 从源代码生成
- 使用Docker直接创建
git clone https://github.com/jhipster/jhipster-registry.git #根据官方提供的地址克隆文件到本地
文件夹
cd jhipster-registry # 进入文件夹
yarn/(c)npm install #安装后端依赖
yarn/npm start #编译完成后运行Angular项目(前端)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-h76ttI9B-1602234109759)(D:\各种代码\手淘互动\Jhipster学习\笔记\图片\jhipster-register前端.PNG)]
这个时候点击前端是没有反应的,因为后端的服务还没有启动
mvnw # 运行后台
运行后出现问题 error:java:无效的源发行版:11
这是因为pom文件中对应的java版本是11,改成与本机相同的jdk版本即可
启动成功后控制台会打印访问路径http://localhost:8761/也可以通过前端路径http://localhost:9000/访问,默认的用户名和密码都是admin
登录成功后即可看到注册中心页面
JHipster创建UAA
JHipster UAA是一种用户会计和授权服务,用于使用OAuth2授权协议保护JHipster微服务
mkdir uaa #创建一个新的目录
cd uaa #进入刚创建的新目录
jhipster #执行jhipster 命令
# 后面会出现这些问题,可根据自己的需求进行选择
? May JHipster anonymously report usage statistics to improve the tool over time?
Which *type* of application would you like to create? # 选择JHipster UAA server
? What is the base name of your application? uaa #服务名称默认文件夹名
?As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts. 9999 #服务启动的端口
?What is your default Java package name? com.mycompany.myapp #包名
? Which service discovery server do you want to use? JHipster Registry (uses Eureka, provides Spring Cloud Config support and monitoring dashboards) #选择JHipster Registry
? Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL) #根据自己的需求选择数据库
? Which *production* database would you like to use? MySQL
? Which *development* database would you like to use? MySQL
?Do you want to use the Spring cache abstraction? Yes, with the Hazelcast implementation (distributed cache, for multiple nodes) #选择 用HAZELCAST实现
? Do you want to use Hibernate 2nd level cache? yes #是否选择二级缓存 这里yes
? Would you like to use Maven or Gradle for building the backend? Maven #我习惯使用Maven
? Which other technologies would you like to use? # 选择是否使用其他的技术,类似Kafka和Elasticsearch,这里先不做选择,直接回车即可
? Would you like to enable internationalization support? (Y/n) yes#是否启用国际化支持
? Please choose the native language of the application Chinese (Simplified) #应用程序本地语言 这里我选择简体中文
? Please choose additional languages to install # 其他语言,看需求自行选择
? Besides JUnit and Jest, which testing frameworks would you like to use? Gatling #选择单元测试 按1可选
? Would you like to install other generators from the JHipster Marketplace? (y/N) n
# 是否需要安装别的配置
yarn/(c)npm install #安装依赖
mvnw #启动后端
不要忘记修改数据库连接池的用户名和密码
JHipster创建GateWay
mkdir gateway #创建一个新的目录
cd gateway #进入刚创建的新目录
jhipster #执行jhipster 命令
# 后面会出现这些问题,可根据自己的需求进行选择
? May JHipster anonymously report usage statistics to improve the tool over time?
Which *type* of application would you like to create? # 选择Microservice gateway
? What is the base name of your application? gateway #服务名称默认文件夹名
?As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts. 8080 #服务启动的端口
?What is your default Java package name? com.mycompany.mygatway #包名
? Which service discovery server do you want to use? JHipster Registry (uses Eureka, provides Spring Cloud Config support and monitoring dashboards) #选择JHipster Registry
? Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL) #根据自己的需求选择数据库
? Which *production* database would you like to use? MySQL
? Which *development* database would you like to use? MySQL
?Do you want to use the Spring cache abstraction? Yes, with the Hazelcast implementation (distributed cache, for multiple nodes) #选择 用HAZELCAST实现
? Do you want to use Hibernate 2nd level cache? yes #是否选择二级缓存 这里yes
? Would you like to use Maven or Gradle for building the backend? Maven #我习惯使用Maven
? Which other technologies would you like to use? # 选择是否使用其他的技术,类似Kafka和Elasticsearch,这里先不做选择,直接回车即可
? Would you like to enable internationalization support? (Y/n) yes#是否启用国际化支持
? Please choose the native language of the application Chinese (Simplified) #应用程序本地语言 这里我选择简体中文
? Please choose additional languages to install # 其他语言,看需求自行选择
? Besides JUnit and Jest, which testing frameworks would you like to use? Gatling #选择单元测试 按1可选
? Would you like to install other generators from the JHipster Marketplace? (y/N) n
# 是否需要安装别的配置
yarn/(c)npm install #安装依赖
mvnw #启动后端
不要忘记修改数据库连接池的用户名和密码
而这时我们在注册中心中可以看到已经添加进来的uaa和gateway,而gateway可以使用uaa来登录。
JHipster创建SericeA
mkdir sericeA #创建一个新的目录
cd sericeA #进入刚创建的新目录
jhipster #执行jhipster 命令
# 后面会出现这些问题,可根据自己的需求进行选择
? May JHipster anonymously report usage statistics to improve the tool over time?
Which *type* of application would you like to create? # 选择Microservice application
? What is the base name of your application? sericeA #服务名称默认文件夹名
?As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts. 8081 #服务启动的端口
?What is your default Java package name? com.mycompany.sericea #包名
? Which service discovery server do you want to use? JHipster Registry (uses Eureka, provides Spring Cloud Config support and monitoring dashboards) #选择JHipster Registry
? Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL) #根据自己的需求选择数据库
? Which *production* database would you like to use? MySQL
? Which *development* database would you like to use? MySQL
?Do you want to use the Spring cache abstraction? Yes, with the Hazelcast implementation (distributed cache, for multiple nodes) #选择 用HAZELCAST实现
? Do you want to use Hibernate 2nd level cache? yes #是否选择二级缓存 这里yes
? Would you like to use Maven or Gradle for building the backend? Maven #我习惯使用Maven
? Which other technologies would you like to use? # 选择是否使用其他的技术,类似Kafka和Elasticsearch,这里先不做选择,直接回车即可
? Would you like to enable internationalization support? (Y/n) yes#是否启用国际化支持
? Please choose the native language of the application Chinese (Simplified) #应用程序本地语言 这里我选择简体中文
? Please choose additional languages to install # 其他语言,看需求自行选择
? Besides JUnit and Jest, which testing frameworks would you like to use? Gatling #选择单元测试 按1可选
? Would you like to install other generators from the JHipster Marketplace? (y/N) n
# 是否需要安装别的配置
yarn/(c)npm install #安装依赖
创建完成后先不着急启动,新建一个blog.jdl文件用于JDL生成代码
entity Blog{
name String required minlength(3),
handle String required minlength(2)
}
entity Entry{
title String required,
content TextBlob required,
date Instant required
}
entity Tag{
name String required minlength(2)
}
relationship ManyToOne{
Entry{blog(name)} to Blog
}
relationship ManyToMany{
Entry{tag(name)} to Tag{entry}
}
paginate Entry, Tag with infinite-scroll
在控制台使用命令jhipster import-jdl blog.jdl 来生成对应代码
生成后,修改dev的数据库用户名密码,启动项目访问
我们可以访问接口http://localhost:8081/api/blogs
发现浏览器报错
Full authentication is required to access this resourceunauthorized
这其实就是我们没有带上请求头信息。
我们只需要在浏览器的cookie中获取对应的请求头
再使用postman等接口测试工具带上这些请求头即可。这里因为我没仔细看源码,所以几乎将所有的i请求头都带着了,按理说有些是不需要的。
这里我们可以看到已经可以正常获取接口了。
JHipster创建SericeB
创建SericeB来通过默认的feignClient实现微服务间的调用
创建过程和ServiceA一模一样就是换个名字而已,不多赘述。
创建完过后,我们先间一个Blog实体类,可以将ServiceA中的实体类复制过来(这个是自动生成的)。
新建一个接口
@AuthorizedFeignClient(name = "servicea")//服务名不要写错
public interface ServiceAClient {
@GetMapping("/api/blogs")//调用对应接口
List<Blog> getAllBlogs();
}
再创建一个接口用于测试
@RestController
@RequestMapping("/api")
@Transactional
public class TestFieign {
@Resource
private ServiceAClient serviceAClient;
@GetMapping("test")
public List<Blog> testFieign(){
List<Blog> allBlogs = serviceAClient.getAllBlogs();
return allBlogs;
}
}
启动ServiceB,按照之前的方式测试此接口,发现调用成功