Spring Cloud(2):搭建Eureka

Eureka Server的搭建:java

使用IDEA工具web

File->New Project->Spring Initializr->nextspring

Nextapi

 Next->Next建立便可網絡

 

修改啓動類:app

package org.dreamtech.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }

}

 

進行配置:dom

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    # 聲明本身是服務端
    register-with-eureka: false
    fetch-registry: false
    # 註冊中心地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

啓動後訪問:http://localhost:8761/進入管控臺ide

因爲只有服務端沒有客戶端,這裏顯示空工具

 

Eureka Client客戶端的搭建:fetch

正常的建立一個SpringMVC項目:

Controller:

package org.dreamtech.product.controller;

import org.dreamtech.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/product")
public class ProductController {

    private final ProductService productService;

    @Autowired
    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    @RequestMapping("/list")
    public Object list(){
        return productService.getProductList();
    }

    @RequestMapping("/find")
    public Object findById(@RequestParam("id") int id){
        return productService.findById(id);
    }

}

Service:

package org.dreamtech.product.service;

import org.dreamtech.product.domain.Product;

import java.util.List;

public interface ProductService {
    List<Product> getProductList();
    Product findById(int id);
}
package org.dreamtech.product.service.impl;

import org.dreamtech.product.domain.Product;
import org.dreamtech.product.service.ProductService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {
    // 模擬DAO層操做
    private static final Map<Integer, Product> productDao = new HashMap<>();

    static {
        Product p1 = new Product(1, "iPhone1", 1111, 10);
        Product p2 = new Product(2, "iPhone2", 2222, 10);
        Product p3 = new Product(3, "iPhone3", 3333, 10);
        Product p4 = new Product(4, "iPhone4", 4444, 10);
        Product p5 = new Product(5, "iPhone5", 5555, 10);
        Product p6 = new Product(6, "iPhone6", 6666, 10);
        Product p7 = new Product(6, "iPhone7", 7777, 10);
        productDao.put(1, p1);
        productDao.put(2, p2);
        productDao.put(3, p3);
        productDao.put(4, p4);
        productDao.put(5, p5);
        productDao.put(6, p6);
        productDao.put(7, p7);
    }

    @Override
    public List<Product> getProductList() {
        Collection<Product> temp = productDao.values();
        return new ArrayList<>(temp);
    }

    @Override
    public Product findById(int id) {
        return productDao.get(id);
    }
}

商品實體類:

package org.dreamtech.product.domain;

import java.io.Serializable;

public class Product implements Serializable {
    //ID
    private int id;
    //商品名稱
    private String name;
    //商品價格
    private int price;
    //商品庫存
    private int store;

    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 int getStore() {
        return store;
    }

    public void setStore(int store) {
        this.store = store;
    }

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

啓動項目,訪問:http://localhost:8080/api/product/list

響應以下說明成功:

[{
    "id": 1,
    "name": "iPhone1",
    "price": 1111,
    "store": 10
}, {
    "id": 2,
    "name": "iPhone2",
    "price": 2222,
    "store": 10
}, {
    "id": 3,
    "name": "iPhone3",
    "price": 3333,
    "store": 10
}, {
    "id": 4,
    "name": "iPhone4",
    "price": 4444,
    "store": 10
}, {
    "id": 5,
    "name": "iPhone5",
    "price": 5555,
    "store": 10
}, {
    "id": 6,
    "name": "iPhone6",
    "price": 6666,
    "store": 10
}, {
    "id": 6,
    "name": "iPhone7",
    "price": 7777,
    "store": 10
}]

引入Eureka:

server:
  port: 8771
# 指定註冊中心地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
# 服務的名稱
spring:
  application:
    name: product-service

IDEA模擬啓動多個服務:

訪問:http://localhost:8761/

若是顯示如上圖,說明搭建成功

 

若是出現這些紅字:Eureka的自我保護,因爲網絡緣由出現沒法調用的狀況不會剔除服務

好比某我的被困在荒島上,不能說這我的死了,可能過一段時間他會獲救,若是關閉了自我保護,閻王就說你死了,誰都救不了你

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

關閉自我保護:

  server:
    enable-self-preservation: false
相關文章
相關標籤/搜索