Spring Cloud(2):搭建Eureka

Eureka Server的搭建:

使用IDEA工具

File->New Project->Spring Initializr->next

Next

 Next->Next创建即可

 

修改启动类:

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);
    }

}

 

进行配置:

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/进入管控台

由于只有服务端没有客户端,这里显示空

 

Eureka Client客户端的搭建:

正常的创建一个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

 

posted @ 2019-05-14 13:37  4ra1n  阅读(771)  评论(0编辑  收藏  举报