springcloud Ultra-Simple Introduction 2--Eureka Service Governance

Keywords: Java Spring Attribute

Eureka Service Governance

Now listen to the first topic, mother... Cough, I got the wrong book.

Eureka introduction

What is eureka?

Simply put, when I have more micro-service applications, it is a very elegant thing to write to death in a re-program, and the same service may exist in multiple instances to distribute services, that is, load balancing.

So, we need a location to store the access list of the service for the consumer to use, which can be implemented with eureka.

Let's look at the concepts of eureka:

  • Related concepts

    • Register

      When the Eureka client registers with the Eureka server, it provides its own metadata, such as IP address and port information.

    • Renew

      The client sends a heartbeat every 30 seconds to renew the service.

    • Cancel

      The client sends a cancellation request to the server when the program closes, and the successful instance will be deleted from the server registration list.

    • Eviction

      By default, when a client fails to send a heartbeat request for 90 seconds in a row, the server deletes the service instance from the service list and excludes the service.

    • Get Fetch Registriers

      The client obtains the service registration list information from the server and caches it locally. List information is updated periodically (30 seconds).

It doesn't matter if we don't understand it. We can build the shelf first, and then slowly realize it.

Eureka role

There are three roles in Eureka: registry, service provider and service consumer.

The registry is the Eureka server, and both service providers and service consumers are clients.

  • Registry Server

    Mainly carries on, the service registration, the service renewal and the service offline. It's about maintaining the list of services.

    Let's start step by step.

    • Dependent dependence
    <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    • Start the class configuration and enable the Eureka service with the @Enable Eureka Server annotation
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekacenteralApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekacenteralApplication.class, args);
        }
    
    }
    • Attribute configuration
    server:
      port: 9090 ##UI Interface Port
    eureka:
      instance:
        hostname: localhost  ##Host name
      client:
        register-with-eureka: false  ##Whether to register with the service center, because it is the service center itself, do not need to register
        fetch-registry: false  ##Are Services Discovered
        service-url:          ##Registry Service Address
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    Start the service, and such a simple registry will start. Browsers can access http://localhost:9090 to see the monitoring interface of eureka.

  • Service Provider

    eureka client, providing services to the outside world

    • Relevant dependencies, as Euraka clients
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>
    • Start the class configuration and start the Eureka client with the @EnableEurekaClient annotation
    @SpringBootApplication
    @EnableEurekaClient
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class,args);
        }
    }
    • Property configuration (multiple services can be started by setting different ports on a stand-alone machine)
    server:
      port: 8080
    eureka:
      instance:
        prefer-ip-address: true  ##Use IP Registration Service
      client:
        register-with-eureka: true  #The default is true. You can set it up without setting it up.
        fetch-registry: true  #The default is true, so you can set it up without setting it up.
        service-url:
          defaultZone: http://localhost:9090/eureka/
    spring:
      application:
        name: microservice-provider
    • Service interface
    @RestController
    @RequestMapping("/")
    public class TestController {
    
        @GetMapping("hi/{name}")
        public String hi(@PathVariable String name, HttpServletRequest request){
            try {
                Thread.sleep(2010);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hi,welcome "+name+" from port:"+request.getServerPort();
        }
    }
  • Service consumers

    Essentially, the same kind of client as the provider caches a list of services locally and updates the cache periodically.

    • Dependent dependence
    <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    • Start Class Configuration
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(App.class,args);
        }
        @Bean
        public RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
    }
    • Attribute configuration
    server:
      port: 8090
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:9090/eureka/
        register-with-eureka: false ##Just as a consumer
        fetch-registry: true  #The default is true, so you can set it up without setting it up.
    
      ## To say a few more words, service providers and consumers are not absolute, they can exist as two roles at the same time.
      ## register-with-eureka: true
      ## fetch-registry: true  
      ## When both familiarity are true, they can exist as both roles, that is, as providers, to provide services to the outside world.
      ## It can also be used as a consumer and consumer service.
    • Service consumption, it's all in the code, I did it, you are free.
    @RestController
    @RequestMapping("/")
    public class TestController {
    
    
        // When a service has only one instance running, a service provider,
        // Even if we don't register with eureka, we can use this method to call on line.
        @Resource
        RestTemplate restTemplate;
        @GetMapping("hi")
        public String hi() {
            return restTemplate.getForObject("http://localhost:8090/hi/consumer", String.class);
        }
    
        //When there are multiple instances of a service, we can use the service to discover or obtain a list of services and invoke one of them.
        // That's the actual scenario, and of course, a more concise way to use it, let's say next time.
        @Resource
        DiscoveryClient discoveryClient;
        //Through service discovery, get the registered service in the registry, get the call address, and then call the service
        @GetMapping("hi2")
        public String hi2() {
            List<ServiceInstance> serviceInstances = discoveryClient
                    .getInstances("microservice-provider");
            if (serviceInstances.size() == 0) return "service has down";
            // Here is the first service to consume.
            return restTemplate.getForObject(String.format("%s/hi/consumer", serviceInstances.get(0).getUri()),
                    String.class);
        }
    }

Posted by nobertos on Sat, 14 Sep 2019 04:40:54 -0700