在上一篇中搭建了eureka服务《springcloud集成Oauth2权限项目-搭建主体vloud项目及eureka注册中心服务(一)》
这一篇搭建一个简易的User服务
创建module 取名叫vcloud-user
user的pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.study</groupId>
<artifactId>vcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.study.vcloud</groupId>
<artifactId>vcloud-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>vcloud-user</name>
<description>用户模块</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主pom中不要忘了加上
user 配置文件application.yml
server:
port: 5000
spring:
application:
name: vcloud-user-server
eureka:
instance:
prefer-ip-address: true
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 20
client:
serviceUrl:
defaultZone: http://root:root@127.0.0.1:1025/eureka/
registry-fetch-interval-seconds: 10
#enabled: false
logging:
level:
com:
study:
vcloud:
user: debug
user 启动类上加上注解@EnableDiscoveryClient ,注册到eureka服务上
依次启动eureka服务
然后启动user服务
你会发现eureka服务启动成功,而user服务启动报错,报一下错误
主要是eureka服务开启了csrf,会导致注册不上。
解决方法,关闭csrf
在eureka服务启动类上加上这段代码
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().httpBasic();
super.configure(http);
}
}
重启eureka服务后就ok
然后登陆到eureka管理页面,127.0.0.1:1025
会多一个我们刚刚创建的微服务,则注册成功