SpringBoot App inside Docker

Commands

// rebuild some service in docker compose

docker-compose up -d --no-deps --build recognition-server

// start all containers in docker compose

docker-compose up

// down all containers

docker-compose down

Files

Dockerfile

FROM eclipse-temurin:17.0.10_7-jdk-jammy
MAINTAINER stanley_ekb
COPY ../build/libs build/libs
ENTRYPOINT ["java","-jar","build/libs/recognition-0.0.1-SNAPSHOT.jar", "sber.recognition.App", "-cp", "foodVision.recognition.main", "--server.port=2077"]

docker-compose.yml

version: '3'

services:
  recognition-server:
    container_name: recognition-server
    build:
      context: ../recognition
      dockerfile: ./docker/Dockerfile
    image: recognition-server:latest
    ports:
      - 2077:2077
#    networks:
#      - spring-cloud-network

  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    restart: unless-stopped
    ports:
      - "22181:2181"

  kafka:
    image: confluentinc/cp-kafka:latest
    container_name: kafka
    depends_on:
      - zookeeper
    restart: unless-stopped
    ports:
      - "9092:9092"
      - "29092:29092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
Posted in Без рубрики | Comments Off on SpringBoot App inside Docker

Kafka.FirstExperience

docker-compose.yml

version: '3'

services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    restart: unless-stopped
    ports:
      - "22181:2181"

  kafka:
    image: confluentinc/cp-kafka:latest
    container_name: kafka
    depends_on:
      - zookeeper
    restart: unless-stopped
    ports:
      - "9092:9092"
      - "29092:29092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

go to the directory of file and type

docker-compose up

kafka container console

docker exec -it kafka /bin/bash

create topic

kafka-topics --create --topic myfirstTopic --partitions 1 --bootstrap-server localhost:9092

list all topics

kafka-topics --list --bootstrap-server localhost:9092

writing to topic

kafka-console-producer --broker-list localhost:9092 --topic myfirstTopic

reading wrom topic

kafka-console-consumer --bootstrap-server localhost:9092 --topic myfirstTopic --from-beginning
Posted in Без рубрики | Comments Off on Kafka.FirstExperience

Docker.MySql and Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0

// first of all disable mySql Service if it was installed before or you will receive error like this

Lost connection to MySQL server at 'reading initial communication packet', system error: 0

// start docker container on 3307 port

docker run --name mysqlContainer -p 3307:3307 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% -d mysql:latest

// connect to mysql monitor

mysql -h localhost -P 3307 -u root -proot

// bash inside docker

docker exec -it mysql bash

Posted in Без рубрики | Comments Off on Docker.MySql and Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0

Gradle.Tips

ignoring cache and refreshes the dependencies

./gradlew --refresh-dependencies
./gradlew resolveAndLockAll --write-locks
Posted in Без рубрики | Comments Off on Gradle.Tips

Spring.AppConfig

application.yaml

server:
  port: 8080

app:
  client-service:
    value: 1
    url: https://httpbin.org/get
    stub-mode: ${CLIENT_SERVICE_STUB} // depends on enviroment, in enviroment APP_CLIENT_SERVICE_STUB

in app config

@EnableConfigurationProperties(value = AppProperties.class)
@Configuration
@EnableConfigurationProperties(value = AppProperties.class)
public class AppConfig {

    @Value("$(app.client-service.url)") // Так можно делать, но не надо делать в проде
    private String value;

    @Bean
    @Profile("!dev")
    public ClientService clientService(AppProperties appProperties) {
        return new ClientServiceImpl(appProperties);
    }

    @Bean
    @Profile("dev")
    public ClientService clientServiceStub(AppProperties appProperties) {
        return new ClientServiceStubImpl();
    }
}

AppProperties.java

package SpringBootApp.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "app")
@Getter
@Setter
public class AppProperties {
    private String value;

    @Getter
    @Setter
    private ClientServiceProperties clientServiceProperties;

    static public class ClientServiceProperties {
        private String url;

        private String stubMode;
    }
}

Posted in Без рубрики | Comments Off on Spring.AppConfig

Leetcode.Best-time-to-buy-and-sell-stock

Task

public class Solution {
    public int MaxProfit(int[] prices) {
                        
        int min = 11000;        

        int profit = 0;
        int maxProfit = 0;            

        for (int i = 0; i < prices.Length; i++) {
          
        if (prices[i] < min) {
            min = prices[i];            
          }          

          profit = prices[i] - min; 
          if (profit > maxProfit)
            maxProfit = profit;                                    
        }

        return maxProfit;
    }
}
Posted in Без рубрики | Comments Off on Leetcode.Best-time-to-buy-and-sell-stock

LeetCode.ClimbStairs.Fibbonacci

task

Here we can recognize fibbonacci sequence

recursievely

public class Solution {
    public int ClimbStairs(int n) {

       if (n == 0)
         return 1;
  
       return n > 1 ? ClimbStairs(n - 1) + ClimbStairs(n -2) : n; 
    }
}

iteratievely

public class Solution {
    public int ClimbStairs(int n) {

        if (n <= 1)
          return 1;

        int first = 1;
        int second = 1;          

        for (int i = 2; i <= n; i++) { 
          int sum = first + second;
          first = second;
          second = sum;   
        }

        return second;
    }
}

Posted in Без рубрики | Comments Off on LeetCode.ClimbStairs.Fibbonacci

Leetcode.Roman-to-integer

Task

public class Solution {
    public int RomanToInt(string s) {
        var numbers = new Dictionary<char, int>();
        numbers.Add('I', 1);
        numbers.Add('V', 5);
        numbers.Add('X', 10);
        numbers.Add('L', 50);
        numbers.Add('C', 100);
        numbers.Add('D', 500);
        numbers.Add('M', 1000);        
        
        var numbersDiminished = new Dictionary<String, int>();
        numbersDiminished.Add("IV", 4);
        numbersDiminished.Add("IX", 9);
        numbersDiminished.Add("XL", 40);
        numbersDiminished.Add("XC", 90);
        numbersDiminished.Add("CD", 400);
        numbersDiminished.Add("CM", 900);

        int result = 0;
        int number = 0;

        for (int i = 0; i < s.Length; i++) {
            
            number = numbers[s[i]];
            

            if ((s[i] == 'I' || s[i] == 'X' || s[i] == 'C') && i != s.Length - 1 )   {
                  string maybeDiminished = s.Substring(i, 2);                  
                  
                  if (numbersDiminished.ContainsKey(maybeDiminished)) {
                      number = numbersDiminished[maybeDiminished];                 
                      i++;   
                  }
            }  
                        
            result += number;
        }

        return result;
    }
}
Posted in Без рубрики | Comments Off on Leetcode.Roman-to-integer

LeetCode.Palindrome-number

task

public class Solution {
    public bool IsPalindrome(int x) {
        var s = x.ToString();
        
        int i = 0;
        int j = s.Length - 1;
        while (i <= j) {
          if (s[i] != s[j]) 
          return false;          
          i++;
          j--;
          
        } 

        return true;
    }
}
Posted in Без рубрики | Comments Off on LeetCode.Palindrome-number

Java.DesignPatterns.Lightweight

lightweight example

moving out repeating heavy objects to the stucture outside

Posted in Без рубрики | Comments Off on Java.DesignPatterns.Lightweight