This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging

introduce

Eureka is a REPRESENTational State Transition (REST) based service that is primarily used in the AWS cloud to locate services for load balancing and failover of mid-tier servers. We call this service Eureka server. Eureka also comes with a Java-based Client component, the Eureka Client, which makes it easier to interact with services. The client also has a built-in load balancer that performs basic cyclic load balancing. At Netflix, more sophisticated load balancers package Eureka to provide weighted load balancing based on multiple factors such as traffic, resource usage, error conditions, etc., to provide excellent elasticity.

The official introduction is here in Eureka wiki.

Eureka is Netflix’s open source RESTful service for service registry discovery. (Netflix is an American company)

Eureka consists of two components: Eureka server and Eureka client.

The Eureka server functions as a service registry server.

The Eureka client is a Java client designed to simplify interaction with the server, act as a polling load balancer, and provide failover support for services.

Netflix uses a separate client in its production environment that provides weighted load balancing based on traffic, resource utilization, and error status.

In my opinion, Eureka’s appeal lies in the following:

Open source: You can explore the implementation and even modify the source code.

Reliable: After years of testing in Netflix’s production environment, it should be reliable and easy to use

Full functionality: not only provides a complete registration and discovery service, but also the Ribbon and other services can be used together.

Based on Java: For Java programmers, it is easy to use.

Spring Cloud can use Spring Cloud, and Eureka has a good integration, very easy to use.

In general, Eureka is a service discovery framework.

Specifically, create two Model projects: one as a service registry, the Eureka Server, and the other as a Eureka Client.

Simple module relationships

Implementing a simple Eureka Demo requires three projects:

  1. The registry
  2. Service provider
  3. Service consumer

1. Registry

  • pom.xml

Spring – the cloud – starter – netflix – eureka – rely on the server

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
Copy the code
  • application.yml
server:
  port: 8888
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone:  http://${eureka.instance.hostname}:${server.port}/eureka/
Copy the code
  • App.java
@SpringBootApplication
@EnableEurekaServer
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
        System.out.println("Registry started successfully"); }}Copy the code

2. Service provider

  • Pom. XML is omitted
  • application.yml
server:
  port: 8888
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone:  http://${eureka.instance.hostname}:${server.port}/eureka/
Copy the code
  • App.java
@SpringBootApplication
@EnableEurekaServer
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
        System.out.println("Registry started successfully"); }}Copy the code
  • ProviderController.java`
@RestController
public class ProviderController {
    @RequestMapping("/getUserList")
    public List<String> getUserList(a) {
        List<String> listUser = new ArrayList<String>();
        listUser.add("zhangsan");
        listUser.add("lisi");
        listUser.add("yushengjun");
        returnlistUser; }}Copy the code

3. Service consumers

  • Pom. XML is omitted
  • application.yml
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8764
spring:
  application:
    name: consumer
Copy the code
  • App.java
@EnableEurekaClient
@SpringBootApplication
public class AppOrder {
    public static void main(String[] args) {
        SpringApplication.run(AppOrder.class, args);
        System.out.println("Successful consumer launch");
    }

    @Bean
    @LoadBalanced // Open the ribbon to provide load balancing
    RestTemplate restTemplate(a) {
        return newRestTemplate(); }}Copy the code
  • ConsumerController.java
@Controller
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/getdata")
    @ResponseBody
    List<String>  getAllpoints(a){
        return restTemplate.getForObject("http://provider/getUserList", List.class); }}Copy the code