1. Write the microservice MSC-Provider8081 and deploy it to K8S

1.1 Create a microservice project MSC-Provider8081

Idea create a Spring Initializr project and name it mSC-provider-8081. Import Lombok, Web, JPA, and MySQL driver dependencies.


Pom.xml adds dependencies

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.1.12. RELEASE</version>

        <relativePath/> <! -- lookup parent from repository -->

    </parent>

    <groupId>com.jd</groupId>

    <artifactId>msc-provider8081</artifactId>

    <version>0.0.1 - the SNAPSHOT</version>

    <name>msc-provider8081</name>

    <description>Demo project for Spring Boot</description>



    <properties>

        <java.version>1.8</java.version>

        <spring-cloud.version>Greenwich.SR5</spring-cloud.version>

    </properties>

    <dependencies>

        <! - physical dependence - >

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-actuator</artifactId>

        </dependency>



        <! -- Eureka client dependencies -->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-data-jpa</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>



        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>druid</artifactId>

            <version>1.1.10</version>

        </dependency>



        <! MySQL > update MySQL driver

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.47</version>

            <scope>runtime</scope>

        </dependency>

        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

            <optional>true</optional>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

            <exclusions>

                <exclusion>

                    <groupId>org.junit.vintage</groupId>

                    <artifactId>junit-vintage-engine</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

    </dependencies>

Copy the code

Defining entity Classes

package com.jd.bean;



import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.Data;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;



@Data

@Entity // Use automatic table building

@JsonIgnoreProperties({"hibernateLazyInitializer"."handler"."fieldHandler"}) // Ignore hibernate lazy loading features

public class Depart {

    @Id // Represents the primary key of the table whose current property is automatically built

    @GeneratedValue(strategy= GenerationType.IDENTITY)// Primary keys are automatically incremented

    private Integer id;

    private String name;

}

Copy the code

Defining the Repository interface

package com.jd.repository;





import com.jd.bean.Depart;

import org.springframework.data.jpa.repository.JpaRepository;



// The first generic is the type of object that the current Repository operates on

// The second generic type is the ID type of the object that the current Repository operates on

public interface DepartRepository extends JpaRepository<Depart.Integer{





}

Copy the code

Defining the Service Interface

package com.jd.service;





import com.jd.bean.Depart;

import java.util.List;



public interface DepartService {



    boolean saveDepart(Depart depart);

    boolean removeDepartById(Integer id);

    boolean modifyDepart(Depart depart);

    Depart getDepartById(int id);

    List<Depart> listAllDeparts(a);

}

Copy the code

Define the Service implementation class

package com.jd.service;



import com.jd.bean.Depart;

import com.jd.repository.DepartRepository;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

import java.util.concurrent.TimeUnit;



@Service

public class DepartServiceImpl implements DepartService {



    @Autowired

    private DepartRepository repository;



    @Override

    public boolean saveDepart(Depart depart) {

        Depart obj = repository.save(depart);

        returnobj ! =nulltrue : false;

    }



    @Override

    public boolean removeDepartById(Integer id) {

        if(repository.existsById(id)){

            repository.deleteById(id);

            return true;

        }

        return false;

    }



    @Override

    public boolean modifyDepart(Depart depart) {

        Depart obj = repository.save(depart);

        returnobj ! =nulltrue : false;

    }



    @Override

    public Depart getDepartById(int id) {

        if(repository.existsById(id)){

            return repository.getOne(id);

        }

        Depart depart = new Depart();

        depart.setName("no this depart");

        return depart;

    }



    @Override

    public List<Depart> listAllDeparts(a) {



        return repository.findAll();

    }

}

Copy the code

Define handler

package com.jd.controller;



import com.jd.bean.Depart;

import com.jd.service.DepartService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.client.ServiceInstance;

import org.springframework.cloud.client.discovery.DiscoveryClient;

import org.springframework.web.bind.annotation. *.

import java.util.List;

import java.util.Map;



@RestController

@RequestMapping("/provider/depart")

public class DepartController {



    @Autowired

    private DepartService departService;



    @PostMapping(" /save")

    public boolean saveHandler(@RequestBody Depart depart){

        return departService.saveDepart(depart);

    }



    @DeleteMapping(" /del/ {id} ")

    public boolean delHandler(@PathVariable("id") Integer id){

        return departService.removeDepartById(id);

    }



    @PutMapping(" /update")

    public boolean updateHandler(@RequestBody Depart depart){

        return departService.modifyDepart(depart);

    }



    @GetMapping(" /get/ {id} ")

    public Depart getHandler(@PathVariable("id") Integer id){

        return departService.getDepartById(id);

    }



    @GetMapping(" /list")

    public List<Depart> listHandler(){

        return departService.listAllDeparts(a);

    }

}

Copy the code

Configuration file application.yml

server:

  port: 8081

spring:

  # specify the name of the microservice to expose

  application:

    name: msc-provider

  # configuration jpa

  jpa:

    generate-ddl: true # specifies whether to create tables at Spring startup. The default is false

    show-sqltrue # Specifies whether SQL statements are displayed on the console. The default is false

    hibernate:

      ddl-autonone Table contents are not updated after the application restarts





  # configuration

  datasource:

    type: com.alibaba.druid.pool.DruidDataSource

    driver-class-name: com.mysql.jdbc.Driver

    url: jdbc:mysql://192.168. 5672.:3306/kkb? useUnicode=true&amp; characterEncoding=utf8

    username: root

    password: root





eureka:

  instance:

    prefer-ip-address: true Use the service IP address to register the service

  client:

    service-url:

# cluster - IP address

DefaultZone: http://10.68.96.71:8761/eureka

Copy the code

Package the project

1.2 Deploying the microservices service provider MSC-Provider8081 to the K8S cluster

1.2.1 Upload the project JAR package to the CentOS machine

[root@master01 ~]# mkdir -p /root/app/demo/provider

1.2.2 Creating a Dockerfile file in /root/app/demo/provider

[root@master01 provider]# cat Dockerfile

# Based on which mirror

FROM java:8

Mount the local folder to the current container

VOLUME /root/app/demo/provider

Copy files to containers

ADD  provider.jar /provider.jar

Declare the ports to be exposed

EXPOSE 8081

Configure the commands to be executed after the container is started

ENTRYPOINT ["java"."-jar"."/provider.jar"]

Copy the code

1.2.3 Building microservice Images with Dockerfile

[root@master01 provider]# docker build -t microservice-provider:v1. [root@master01 provider]# docker tag microservice-provider:v1 registry.cn-beijing.aliyuncs.com/registry_tomato/microservice-provider:v1

1.2.4 Uploading the image to Ali Cloud Hub

Note: Docker must be logged in to Docker login first

[root@master01 provider]# docker push registry.cn-beijing.aliyuncs.com/registry_tomato/microservice-provider:v1

1.2.5 Creating the Cluster deployment File provider.yaml

[root@master01 provider]# cat provider.yml

#deploy

apiVersion: apps/v1

kind: Deployment

metadata:

  name: provider

spec:

  selector:

    matchLabels:

      app: provider

  replicas: 1

  template:

    metadata:

      labels:

        app: provider

    spec:

      containers:

      - name: provider

        image: registry.cn-beijing.aliyuncs.com/registry_tomato/microservice-provider:v1

        ports:

        - containerPort: 8081

---

#service

apiVersion: v1

kind: Service

metadata:

  name: provider-service

spec:

  ports:

  - port: 8081

    nodePort: 38081

    protocol: TCP

  type: NodePort

  selector:

    app: provider

Copy the code

1.2.6 Starting the service Provider

[root@master01 provider]# kubectl apply -f provider.yml

1.2.7 Viewing the POD Copy and Service

[root@master01 provider]# kubectl get pods -o wide

[root@master01 provider]# kubectl get svc -o wide

1.2.8 Test and Verification

Add testing

Query by ID

Modify the data

Query all

Delete by ID