ApiBoot Logging supports consolidation of service registries (Eureka, Consul, Nacos Discovery, Zookeeper…) The Logging Client will find the Logging Admin instance of the specified ServiceID in the service registry. The deployment server IP and port number of the Logging Admin are returned through the load balancing policy within SpringCloud Discovery so that the Logging Client can complete the reporting process of the request log.

Blog post: blog.yuqiyu.com/apiboot-log…

Set up Eureka Server

Let’s first set up a Eureka Server, please visit the article “Set up a Service Registry Eureka Server” to see the specific set up process.

Register Logging Admin with Eureka

Since we are using a service registry, we need to modify the Logging Admin in the previous section, add the Eureka client-related dependencies, and add the Eureka Server configuration in the application. Yml configuration file. If you are not familiar with Logging Admin, you can visit “Report logs collected by ApiBoot Logging to Admin” to view the content of the article. There is source code at the bottom of the article.

Add a Eureka Client dependency

We need to register Logging Admin with Eureka Server. For Eureka Server, Logging Admin is a Client role.

We added the following configuration to the pom.xml file:

<! --Eureka Client-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code

Enable the Eureka Client

After adding the dependency, we also need to add @enableDiscoveryClient annotation to the XxxApplication entry class to enable the relevant functions of Eureka Client, as shown below:

@SpringBootApplication
@EnableLoggingAdmin
@EnableDiscoveryClient
public class LoggingAdminApplication {
    /** * logger instance */
    static Logger logger = LoggerFactory.getLogger(LoggingAdminApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(LoggingAdminApplication.class, args);
        logger.info("{} service started successfully."."Log Management Centre"); }}Copy the code

Configure registration to Eureka Server

We add the configuration information for connecting to Eureka Server in the application.yml configuration file as follows:

# Eureka Config
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10000/eureka/
  instance:
    prefer-ip-address: true
Copy the code

Logging Client is registered with Eureka

Logging Client is actually our business service, so don’t be misled by the name. In this chapter, we create a User-Service module as the test business service. We also need to register user-Service as the Client with Eureka Server. See the article “Unified Management of Request Logging using ApiBoot Logging” to create a project.

Add a Eureka Client dependency

Add the following dependencies to the POM.xml configuration file:

<! --Eureka Client-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code

Enable the Eureka Client

After adding the dependency, we also need to enable the Eureak Client, which is an essential step. Add the following to our entry class XxxApplication:

@SpringBootApplication
@EnableDiscoveryClient
@EnableLoggingClient
public class UserServiceApplication {
    /** * logger instance */
    static Logger logger = LoggerFactory.getLogger(UserServiceApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
        logger.info("{} service started successfully."."User"); }}Copy the code

Configure registration to Eureka Server

We add the relevant configuration information of Eureka Server to the application.yml configuration file, as follows:

# Eureka Config
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10000/eureka/
  instance:
    prefer-ip-address: true
Copy the code

Configure the Logging Admin service information

This is the core content of this chapter, we are through the API. In previous boot. Logging. Admin. The server address parameter configure logging admin IP address and service port, In this chapter, we will use the Service registry (Eureka Server) to retrieve the Logging Admin service information from the instance list. ApiBoot Logging API provides a configuration parameter. The boot. Logging. Discovery. The service – to configure Logging id Admin ServiceID, That is the value of the spring.application.name parameter, as shown below:

# ApiBoot Config
api:
  boot:
    logging:
      discovery:
        # Logging Admin ServiceID
        service-id: logging-admin
      show-console-log: true
      format-console-log-json: true
Copy the code

Every time we send a request, the Logging Client will get a list of services with ServiceID = logging-admin from the Eureak Server. After filtering by load balancing, the Logging Client will obtain an available instance information and report the log.

Run the test

We started eureka-server, Logging-admin, and user-service, the three services used in this chapter.

Run the curl command to access the Controller address provided by the user-service, as shown below:

➜ ~ curl http://localhost:9090/test\? Name \=admin Hello: adminCopy the code

We can see the request log information reported by user-service in the logging-admin console, as follows:

Receiving Service: [user-service -> 127.0.0.1], Request Log Report, Logging Content: [ { "endTime":1572921905360, "httpStatus":200, "requestBody":"", "requestHeaders":{ "host":"localhost:9090", "The user-agent" : "curl / 7.64.1", "accept" : "* / *"}, "requestIp" : "0:0:0:0:0:0:1-0," "requestMethod" : "GET", "RequestParam" : "{\" name \ ": \" admin \ "} ", "requestUri" : "/ test", "responseBody" : "hello: Admin ", "responseHeaders":{}, "serviceId":"user-service", "serviceIp":"127.0.0.1", "servicePort":"9090", "spanId":"d97c515f-a147-4f89-9c59-398905c95a73", "startTime":1572921905336, "timeConsuming":24, "traceId":"5e6c0357-1625-4a28-af18-cacdddba146a" } ]Copy the code

Since then we have successfully integrated Eureka with ApiBoot Logging.

Type on the blackboard and underline

ApiBoot Logging provides two ways to obtain information about the Logging Admin service: Service-id and server-address are commonly used. The service-ID mode can be seamlessly integrated with SpringCloud, and link information can be transmitted through Openfeign and RestTemplate. And we’ll talk about that in a later point.

Code sample

If you like this article please click Star for source repository, thanks!! The sample source code for this article can be obtained in the directory Springboot2. x/ APIboot-logging-beaux-Eureka-report-logs:

  • Gitee:Gitee.com/hengboy/spr…

Author’s personal blog uses the open source framework ApiBoot to help you become an Api service architect