Config 工程创建之后,Eureka就可以使用Git上的配置启动服务了。
Git 服务器的搭建这里就不细说了(自行解决),下面是我上传再git的配置文件:
创建EurekaServer项目(eureka-server):
pom.xml
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies>
配置启动类:
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
bootstrap.yml
spring: application: name: eureka-server cloud: config: uri: http://127.0.0.1:9090
这里Eureka配置开启了验证功能,spring security默认开启csrf校验,服务之间通讯并没有web页面,所以需要关闭:
package com.eureka.server.util; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { super.configure(http); http.csrf().disable(); } }
使用Git中的配置文件启动服务:
需要使用--spring.profiles.active=peer2命令,peer2 是config中的profile