溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在中springcloud中對pom.xml進行配置

發(fā)布時間:2020-12-18 14:19:38 來源:億速云 閱讀:339 作者:Leah 欄目:開發(fā)技術

怎么在中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("簡單優(yōu)雅的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;

 }
}

關于怎么在中springcloud中對pom.xml進行配置問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI