1、编写微服务消费者consumer并部署到K8S

1.1 创建一个 Spring Initializr 工程

命名为 01-consumer-8080,导入 Lombok 与 Web 依赖。

pom.xml添加依赖

 <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-consumer8080</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>msc-consumer8080</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>
        <!--actuator依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--eureka客户端依赖-->
        <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-web</artifactId>
        </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>
        </dependency>
    </dependencies>

定义实体类

package com.jd.bean;
import lombok.Data;

@Data
public class Depart {

    private Integer id;
    private String name;
}

定义 JavaConfig 容器类

package com.jd.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class DepartConfigure {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

定义控制器类

package com.jd.controller;
import com.jd.bean.Depart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;

@RestController
@RequestMapping("/consumer/depart")
public class SomeController {

    @Autowired
    private RestTemplate restTemplate;
    //直连提供者
    //private static final String SERVICE_PROVIDER = "http://localhost:8081";
    //要使用微服务名称来从eureka server查找提供者
    private static final String SERVICE_PROVIDER = "http://msc-provider";


    @PostMapping("/save")
    public boolean saveHandler(@RequestBody Depart depart){
        String url = SERVICE_PROVIDER + "/provider/depart/save";
        return restTemplate.postForObject(url, depart,Boolean.class);
    }

    @DeleteMapping("/del/{id}")
    public void delHandler(@PathVariable("id") int id){
        String url = SERVICE_PROVIDER + "/provider/depart/del/" + id;
        restTemplate.delete(url);
    }

    @PutMapping("/update")
    public void updateHandler(@RequestBody Depart depart){
        String url = SERVICE_PROVIDER + "/provider/depart/update";
        restTemplate.put(url, depart);
    }

    @GetMapping("/get/{id}")
    public Depart getByIdHandler(@PathVariable("id") int id){
        String url = SERVICE_PROVIDER + "/provider/depart/get/" + id;
        return restTemplate.getForObject(url, Depart.class);
    }

    @GetMapping("/list")
    public List<Depart> listHandler(){
        String url = SERVICE_PROVIDER + "/provider/depart/list";
        return restTemplate.getForObject(url, List.class);
    }
}

配置文件 application.yml

server:
  port: 8080
spring:
  #指定当前微服务对外暴露的名称
  application:
    name: msc-consumer


eureka:
  instance:
    prefer-ip-address: true #注册服务的时候使用服务的ip地址
  client:
    service-url:
      #指定当前client要连接的eureka server
      defaultZone: http://10.68.96.71:8761/eureka

将工程打包

1.2 部署微服务消费者工程msc-consumer8080到K8S集群

1.2.1 将consumer工程的jar包上传到CentOS机器上

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

1.2.2 在目录/root/app/demo/consumer下创建Dockerfile文件

[root@master01 consumer]# cat Dockerfile

# 基于哪个镜像
FROM java:8
# 将本地文件夹挂载到当前容器
VOLUME /root/app/demo/consumer
# 复制文件到容器
ADD consumer.jar /consumer.jar
# 声明需要暴露的端口
EXPOSE 8080
# 配置容器启动后执行的命令
ENTRYPOINT ["java","-jar","/consumer.jar"]
1.2.3 用Dockerfile构建微服务镜像

在目录/root/app/demo/consumer下执行 docker build构建镜像

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

1.2.4 镜像上传到阿里云

备注:docker 需要先登录 docker login

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

1.2.5 创建集群部署文件 consumer.yaml

[root@master01 provider]# cat consumer.yml

#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: consumer
spec:
  selector:
    matchLabels:
      app: consumer
  replicas: 1
  template:
    metadata:
      labels:
        app: consumer
    spec:
      containers:
      - name: consumer
        image: registry.cn-beijing.aliyuncs.com/registry_tomato/microservice-consumer:v1
        ports:
        - containerPort: 8080
---
#service
apiVersion: v1
kind: Service
metadata:
  name: consumer-service
spec:
  ports:
  - port: 8080
    protocol: TCP
    nodePort: 38080
  type: NodePort
  selector:
    app: consumer
1.2.6 启动服务消费者

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

1.2.7 查看pod副本和service

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

1.2.8 测试和验证

根据id查询

更新数据

获取所有数据

测试成功,没问题服务消费者部署成功!!

代码下载地址:
https://github.com/coderTomato/microservicecloud.git

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐