温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在中springcloud中对pom.xml进行配置

发布时间:2020-12-18 14:19:38 来源:亿速云 阅读:339 作者:Leah 栏目:开发技术

怎么在中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;

 }
}

关于怎么在中springcloud中对pom.xml进行配置问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI