Using Eureka Registration Center [Microservice]


Preface

Microservice content:

  • service call
  • Service downgrade
  • Service registration and discovery
  • Service circuit breaker
  • load balancing
  • service message queue
  • service gateway
  • Configuration center management
  • Automated build and deployment
  • Service monitoring
  • Full link tracking
  • Service scheduled tasks
  • Scheduling operations

Eureka adopts the CS design architecture. Eureka Sever serves as the server for the service registration function. It is the service registration center. Other microservices in the system use Eureka clients to connect to Eureka Server and maintain heartbeat connections . In this way, system maintenance personnel can use Eureka Server to monitor whether each microservice in the system is running normally.

In service registration and discovery, there is a registration center. When the server starts, it will register the current server information such as service address, communication address, etc. to the registration center in an alias manner . The other party (consumer service provider) uses the alias to obtain the actual service communication address from the registration center, and then implements local RPC calls. The core design idea of ​​the RPC remote calling framework : lies in the registration center, because it is managed by the registration center. A dependency relationship between each service (service governance concept). In any RPC remote framework, there will be a registration center to store service address related information (interface address)


1. Service call

When Eureka serves as the registration center, the service calling process is
Insert image description here
service registration: register the service information into the registration center.

Service discovery: Obtain service information from the registration center

Content: Save key service command and get value closed address

1 First start the eureka registration center

2 Start the service provider service

3. After the service is started, its own information ( the service address is registered in eureka as an alias )

4. When another service needs to call the interface, use the service alias to go to the registration center to obtain the actual RPC remote calling address.

5. After calling the address, the bottom layer actually uses HttpClient technology to implement remote calls.

6. After obtaining the service address, it will be cached in the local jvm memory. By default, the service call address is updated every 30 seconds.

1. Summary of service calls

Registration center (Eureka) port: 7001 Address: http://eureka7001.com:7001/eureka
payment module service: Port: 8001, service name: cloud-payment-service. Register at registration center 7001

  • Need to be configured in the application.yml configuration file (this service information and registration service center address)
server:
  port: 8001
spring:
  application:
    name: cloud-payment-service
...
eureka:
  client:
    #表示是否将自己注册进Eurekaserver默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka #配置注册中心地址
  instance:
    instance-id: payment8001 #添加此处
    prefer-ip-address: true #添加此处 

Consumption service (calling 8001 service): port: 80, service name: cloud-order-service

  1. Configure
server:
  port: 80
spring:
  application:
    name: cloud-order-service

eureka:
  client:
    #表示是否将自己注册进Eurekaserver默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
  1. In the controller,
    Insert image description here
    services 7001 and 8001 are started through RPC calls. The 80
    browser accesses through the 80 service and consumes the 7001 service;
    access address: localhost:80/consumer/payment/xxx

2. Service discovery

For microservices registered in eureka, information about the service can be obtained through service discovery
private DiscoveryClient discoveryClient

org.springframework.cloud.client.discovery public interface
DiscoveryClient extends org.springframework.core.Ordered Maven:
org.springframework.cloud:spring-cloud-commons:2.2.1.RELEASE

1. Inject the bean

 @Resource
 private DiscoveryClient discoveryClient;

2. Call the method to obtain service information

List< String > services = discoveryClient.getServices(); //Get all registered service names List
List< ServiceInstance > instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); //Get service instances based on service names

 @GetMapping(value = "/payment/discovery")
 public Object discovery() {
    
    
      List<String> services = discoveryClient.getServices();
      for (String service : services) {
    
    
          log.info("****** element" + service);
      }
      List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
      for (ServiceInstance instance : instances) {
    
    
          log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());

      }
      return this.discoveryClient;
  }

3. Eureka cluster

Question: What is the core of high availability for microservice RPC remote service calls? Imagine that your registration center has only one. If it fails, the entire service environment will be unavailable.

Solution: Build an Eureka registration center cluster to achieve load balancing + fault tolerance.

Guess you like

Origin blog.csdn.net/weixin_44625361/article/details/129946962
Recommended