Spring cloud 开发中遇到的坑

2019-02-26  本文已影响0人  MartinX_c56e

一、版本问题,

    在框架搭建之初一定要统一好spring版本兼容问题,具体可以去spring.io 进入spring cloud 的中有兼容版本介绍,要不然后期各种版本兼容问题会让心态不爽。 

二、跨域问题的处理,每个service都要添加

    1、需要的JAR 

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>4.0.0</version>

</dependency>

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-core-asl</artifactId>

<version>1.9.13</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

</dependency>

2、在启动类里面添加

/**

* @return 跨域过滤器

* @author CRH

* @date 2018/12/5

*/

@Bean

public WebMvcConfigurer corsConfigurer() {

return new WebMvcConfigurerAdapter() {

@Override

public void addCorsMappings(CorsRegistry registry) {

registry.addMapping("/**")

.allowedOrigins("*")

.allowCredentials(true)

.allowedMethods("GET","POST","DELETE","PUT")

.maxAge(3600);

}

};

}

三、Eureka-Server 端的配置

启动类添加

@EnableEurekaServer注解

1、pom文件集成


<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka-server</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

2、application.yml的配置

server:

port: 1111

spring:

application:

name: eureka-service

eureka:

server:

enableSelfPreservation: false

instance:

hostname: localhost

prefer-ip-address: true

#    leaseRenewalIntervalInSeconds: 1

#    leaseExpirationDurationInSeconds: 2

  client:

register-with-eureka: false

fetch-registry: false

service-url:

defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

Eureka-client的配置

启动类添加@EnableDiscoveryClient注解

pom的配置

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

applicaton.yml 配置

配置server时一定要注意两点(每个server)

一、必须配置spring.application.name (网关监听)

二、必须开放应用服务器注册IP地址(用来网关转发)

eureka:

client:

service-url:

defaultZone: http://127.0.0.1:1111/eureka   #Eureka-Server地址以及端口名称

instance:

appname: sm-server   #服务名

prefer-ip-address: true   #IP地址注册服务

session共享问题

使用redis来做session共享,所有的service都配置

pom配置

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.session</groupId>

<artifactId>spring-session-data-redis</artifactId>

</dependency>

appliaction.yml

spring:

redis:

host: 10.0.0.17

database: 8

pool:

max-active: 8

min-idle: 1

max-idle: 1

max-wait: -1

上一篇下一篇

猜你喜欢

热点阅读