Wednesday, February 15, 2023

How to connect to a cloud Kubernetes service

You can use kubectl, the command line tool for Kubernetes, to access a Kubernetes cluster from your local host. To do this, you will need to create a Kubernetes Service Account, then create a service account token and configure your local kubectl to use that token.

To create a Kubernetes Service Account on your local host, you will need to use the kubectl command line tool. kubectl create serviceaccount <service-account-name> Replace <service-account-name> with a name of your choice. This will create a Kubernetes Service Account with the given name. Once you have created the service account, you will need to create a service account token. You can do this by running the following command: kubectl create secret generic <secret-name> --from-literal=token=<token-value> Replace <secret-name> with a name of your choice, and <token-value> with a valid token value. This will create a service account token with the given name and token value. Once the service account token is created, you can configure your local kubectl to use the token. To do this, you can run the following command: kubectl config set-credentials <service-account-name> --token=<token-value> Replace <service-account-name> with the name of the service account you created and <token-value> with the token value you generated. This will configure your local kubectl to use the token for authentication when connecting to the service account.

Wednesday, January 11, 2023

kafka consumer

import org.apache.kafka.clients.consumer.ConsumerConfig;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.kafka.annotation.EnableKafka;

import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;

import org.springframework.kafka.core.ConsumerFactory;

import org.springframework.kafka.core.DefaultKafkaConsumerFactory;


import java.util.HashMap;

import java.util.Map;


@EnableKafka

@Configuration

public class KafkaConsumerConfig {


    @Bean

    public ConsumerFactory<String, String> consumerFactory() {

        Map<String, Object> config = new HashMap<>();


        config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");

        config.put(ConsumerConfig.GROUP_ID_CONFIG, "group_id");

        config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

        config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);


        return new DefaultKafkaConsumerFactory<>(config);

    }


    @Bean

    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {

        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory();

        factory.setConsumerFactory(consumerFactory());

        return factory;

    }


}



import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class KafkaConsumer {
    @KafkaListener(topics = "topic_name", groupId = "group_id")
    public void consume(String message) {
        // load file action
    }
}

Monday, January 9, 2023

maven generate java classes from xsd schema

 You can use the JAXB Maven plugin to generate Java classes from an XSD schema. Here's an example of how to do it:

  1. Add the JAXB plugin to your pom.xml file:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory> <outputDirectory>${basedir}/src/main/java</outputDirectory> </configuration> <dependencies> <dependency> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> </dependency> </dependencies> </plugin> </plugins> </build>
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.4.0</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory> <outputDirectory>${basedir}/src/main/java</outputDirectory> </configuration> </plugin> </plugins> </build>
  1. Place your XSD schema file(s) in the ${basedir}/src/main/resources/xsd directory (or the directory specified in the schemaDirectory parameter in the plugin configuration).

  2. Run the following Maven command to generate the Java classes:

mvn clean generate-sources

The generated Java classes will be placed in the ${basedir}/src/main/java directory (or the directory specified in the outputDirectory parameter in the plugin configuration).

Note: This will only work if you have the necessary dependencies in your pom.xml file. Specifically, you will need the following dependency:

<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency>