SpringBoot之自定义Starter及AutoConfiguration

2023年5月24日11:07:12

现在微服务化是大趋势,因为现在伴随着移动互联网的快速发展,快速上线,快速更新等需求越来越多,所以云平台营运而来,从Docker在到Kubernate,微服务逐渐成为了现代软件开发的新宠。说道微服务,我们就要说一下,微服务需要哪些服务来支撑,为什么选择SpringBoot,微服务化因为服务粒度足够小,所以需要将多个服务进行组合来完成具体的一个业务,但是也伴随着问题而来,服务间的调用依赖,如果服务不可用就会出现问题,如果网络带宽不足,性能也会大打折扣,同时当多个服务依次调用的时候,整个链路异常定位同样很难,现在异步操作随处可见,好不容易业务跑通了,发现配置变更,不可能运行中的所有服务器都更改一下在重启吧,统一的配置管理迫在眉睫,最后关键的服务注册与发现不得不上,你不可能新增一个服务就维护一个IP和端口和URI吧。所以通过刚才的简述,学习过微服务的同学可能就已经能明白微服务需要哪些东西了,需要的东西很多,SpringBoot和SpringCloud提供了一站式解决方案,所以很多东西不需要自己再去实现就可以直接使用,因为微服务的逐步上线及业务拆分是需要一个长期的过程的,毕竟一个优质可靠的框架不是靠一次设计就可以完善的,也不是说SpingCloud就一定是最好的,通过入门,我们之后再使用过程中可以积累经验,再创造适合自己的微服务框架也未尝不可,好了今天就先说说微服务具体都有啥吧。微服务几个核心的部件分别是服务注册与发现,分布式配置管理,消息总线,熔断及降级,服务间调用Feign,性能监控,服务网关等。今天我们先说说微服务最基础的框架SpringBoot。

SpringBoot是一套具有现代思想,为了完全解脱复杂配置而设计的快速开发框架,他提供了很多Enable*注解,只要使用对应的注解就会自动生成相应的配置文件,是不是很神奇,而且SpringBoot使用统一的配置文件application.properties,我们只需要在配置文件中定义必须的参数即可,然后我们就可以去开发我们的业务逻辑撸代码去了,是不是很爽。做过开发有几年的同志一定对开发环境的配置和搭建苦恼不已,往往大家都是更热衷于撸代码搞开发,而不是去搞环境,玩配置。因为环境往往不是天天搭建,而一整套环境搭建起来又非常复杂,所以Spring为了解决在代码中的配置就创造了SpringBoot,SpringBoot中包含了一个包AutoConfiguration,这个包专门用来处理配置相关管理,Spring已经封装了大部分主流的框架,单这还不够,作为一个专业撸代码的,自己做的代码怎么也能自动配置呢?这个很关键啊,是不是,也许有的人说了,提供的工具足够了啊,为什么还要自己开发呢?哈哈,这就说明了你还很嫩,还没有达到一定的高度,这里我不是说我是大牛,只是说明了一个态度,作为一个专业人员,一套框架的核心原理及精髓,我们还是需要去深入学习的,好了,说了半天也该来电干货了,先上代码,在根据代码详细说明。

为了开发自定义的Starter我们需要引入依赖spring-boot-autoconfigure,这个依赖是必须要引入的依赖,其他的依赖可以根据需要进行添加,接下来是spring-boot-configuration-processor,这个依赖也是必须要引入的,但是有个前提,那就是当你需要配置文件的时候@ConfigurationProperties(prefix = "spring.mystarter"),你就必须引入。

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.easy.starter</groupId>
	<artifactId>MyStarter</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
			<version>2.0.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<version>2.0.4.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>MyStarter</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
package com.easy.app.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.mystarter")
public class MyProperties {
	private String msg = "This is a starter Demo!";
	private boolean show = true;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}
}
package com.easy.app.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.easy.app.properties.MyProperties;
import com.easy.app.service.MyService;

@Configuration
@EnableConfigurationProperties(MyProperties.class)
@ConditionalOnClass(MyService.class)
@ConditionalOnProperty(prefix = "mystarter", value = "enabled", matchIfMissing = true)
public class MyConfiguration {
	@Autowired
	private MyProperties myProperties;

	@Bean
	@ConditionalOnMissingBean(MyService.class)
	public MyService helloService() {
		MyService myService = new MyService();
		myService.setMsg(myProperties.getMsg());
		myService.setShow(myProperties.isShow());
		return myService;
	}

}
package com.easy.app.service;

public class MyService {
	private String msg;
	private boolean show = true;

	public String sayHello() {
		return show ? "Hello," + msg : "Hidden";
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setShow(boolean show) {
		this.show = show;
	}
}

 下面的这段代码是开启自动配置的核心配置,是告诉Spring那个类是是自动配置的配置类。在src/main/resources下创建路径/META-INF/spring-factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.easy.app.config.MyConfiguration

 虽然现在有了自动配置,但是我们还是需要在application.properties中配置参数,但是没有提示怎么办,没问题我们同样有办法,同样在src/main/resources下创建路径/META-INF/spring-configuration-metadata.json,配置好之后,我们在使用的时候就会出现配置了。

{
  "hints": [],
  "groups": [
    {
      "sourceType": "com.easy.app.properties.MyProperties",
      "name": "spring.mystarter",
      "type": "com.easy.app.properties.MyProperties"
    }
  ],
  "properties": [
    {
      "sourceType": "com.easy.app.properties.MyProperties",
      "name": "spring.mystarter.msg",
      "type": "java.lang.String"
    },
    {
      "sourceType": "com.easy.app.properties.MyProperties",
      "name": "spring.mystarter.show",
      "type": "java.lang.Boolean"
    }
  ]
}

 

  • 作者:EvanJiemo
  • 原文链接:https://blog.csdn.net/u013560667/article/details/82389761
    更新时间:2023年5月24日11:07:12 ,共 4909 字。