Eureka 分为Server及client
两者的配置均为3步
Eureka Server配置
添加依赖
<properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR4</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
appliction.yml中配置如下内容:
server: port: 8761 spring: application: name: eurekaServer eureka: instance: registerWithEureka: false fetchRegistry: false client: service-url: defaultZone: http://localhost:8761/eureka register-with-eureka: false fetch-registry: false
- Main方法加
@EnableEurekaServer
启动后,输入localhost:8761 即可看到Eruka的界面
注意: 不要输入localhost:8761/eureka
Eruka Client配置
添加依赖
<properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR4</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
配置application.yml
此处defaultZone即为eureka server的路径spring: application: name: eurekaclient eureka: client: serviceUrl: defaultZone: http://106.14.5.63:8761/eureka/
- main方法加上
@EnableEurekaClient
配置需验证登录的eureka server
Eureka server side:
添加spring security依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
application.yml中加入用户名密码
spring: security: basic: enabled: true user: name: user password: password456
csrf配置
新版本的Spring security添加了csrf,所以需要配置取消csrf, 否则会报如下的错误:Root name 'timestamp' does not match expected ('instance') for type [simple type, class com.netflix.appinfo.InstanceInfo]
添加配置:
@EnableWebSecurity @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); //关闭csrf http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //开启认证 }
Eureka Client side:
application.yml中修改url即可:eureka: client: serviceUrl: defaultZone: http://user:password456@localhost:8761/eureka/
重新运行即可。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。