亲宝软件园·资讯

展开

springcloud pom.xml完整配置详解

zisuu 人气:0
这篇文章主要介绍了springcloud pom.xml完整配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1、父工程

<packaging>pom</packaging>

 <name>Maven</name>
 <!-- FIXME change it to the project's website -->
 <url>http://maven.apache.org/</url>
 <inceptionYear>2001</inceptionYear>

 <distributionManagement>
  <site>
   <id>website</id>
   <url>scp://webhost.company.com/www/website</url>
  </site>
 </distributionManagement>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>12</maven.compiler.source>
  <maven.compiler.target>12</maven.compiler.target>
  <junit.version>4.12</junit.version>
  <lombok.version>1.18.10</lombok.version>
  <log4j.version>1.2.17</log4j.version>
  <mysql.version>8.0.18</mysql.version>
  <druid.version>1.1.16</druid.version>
  <mybatis.spring.boot.version>2.1.1</mybatis.spring.boot.version>
 </properties>

 <dependencyManagement>
  <dependencies>
   <!--父工程引入spring cloud alibaba 2.1.0.RELEASE-->
   <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.1.0.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>

   <dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-project-info-reports-plugin</artifactId>
    <version>3.0.0</version>
   </dependency>
   <!--spring boot 2.2.2-->
    <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-gateway</artifactId>
   </dependency>
   <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.2.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
   <!--spring cloud Hoxton.SR1-->
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Hoxton.SR1</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
   <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.1.0.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
   <!--mysql-->
   <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql.version}</version>
    <scope>runtime</scope>
   </dependency>
   <!-- druid-->
   <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>${druid.version}</version>
   </dependency>
   <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis.spring.boot.version}</version>
   </dependency>
   <!--junit-->
   <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
   </dependency>
   <!--log4j-->
   <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>${log4j.version}</version>
   </dependency>
  </dependencies>

 </dependencyManagement>
 <!--maven插件-->
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
     <fork>true</fork>
     <addResources>true</addResources>
    </configuration><!---->
   </plugin>
  </plugins>
 </build>

2、子工程

 <dependencies>
  <dependency>
   <groupId>org.example</groupId>
   <artifactId>common-service</artifactId>
   <version>${project.version}</version>
  </dependency>
  <dependency>
   <groupId>com.aliyun.oss</groupId>
   <artifactId>aliyun-sdk-oss</artifactId>
   <version>2.8.2</version>
  </dependency>
  <dependency>

   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper-spring-boot-starter</artifactId>
   <version>1.2.10</version>

  </dependency>
  <dependency>
   <groupId>commons-lang</groupId>
   <artifactId>commons-lang</artifactId>
   <version>2.6</version>
  </dependency>
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
  </dependency>
  <dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-httpclient</artifactId>
  </dependency>
  <dependency>
   <groupId>com.alibaba.csp</groupId>
   <artifactId>sentinel-datasource-nacos</artifactId>
  </dependency>
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2</version>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  <dependency>
   <groupId>org.example</groupId>
   <artifactId>common</artifactId>
   <version>${project.version}</version>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.18</version>
  </dependency>
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-core</artifactId>
   <version>2.9.0</version>
  </dependency>
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-annotations</artifactId>
   <version>2.9.0</version>
  </dependency>
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.9.0</version>
  </dependency>
  <dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  </dependency>
  <dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
  </dependency>
  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-websocket</artifactId>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.41</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
     <exclusion>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
     </exclusion>
     <exclusion>
      <groupId>io.lettuce</groupId>
      <artifactId>lettuce-core</artifactId>
     </exclusion>
    </exclusions>
   </dependency>
   <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
   </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
  </dependency>
 </dependencies>

3、swapper配置

package main.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
 @Bean
 public Docket createRestApi() {
  return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .select()
    .apis(RequestHandlerSelectors.basePackage("main.controller"))
    .paths(PathSelectors.any())
    .build();

 }

 //配置在线文档的基本信息
 private ApiInfo apiInfo() {
  return new ApiInfoBuilder()
    .title("springboot利用swagger构建api文档")
    .description("简单优雅的restfun风格,https://me.csdn.net/blog/miachen520")
    .termsOfServiceUrl("https://me.csdn.net/blog/miachen520")
    .version("1.0")
    .build();

 }
}

4、跨域配置

package main.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class webConfig implements WebMvcConfigurer {

 @Override
 public void addCorsMappings(CorsRegistry registry) {
  // 设置允许跨域的路径
  registry.addMapping("/**")
    // 设置允许跨域请求的域名
    .allowedOrigins("*")
    // 是否允许证书
    .allowCredentials(true)
    .allowedMethods("*")
    .maxAge(3600);
 }
}

5、Redis序列化

serializer:

package main.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.Charset;
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
 public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
 private Class<T> clazz;
 public FastJsonRedisSerializer(Class<T> clazz) {

  super();

  this.clazz = clazz;

 }

 @Override
 public byte[] serialize(T t) throws SerializationException {

  if (null == t) {

   return new byte[0];

  }

  return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);

 }

 @Override
 public T deserialize(byte[] bytes) throws SerializationException {

  if (null == bytes || bytes.length <= 0) {

   return null;

  }

  String str = new String(bytes, DEFAULT_CHARSET);
  ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
  return (T) JSON.parseObject(str, clazz);
 }
}

config:

package main.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration

@ConditionalOnClass(RedisOperations.class)

@EnableConfigurationProperties(RedisProperties.class)

public class redisConfig {
 @Bean

 @ConditionalOnMissingBean(name = "redisTemplate")

 public RedisTemplate<Object, Object> redisTemplate(

   RedisConnectionFactory redisConnectionFactory) {

  RedisTemplate<Object, Object> template = new RedisTemplate<>();

  //使用fastjson序列化

  FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);

  // value值的序列化采用fastJsonRedisSerializer

  template.setValueSerializer(fastJsonRedisSerializer);

  template.setHashValueSerializer(fastJsonRedisSerializer);

  // key的序列化采用StringRedisSerializer

  template.setKeySerializer(new StringRedisSerializer());

  template.setHashKeySerializer(new StringRedisSerializer());

  template.setConnectionFactory(redisConnectionFactory);

  return template;

 }

 @Bean
 @ConditionalOnMissingBean(StringRedisTemplate.class)

 public StringRedisTemplate stringRedisTemplate(

   RedisConnectionFactory redisConnectionFactory) {

  StringRedisTemplate template = new StringRedisTemplate();

  template.setConnectionFactory(redisConnectionFactory);

  return template;

 }
}

加载全部内容

相关教程
猜你喜欢
用户评论