springcloud Greenwich SR4版本笔记
本文只记录实际版本,配置,pom,代码以及注意事项。别的在其他springcloud 的F版本中已有详述。
目录:
jdk: 1.8 springcloud: Greenwich.SR4 springboot: 2.1.9.RELEASE
1.2.1 项目父pom
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> </parent> <modules> <module>finchley-sr4-eureka-server</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
1.2.2 eureka server pom
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
application.yml中配置如下:
server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
eureka server 中起动类代类如下:
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
server: port: 8761 user: huaxia password: huaxia spring: security: user: name: ${user} password: ${password} eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${user}:${password}@{eureka.instance.hostname}:${server.port}/eureka/
配置时修改如下
eureka: client: service-url: defaultZone:http://${user}:${password}@{eureka.instance.hostname}:${server.port}/eureka/
由于默认是开启CSRF,所以需要将其关闭,不然会出现如下错误:
javax.ws.rs.WebApplicationException: com.fasterxml.jackson.databind.exc.MismatchedInputException:
Root name ‘timestamp‘ does not match expected (‘instance‘) for type [simple type, class com.netflix.appinfo.InstanceInfo]
创建一个WebSecurityConfig
类,代码如下:
// WebSecurityConfig.java @EnableWebSecurity @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); //关闭csrf super.configure(http); //开启认证 } }
安全配置参考:https://blog.csdn.net/wgh100817/article/details/101719042
【springcloud】springcloud Greenwich SR4版本笔记
原文:https://www.cnblogs.com/wjqhuaxia/p/11979971.html