Spring Cloud: building Eureka

Keywords: Java Spring P4 network

Setup of Eureka Server:

Using IDEA tools

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

Next

Create next - > next

 

Modify the startup class:

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

}

 

To configure:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    # Declare yourself as a server
    register-with-eureka: false
    fetch-registry: false
    # Address of Registration Center
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

After startup, access: http://localhost:8761 / enter the control console

Since only the server has no client, it is empty here

 

Setup of Eureka Client:

Create a spring MVC project normally:

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 {
    // simulation DAO Layer operation
    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);
    }
}

Commodity entity class:

package org.dreamtech.product.domain;

import java.io.Serializable;

public class Product implements Serializable {
    //ID
    private int id;
    //Trade name
    private String name;
    //commodity price
    private int price;
    //Commodity inventory
    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;
    }
}

Start the project, visit: http://localhost:8080/api/product/list

The response is as follows:

[{
    "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
}]

Introducing Eureka:

server:
  port: 8771
# Specify registry address
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
# Name of the service
spring:
  application:
    name: product-service

IDEA simulation starts multiple services:

Visit: http://localhost:8761/

If the above figure is displayed, it indicates that the building is successful

 

If these red words appear: Eureka's self-protection, the service will not be excluded if it cannot be called due to network reasons

For example, if someone is trapped on a desert island, it can't be said that this person is dead. Maybe he will be saved in a period of time. If self-protection is closed, Yan Wang will say that you are dead and no one can save you

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.

Turn off self protection:

  server:
    enable-self-preservation: false

Posted by ciaran on Mon, 11 Nov 2019 07:21:31 -0800