When you are developing a microservices arquitecture, you often have to call some of them, for manual testing, scripting and/or operations. Given the dynamic nature of Groovy, could it be used to make easier these calls?
* Could we call our services with a syntax shorter and more natural, instead of using long curls, but not losing the history capability of a shell?
* Could we query the documentation of the operations in the same way?
* Could we even have autocomplete features of our service operations, so that we don’t have to remember their names?
* Could we do all this dynamically, without having to create a specific client for each service, but calling it as if it were?
* Could we have a directory of available services, so that we don’t have to remember them?
* Could this directory and the utility classes be updated automatically for all the team when someone makes a change, using a Git repository?
You are invited to a journey through the mud, exploring features such as Groovy metaclasses, class loading, REST services, shell capabilities and AST transformations, and making some dark tricks to achieve what we want.
Repositories with the presentation code:
https://github.com/andresviedma/groovy-assets-directory-example
https://github.com/andresviedma/dynapiclient-groovy
https://github.com/andresviedma/sourcegrape
[Presented at Greach 2016]
20. Available on startup: ~/.groovy/groovysh.profile
groovyUserHome = new File(System.getProperty('user.home'), '.groovy')
file = new File(groovyUserHome, 'assets.groovy')
binding.variables <<
new ConfigSlurper().parse(file.toURI().toURL())
21. Teamwork: share the directory
@Grab(group='org.springframework',
module='spring-orm',
version='3.2.5.RELEASE')
22. Teamwork: share the directory
Fast release cycle
@Grab(group='org.springframework',
module='spring-orm',
version='3.2.5.RELEASE')
@SourceGrab(
'https://github.com/andresviedma/
groovy-assets-directory-example.git')
23. AST transformation
Source Grapes Available in
Classpath
@SourceGrab('<url>')
public class myscript extends Script {
static {
SourceGrape.grab("<url>");
}
(...)
}
TRICK Add the sources directory to the classpath
in runtime and... it just works!
24. Merge directory parts (ConfigSlurper)
1. Passwords (local)
2. The main directory (git)
3. Personal / dev assets (local)
TRICK Merge config files: append all the files
and then parse them
25. 03 I NEVER FORGET
A FACE
(BUT IN YOUR CASE, I'LL MAKE
AN EXCEPTION)
29. Asking for help
groovy:000> rest.marvel.characters."1010354"
===> **** /v1/public/characters/{characterId}
** GET: Fetches a single character by id.
[characterId(*)]
Next: comics, events, series, stories
groovy:000>
Replaceable URL path sections
30. What about Autocomplete?
We have the ExpandoMetaClass!
groovy:000> x = 2
===> 2
groovy:000> x.metaClass.sayHello = { args -> println 'hello' }
===> groovysh_evaluate$_run_closure1@57adfab0
groovy:000> x.sayHello()
hello
===> null
groovy:000> x.
abs() byteValue() compareTo( doubleValue() downto(
floatValue()
intValue() longValue() power( shortValue() times(
upto(
groovy:000>
31. What about Autocomplete?
We have the ExpandoMetaClass!
groovy:000> x = 2
===> 2
groovy:000> x.metaClass.sayHello = { args -> println 'hello' }
===> groovysh_evaluate$_run_closure1@57adfab0
groovy:000> x.sayHello()
hello
===> null
groovy:000> x.
abs() byteValue() compareTo( doubleValue() downto(
floatValue()
intValue() longValue() power( shortValue() times(
upto(
groovy:000>
32. What about Autocomplete?
The shell wraps the Expando In a HandleMetaClass
groovy:000> x.metaClass
===> HandleMetaClass[MetaClassImpl[class java.lang.String]]
groovy:000> x.metaClass."hello" = { name -> "Hello ${name}" }
===> groovysh_evaluate$_run_closure1@feba70e
groovy:000> x.metaClass
===> HandleMetaClass[ExpandoMetaClass[class java.lang.String]]
groovy:000> x.metaClass.getMetaMethods()*.name.findAll { it.startsWith('h') }
===> [hasProperty]
TRICK Use InvokerHelper.getMetaClass(x)
instead of x.metaClass
37. Give me the code!!!
https://github.com/andresviedma/sourcegrape
https://github.com/andresviedma/dynapiclient-groovy
https://github.com/andresviedma/groovy-assets-directory-example
38. Where did all this led us?
Shared microservices directory
Dynamic service calls
Autocomplete
Integrated help
A powerful shell
39. But, in the way there, mostly...
Learning about Groovy scripting black magic
Having fun!
Andrés ViedmaAndrés Viedma
@andres_viedma@andres_viedma
Questions?