The project tony worked on is now in a painful change: from the old monolithic application to new style microservice model. The leader gives him a job to do the new logging system, from logging the log, log collection, to log analyzing.
Log in Monolithic Applications
“We attach great importance to the logging system, do you understand why?” Leader asked.
“Yep, the log can be used to debug the application in production environment in which we can’t set breakpoints. It can also be used to monitor the application status about the its health. We can even do the intrusion detection and related security by log.” Tony answered.
"Yes, you are right. In a monolithic application, we package all functionality into a single Jar
/War
, then deploy it using application server like Tomcat
or even Glassfish
. It has some disadvantages like bad scalability, small changes leading to whole application re-deployment etc. But it also some convenience in logging:
- The configuration is centralized – log configuration is centralized in those
Jar
; - The logging result is centralized – logging result is in somewhere of server;
- The log is naturally grouped by request;
“So, in our new microservice application, we still need those functionality.” Leader said.
“Em, the first two requirement is easy to understand. What’s meaning of the third one?”
“Saying we have a user request into the monolithic application, the log content in the invocation chain is grouped by their invocation order. We can easily tell how the request is processed if we have fine-tuned log. But in microservice, the log is decentralized in different services. We need use the new logging system to group them together.” Leader added.
“Ok, I understand.”
Log with Spring Cloud
“Considering our team using the Spring Cloud as the microservice framework, we need to see how to centralize logging config in Spring.” Tony said to himself.
After some searching, tony found the Spring Cloud Config module.
"First, we need to add dependency to setup Spring Cloud Config server:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
"Then, we EnableConfigServer
using annotation:
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] arguments) {
SpringApplication.run(ConfigServer.class, arguments);
}
}
"Next, we can config the where are our config file stores. In our case, we use git-based repo:
server.port=8888
spring.cloud.config.server.git.uri=ssh://localhost/config-repo
spring.cloud.config.server.git.clone-on-start=true
"And If we have a client with proper dependency spring-cloud-starter-config
and config files are positioned according to the following rule, config server will return the config files according to url:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
where “application” is the spring.config.name
in the SpringApplication
, “profile” is an active profile (or comma-separated list of profiles), “label” is an optional git branch label (defaults to “master”.)" Tony thought when writing the demo.
Share Common Spring Config
“In above method, we store different applications’ config in different files. How to share common configs across all of the applications?” Tony wonder
After some google, tony find that application.[properties|yml]
is shared across all applications using config server. So adding the common config in application.[properties|yml]
is done.
Share Plain Text Config
"But in our case, we want to share logback config because we use some logback specific config which can’t be configurated in application.yml
but in logback.xml
. How to achieve it? Just adding logback.xml
in root folder seems doesn’t work1.
“It seems that there exists no other way. I can only do the following workaround in shared application.yml
. When Spring Boot download the shared config from config server, it can then retrieve log config by itself.” Tony thought.
logging:
config: http://host:port/path/logback.xml
“Oh, it’s too late. I just finish the first requirement of config centralization. I need to do others tomorrow.” Tony notice the time then go to the bed.
Ref
Written with StackEdit.
According to the official document of Spring Cloud Config, it should work. But as far as we have tried, it doesn’t work :(. ↩︎
评论
发表评论