Code Monkey home page Code Monkey logo

springcloud-swarm's Introduction

Spring Cloud Demo on Docker

Spring Cloud application

Demo application is a tiny Spring Cloud application which has three services. Web and BookServicewill register itself to Eureka Service during boot. Web service depends on BookService, when it is invoked, it will find available instances of BookService from Eureka Server. Web will call BookService REST API to fulfill its result.

Build

Run this command to build all images, it will rebuild all images even if no code change.

./build-all.sh

Run application in local environment

Run demo application in local environment is wasy, there is an all-in-one compose file which allows you to run all the services.

cd compose
docker-compose -f all-in-one.yml up -d

Find which port mapping for each services.

$ docker-compose -f all-in-one.yaml ps
        Name                       Command               State            Ports
----------------------------------------------------------------------------------------
compose_bookservice_1   java -Djava.security.egd=f ...   Up
compose_eureka_1        java -Djava.security.egd=f ...   Up      0.0.0.0:8761->8761/tcp
compose_web_1           java -Djava.security.egd=f ...   Up      0.0.0.0:32771->8080/tcp

You will see eureka maps to port 8761 of localhost while web maps to 32771 in this case.

Access Eureka Server

Access Eureka server through port 8761. You will see this Eureka server has one replica and two services registered.

http://localhost:8761

Access the demo application

Docker allocated an random port for web service, you can get the port number using docker-compose ps command, or you can assign a port number in all-in-one.yml.

Run this command or access it using a web browser.

curl http://localhost:<port>

Deploy to docker swarm using compose V3 template

Let's take a look at compose/all-in-one.yml. There are no version 3 specific compose syntax, you can deploy the application by using docker-compose in local environment, or deploy it to a Docker swarm cluster directly.

version: '3'
services:
  eureka:
    image: binblee/demo-eurekaserver
    ports:
      - "8761:8761"
    ...

  web:
    image: binblee/demo-web
    environment:
      - EUREKA_SERVER_ADDRESS=eureka
    ports:
      - "8080"
    ...

  bookservice:
    image: binblee/demo-bookservice
    environment:
      - EUREKA_SERVER_ADDRESS=eureka
    ...

You can deploy it to a docker swarm cluster like this:

docker stack deploy -f all-in-one.yml springcloud-demo

Run in production environment

All in one is good, but you need to consider below factors when you want to run all the services in an production environment.

  • deploy Eureka server and applications separately, as Eureka server as an infrasturcture level service will not be updated frequently, it is not proper to deploy it with applications every time the application changes.
  • again, Eureka server as infrastructure level need some kind of HA (high availablity).

Let's break the deployment into two compose files. Using eureka.yml you can deply a three-node-cluster of Euerka. All instances have same network alias eureka for which Eureka client will looking for.

eureka.yml

version: '3'
services:
  eureka1:
    image: binblee/demo-eurekaserver
    networks:
      springcloud-overlay:
        aliases:
          - eureka
    ports:
      - "8761:8761"
    environment:
      - ADDITIONAL_EUREKA_SERVER_LIST=http://eureka2:8761/eureka/,http://eureka3:8761/eureka/
    ...
  eureka2:
    image: binblee/demo-eurekaserver
    networks:
      springcloud-overlay:
        aliases:
          - eureka
    ports:
      - "8762:8761"
    environment:
      - ADDITIONAL_EUREKA_SERVER_LIST=http://eureka1:8761/eureka/,http://eureka3:8761/eureka/
    ...
  eureka3:
    image: binblee/demo-eurekaserver
    networks:
      springcloud-overlay:
        aliases:
          - eureka
    ports:
      - "8763:8761"
    environment:
      - ADDITIONAL_EUREKA_SERVER_LIST=http://eureka1:8761/eureka/,http://eureka3:8761/eureka/
    ...
networks:
  springcloud-overlay:
    external:
      name: springcloud-overlay

demoweb.yml gets the content from original all-in-one.yml, adding network properties. All services will connect to springcloud-overlay network.

version: '3'
services:
  web:
    image: binblee/demo-web
    networks:
      - springcloud-overlay
    environment:
      - EUREKA_SERVER_ADDRESS=eureka
    ports:
      - "8080"
    ...

  bookservice:
    image: binblee/demo-bookservice
    networks:
      - springcloud-overlay
    environment:
      - EUREKA_SERVER_ADDRESS=eureka
    ...

networks:
  springcloud-overlay:
    external:
      name: springcloud-overlay

Let's deploy it, noted that springcloud-overlay network needs to be created before eureka and demoweb are deployed.

docker network create -d overlay springcloud-overlay
cd compose/
docker stack deploy -c eureka.yml
docker stack deploy -c demoweb.yml

Access port 8761 of any node in swarm, you will see Eureka instance #1 has two replicas, and services are registered to it. Visit port 8762 and 8763, you will get other two Eureka server, have a try.

Recap

springcloud-swarm's People

Contributors

binblee avatar

Watchers

James Cloos 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.