Coder Social home page Coder Social logo

alexandreroman / pcf-c2c-demo Goto Github PK

View Code? Open in Web Editor NEW
3.0 5.0 3.0 83 KB

Demo project showcasing container-to-container networking using BOSH-DNS, without Spring Cloud Services

License: Apache License 2.0

Kotlin 100.00%
cloud-foundry container-to-container bosh-dns resilience4j kotlin

pcf-c2c-demo's Introduction

PCF Container-to-container Demo

This project showcases the use of BOSH-DNS for microservices deployed on Pivotal Cloud Foundry. Thanks to BOSH-DNS, microservices running on PCF do not require a service registry (such as Netflix Eureka or Hashicorp Consul), since all apps owning a route on the domain apps.internal can be resolved by all apps running on the platform. For example, if an app has a route foo.apps.internal, any app can access it from within the platform. Moreover, these apps do not require a public route in order to be accessible from apps, and do not use the gorouter.

An app making a direct connection requires a network policy. This network policy allows a container app to open a connection to an other container:

$ cf add-network-policy <app-source> --destination-app <app-target> --protocol tcp --port <port>

Using BOSH-DNS and container-to-container, your microservices do not require any external libraries to locate endpoints.

If you are using these features, you do not need to install the Spring Cloud Service tile on PCF, since BOSH-DNS is a core platform feature.

This project is not using Spring Cloud Netflix: no Hystrix, no Eureka, no Ribbon.

REST calls are made using Retrofit2, and network errors are managed using a circuit breaker pattern implemented by Resilience4j. Yet, this app is fault tolerant, and can be scaled-out (more instances) and scaled-up (more CPU/memory) with no downtime.

All these features are available for all apps with any language (not only Spring Boot apps written in Java).

How to use it?

This demo project is made of two components:

  • pcf-c2c-backend: a microservice exposing a REST API
  • pcf-c2c-frontend: a microservice connecting to backend instances

You can use this project on any PCF 2.2+ instances, such as Pivotal Web Service.

Compile this project with Maven and a JDK 8, and deploy these apps to Pivotal Cloud Foundry:

$ ./mvnw clean package && cf push

The frontend app is the only one exposing a public endpoint:

$ curl -s http://pcf-c2c-frontend-RANDOM-WORDS.domain.com
Welcome to PCF Container-to-container Demo
Frontend instance: [pcf-c2c-frontend/0 10.255.24.52]
Connecting to backend: pcf-c2c-backend.apps.internal:8080
Received message from backend:
  No backend service available
Time spent: 8 ms

As you can see, backend app instances are not seen by frontend app instances. You need to "allow" connections between frontend app instances and backend app instances:

$ cf add-network-policy pcf-c2c-frontend --destination-app pcf-c2c-backend --protocol tcp --port 8080

This command enables container-to-container networking between app instances, from frontend app instances to backend app instances.

As soon as this network policy is applied (it can take up to ten seconds), backend app instances are now accessible by frontend app instances:

$ curl -s http://pcf-c2c-frontend-RANDOM-WORDS.domain.com
Welcome to PCF Container-to-container Demo
Frontend instance: [pcf-c2c-frontend/0 10.255.24.52]
Connecting to backend: pcf-c2c-backend.apps.internal:8080
Received message from backend:
  [pcf-c2c-backend/3 10.255.24.25] says:
  Thank you for coming, [pcf-c2c-frontend/0 10.255.24.52]!
  Visitor count: 1
Time spent: 10 ms

If you kill a backend app instance used by a frontend app instance, an other backend app instance will automatically be resolved by BOSH-DNS the next time a frontend app instance is making a REST call. Notice there is no app downtime while a new backend app is being used.

Client-side load-balancing is done without using an external library (such as Netflix Ribbon). A custom OkHttp3 Interceptor implementation (used by Retrofit2) is included to load balance network requests. This implementation simply uses backend IP addresses given by BOSH-DNS, using a call to InetAddress.getByName.

You can disable client-side load-balancing by overriding the property backend.loadBalancing, which is set to true by default. You may also set this property using an environment variable:

$ cf set-env pcf-c2c-frontend BACKEND_LOADBALANCING false
$ cf restage pcf-c2c-frontend

Contribute

Contributions are always welcome!

Feel free to open issues & send PR.

License

Copyright © 2018 Pivotal Software, Inc.

This project is licensed under the Apache Software License version 2.0.

pcf-c2c-demo's People

Contributors

alexandreroman avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

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.