天天看點

SpringCloud學習筆記(四、注冊資料微服務)

建立子項目

SpringCloud學習筆記(四、注冊資料微服務)

pom.xml:

spring-cloud-starter-netflix-eureka-client 表示這是個 eureka 用戶端。

spring-boot-starter-web: 表示這是個web服務,會提供控制層

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>edu.hpu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>product-data-service</artifactId>

    <name>product-data-service</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <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-web</artifactId>
        </dependency>
    </dependencies>


</project>
      

實體類Product:

package edu.hpu.springcloud.pojo;

public class Product {
    private int id;
    private String name;
    private int price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public Product(int id, String name, int price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public Product() {
    }
}      

服務層ProductService:

這裡把 端口号 放進了産品資訊裡。 這個資料服務會做成叢集,那麼通路者為了分辨到底是從哪個資料微服務取的資料,就需要提供個端口号,才能意識到是從不同的微服務得到的資料。

package edu.hpu.springcloud.pojo;

public class Product {
    private int id;
    private String name;
    private int price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public Product(int id, String name, int price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public Product() {
    }
}      
控制層ProductController:

把 Product 集合轉換成 json 數組。

package edu.hpu.springcloud.controller;

import edu.hpu.springcloud.pojo.Product;
import edu.hpu.springcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping("/products")
    public Object products() {
        List<Product> ps = productService.listProducts();
        return ps;
    }
}      

啟動類ProductDataServiceApplication:

這裡自定義了一個Future類,五秒内使用者沒有選擇端口,則自動選用8001。

package edu.hpu.springcloud;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NetUtil;
import cn.hutool.core.util.NumberUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@SpringBootApplication
@EnableEurekaClient
public class ProductDataServiceApplication {
    public static void main(String[] args) {
        int port = 0;
        int defaultPort = 8002;
        Future<Integer> future = ThreadUtil.execAsync(() ->{
            int p = 0;
            System.out.println("請于5秒鐘内輸入端口号, 推薦  8001 、 8002  或者  8003,超過5秒将預設使用 " + defaultPort);
            Scanner scanner = new Scanner(System.in);
            while(true) {
                String strPort = scanner.nextLine();
                if(!NumberUtil.isInteger(strPort)) {
                    System.err.println("隻能是數字");
                    continue;
                }
                else {
                    p = Convert.toInt(strPort);
                    scanner.close();
                    break;
                }
            }
            return p;
        });
        try{
            port=future.get(5, TimeUnit.SECONDS);
        }
        catch (InterruptedException | ExecutionException | TimeoutException e){
            port = defaultPort;
        }

        if(!NetUtil.isUsableLocalPort(port)) {
            System.err.printf("端口%d被占用了,無法啟動%n", port );
            System.exit(1);
        }

        new SpringApplicationBuilder(ProductDataServiceApplication.class).properties("server.port=" + port).run(args);
    }
}
      

配置application.yml:

#   server:
#   port: 因為會啟動多個 product-data-service, 是以端口号由使用者自動設定,推薦 8001,8002,8003

spring:
  application:
    name: product-data-service
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/      

啟動并通路

啟動ProductDataServiceApplication,

SpringCloud學習筆記(四、注冊資料微服務)

第一次出了點問題:

com.sun.jersey.api.client.ClientHandlerException: java.net.SocketTimeoutException: Read timed out      

這個好像是配置的問題,我沒有查出來,再跑一遍,結果就沒問題了,但是在注冊中心還是有顯示,目前沒有搞清楚到底什麼情況。

通路注冊中心:

http://localhost:8761/
SpringCloud學習筆記(四、注冊資料微服務)

通路端口為8001服務:

http://127.0.0.1:8001/products
SpringCloud學習筆記(四、注冊資料微服務)

再跑一下啟動類,這次選擇8002:

注冊中心:

SpringCloud學習筆記(四、注冊資料微服務)
http://127.0.0.1:8002/products
SpringCloud學習筆記(四、注冊資料微服務)

參考:

【1】、

http://how2j.cn/k/springcloud/springcloud-eureka-client/2039.html#nowhere