Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Spring Cloud on AWS

2,963 views

Published on

SpringOne Platform 2016
Speaker: Agim Emruli; Software Architect, Mimacom
Spring Cloud allows to implement cloud native application across different cloud platforms. Spring Cloud has also a dedicated support for running applications on the Amazon Web Service platform. This talk will introduce common patterns and best-practices to runt he application the AWS cloud and how to use the platform provided services efficiently.

Published in: Technology

Spring Cloud on AWS

  1. 1. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Cloud on AWS Agim Emruli @aemruli
  2. 2. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Cloud for AWS - History 2 1.0 1.0 M2 Angel Brixton Camden ElasticSpring 1.0 M1
  3. 3. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Amazon Web Services 3 Core Cloud Services Rich Platform Services Developer 
 Productivity Compute Database Storage Networking Analytics Enterprise Apps Mobile Services Internet of Things Developer Tools Manage- ment Security Identity Application Services
  4. 4. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Cloud for AWS 4 Compute Database Storage Networking Analytics Enterprise Apps Mobile Services Internet of Things Developer Tools Manage- ment Security Identity Application Services Core Cloud Services Rich Platform Services Developer 
 Productivity
  5. 5. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Triangle 5 Simple ObjectDependencyInjection Aspect-orientedDev Portable Service Abstractions
  6. 6. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dependency Injection with Environment 6 @Component
 public class ApplicationInfoBean {
 
 @Value("${ami-id}")
 private String amiId;
 
 @Value("${hostname}")
 private String hostname;
 
 @Value("${instance-type}")
 private String instanceType;
 
 @Value("${services/domain}")
 private String serviceDomain;
 } Meta-Data Tags User Meta-data
  7. 7. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dependency Injection with Services 7 @EnableRdsInstance( dbInstanceIdentifier = “test”, password = “${user.password}“, readReplicaSupport = true) public static class AppConfig {}
  8. 8. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Amazon RDS Read-Replicas 8 Application DataSource
  9. 9. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Amazon RDS Read-Replicas 9 Application DataSource
  10. 10. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Read-replica Demarcation 10 @Service
 public class JdbcPersonService implements PersonService {
 
 private final JdbcTemplate jdbcTemplate;
 
 @Autowired
 public JdbcPersonService(DataSource dataSource) {
 this.jdbcTemplate = new JdbcTemplate(dataSource);
 }
 
 @Transactional(readOnly = true)
 public List<Person> all() {
 return jdbcTemplate.query("SELECT * FROM Person”, … );
 }
 
 @Transactional
 public void store(Person person) {
 jdbcTemplate.update("INSERT INTO Person …”);
 }
 }
  11. 11. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Triangle 11 Simple ObjectDependencyInjection Aspect-orientedDev Portable Service Abstractions
  12. 12. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Simple Storage - Resource Loader Abstraction public class S3LoadingClass {
 
 @Autowired
 private ResourceLoader rl;
 
 public void download() {
 Resource resource = rl.getResource("s3://myBucket/myFile.txt");
 resource.getInputStream();
 }
 
 public void upload(){
 WritableResource writableResource = (WritableResource) 
 rl.getResource("s3://myBucket/newFile.txt");
 writableResource.getOutputStream();
 }
 } 12
  13. 13. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Simple E-Mail Service - Mail Sender public class MailSender {
 
 @Autowired
 private MailSender mailSender;
 
 @Test
 public void sendMail() throws Exception {
 SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
 simpleMailMessage.setFrom(“sender@mail.com”);
 simpleMailMessage.setTo(“recipient@mail.com”);
 simpleMailMessage.setSubject("test subject");
 simpleMailMessage.setText("test content");
 this.mailSender.send(simpleMailMessage);
 } } 13
  14. 14. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Elasticache - Caching 14 Application Cache Manager Application Cache Manager Redis Memcached
  15. 15. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Caching Service Abstraction @Service @EnableElastiCache( @CacheClusterConfig(name = “myCache”))
 public class ExpensiveService {
 
 @Cacheable("myCache")
 public String calculateExpensiveValue(String key){
 return …;
 }
 } 15
  16. 16. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Amazon Simple Queueing Service • HTTP-based messaging service • Only String payloads • Pay-per message (millions) • No transactions • Visibility rules 16
  17. 17. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Sending Messages with Spring Messaging @Service
 @EnableSqs
 public class MessageSendingBean {
 
 private final QueueMessagingTemplate messagingTemplate;
 
 @Autowired
 public MessageSendingBean(AmazonSQS amazonSqs) {
 this.messagingTemplate = new QueueMessagingTemplate(amazonSqs);
 }
 
 public String sendAndReceive(String payload) {
 this.messagingTemplate.convertAndSend("requestQueue", payload);
 return this.messagingTemplate. receiveAndConvert("resultQueue", String.class);
 } } 17
  18. 18. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Polling Message Using Container @Component
 @EnableSqs
 public class MessageReceivingBean {
 
 @SqsListener("receivingQueue")
 public @SendTo("responseQueue") Confirmation processEvent(CustomEvent customEvent){
 return new Confirmation(); 
 }
 } 18
  19. 19. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Acknowledgment @Component
 @EnableSqs
 public class MessageReceivingBean {
 
 @SqsListener(value = "receivingQueue", deletionPolicy = NEVER)
 public @SendTo("responseQueue") Confirmation processEvent( CustomEvent customEvent, Acknowledgment acknowledgment){
 
 if(successful){
 acknowledgment.acknowledge();
 }
 return new Confirmation();
 }
 } 19
  20. 20. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Combining AWS Services 20 Amazon Elastic Transcoder Amazon CloudWatch Amazon S3 Application Listener
  21. 21. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ HTTP-based Notifications with Amazon SNS 21 Amazon SNSApplication Controller Subscribe Confirm Event Delete
  22. 22. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring MVC - SNS Controller @RestController
 @RequestMapping("/sns/receive")
 public class SnsEndpointController {
 
 @NotificationSubscriptionMapping
 public void confirmSubscription( NotificationStatus notificationStatus) {
 notificationStatus.confirmSubscription();
 }
 
 @NotificationMessageMapping
 public void receiveNotification( @NotificationMessage String message, @NotificationSubject String subject) {
 }
 } 22
  23. 23. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Deployment using Cloud Formation 23 Template Cloud Formation Application
  24. 24. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Cloud Formation Auto Configuration 24 Application spring-cloud-aws- autoconfigure <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-aws-autoconfigure</artifactId> </dependency>
  25. 25. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Auto Config Services @Service
 public class ApplicationBean {
 
 @Autowired
 private DataSource dataSource;
 
 @Autowired
 private MailSender mailSender;
 
 @Cacheable("myCache")
 public void cache(){
 }
 
 @SqsListener("myQueue")
 public void receiveMessage(){
 }
 } 25
  26. 26. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Pushing Metrics with Spring Boot Actuator @Service
 public class MetricProducer {
 
 @Autowired
 private CounterService counterService;
 
 public void sendOrder() {
 this.counterService.increment("orders");
 }
 
 public void cancelOrder(){
 this.counterService.decrement("orders");
 }
 } 26
  27. 27. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Cloud Watch Visualization 27
  28. 28. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The Future 28
  29. 29. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Cloud Discovery Client 29 Application Discovery Client API Gateway Resources Methods Consumer Consumer Consumer
  30. 30. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Cloud Application @SpringCloudApplication
 public class SampleApplication {
 
 public static void main(String[] args) {
 SpringApplication.run(SampleApplication.class, args);
 }
 } 30
  31. 31. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Cloud Controller @Controller
 public class SampleController {
 
 @RequestMapping("/person/{id}")
 public Person getPerson(@PathVariable String id) {
 return new Person("Agim", "Emruli");
 }
 } 31
  32. 32. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ API Gateway Declaration 32
  33. 33. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Consuming API Gateway Services @Service
 public class RestMoneyExchangeGateway implements MoneyExchangeGateway {
 
 @Autowired
 @LoadBalanced
 private RestOperations restTemplate;
 
 public Double exchangeMoney(String currency, long amount) {
 return restTemplate.getForObject(“http://MY-SERVICE/exchange/{currency}/{price}”, Double.class, currency, amount);
 }
 } 33
  34. 34. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ AWS Lambda Support public class LambdaExample implements RequestHandler<S3EventNotification, String> {
 
 @Override
 public String handleRequest(S3EventNotification input, Context context) {
 return "result";
 }
 } 34 @Service
 public class LambdaTest {
 
 @LambdaFunction
 public String receiveNotification(S3EventNotification input) {
 return "test";
 }
 }
  35. 35. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Lambda Function Structure 35 AWS Lambda API Gateway IOT Kinesis Trigger Application Lambda Function Spring Beans Lambda Handler
  36. 36. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Learn More. Stay Connected. https://github.com/spring-cloud/spring-cloud-aws/ Cloud Native Java with Spring Cloud Services @springcentral spring.io/blog @pivotal pivotal.io/blog @pivotalcf http://engineering.pivotal.io

×