springcloud ultra easy entry 2 - Eureka Service Management

Eureka Service Management

Please listen to the first topic, the mother. . . Keke, get the wrong book.

About Eureka

Eureka What is it?

In simple terms it, when my micro multi-service applications up dead one by one and then write in the program is not a very elegant thing, and the same service there may be multiple instances to bypass the service is load balancing.

So, we need a place to store a list of access services, for consumption by end use, this thing does, it may be implemented by eureka.

We look eureka related concepts:

  • Related concepts

    • Service Registration (Register)

      When eureka Eureka client registers to the server, which provides its own metadata, such as IP address, port information

    • Service contract (Renew)

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

    • Service offline (Cancel)

      The client sends a cancellation request to the server when the program is closed, the success of the server instance will be deleted from the list of registered

    • Excluding services (Eviction)

      By default, when the client 90 seconds to send a heartbeat request, the server will service instance is deleted from the list of services, the service removed

    • Obtain a list of registered service information (Fetch Registriers)

      Client obtains a list of registered service information from the server and cache it locally. List information on a regular basis (30 seconds) is updated.

I do not understand it does not matter, we can put a shelf built up, then slowly put Wu.

Eureka role

There are three roles, registry, service providers, service consumers in Eureka.

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

  • Registry server

    Mainly, service registration, service contract and service offline. It is to maintain a list of friends stuff services.

    Let us step by step to start

    • Its dependencies
    <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    • Startup class configuration, use @EnableEurekaServerannotations to enable Eureka Service
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekacenteralApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekacenteralApplication.class, args);
        }
    
    }
    • Configuration Properties
    server:
      port: 9090 ##UI界面端口
    eureka:
      instance:
        hostname: localhost  ##主机名称
      client:
        register-with-eureka: false  ##是否注册到服务中心,因为本身就是服务中心,不需要注册
        fetch-registry: false  ##是否发现服务
        service-url:          ##注册中心服务地址
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    Start the service, such a simple registration center will start up a browser to access http: // localhost: 9090 you can see the eureka monitoring interface.

  • service provider

    eureka clients, providing services outside

    • Its dependencies, as Euraka client
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>
    • Startup class configuration, usage @EnableEurekaClientnotes, start Eureka client
    @SpringBootApplication
    @EnableEurekaClient
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class,args);
        }
    }
    • (By setting different ports in stand-alone to start a plurality of service) attribute configuration
    server:
      port: 8080
    eureka:
      instance:
        prefer-ip-address: true  ##使用IP注册服务
      client:
        register-with-eureka: true  #默认就是true,可以不用设置
        fetch-registry: true  #默认是true,可以不用设置
        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();
        }
    }
  • Consumer Services

    Essentially the same category with the provider to the client, will be a service list information in the local cache, the cache is updated regularly

    • Its dependencies
    <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    • Startup class configuration
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(App.class,args);
        }
        @Bean
        public RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
    }
    • Configuration Properties
    server:
      port: 8090
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:9090/eureka/
        register-with-eureka: false ##仅仅作为消费者
        fetch-registry: true  #默认是true,可以不用设置
    
      ## 多说几句,服务者跟消费者不是绝对的,他们可以同时作为两种角色存在
      ## register-with-eureka: true
      ## fetch-registry: true  
      ## 当这两个熟悉都为true的时候,就可以作为两种角色同时存在了,即可以作为提供者,向外提供服务
      ## 又可以作为消费者,消费服务
    • Consumer services are in the code, I did, you are free.
    @RestController
    @RequestMapping("/")
    public class TestController {
    
    
        // 服务只有一个实例运行的时候,一个服务提供者,
        // 就算不注册到eureka,我们也可以使用这种方式就行调用
        @Resource
        RestTemplate restTemplate;
        @GetMapping("hi")
        public String hi() {
            return restTemplate.getForObject("http://localhost:8090/hi/consumer", String.class);
        }
    
        //当服务有多个实例的时候,我们就可以使用服务发现或获取服务列表,并取其中一个实例进行调用
        // 这样才是实际场景,当然还用更简洁的使用方式,我们下次说
        @Resource
        DiscoveryClient discoveryClient;
        //通过服务发现,获取注册中心上注册的服务,获取调用地址,然后调用服务
        @GetMapping("hi2")
        public String hi2() {
            List<ServiceInstance> serviceInstances = discoveryClient
                    .getInstances("microservice-provider");
            if (serviceInstances.size() == 0) return "service has down";
            // 这里就是取第一个服务来消费
            return restTemplate.getForObject(String.format("%s/hi/consumer", serviceInstances.get(0).getUri()),
                    String.class);
        }
    }

Guess you like

Origin www.cnblogs.com/justalittlecoder/p/11519922.html
Recommended