1、搭建Eureka服务器
新建一个父项目spring-cloud,依赖如下
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.taikang.bd</groupId>
<artifactId>spring-cloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-server</module>
<module>my-demo</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在父项目spring-cloud上新建一个模块eureka-server,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.taikang.bd</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<dependencies>
<!--eureka server的核心依赖,服务注册和发现-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--引入安全的依赖,访问注册服务都需要输入用户名和密码-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>
新建启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApi {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApi.class,args);
}
}
application.yml
server:
port: 8761
spring:
application:
name: eureka-server
security:
user:
name: admin
password: admin
eureka:
client:
serviceUrl:
defaultZone: http://admin:admin@192.168.11.111:8761/eureka/,http://admin:admin@192.168.11.112:8761/eureka/
register-with-eureka: false
fetch-registry: false #作为服务发现和注册,不去检索服务
这样配置完成后,发现client端注册不上,是因为新版的security默认开启csrf了,加上配置关闭即可
@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(); //开启认证
}
}
参考 https://blog.csdn.net/weixin_39913200/article/details/80845867
---
eureka作为服务发现注册中心,搭建集群时,服务其实只注册到一个eureka,eureka就会通知其他的eureka,为了高可用,还需都注册一下
server:
port:
spring:
application:
name:
eureka:
client:
serviceUrl:
defaultZone:
instance:
prefer-ip-address: true #没有这行你休想访问不同电脑的swagger接口
management:
security:
enabled: false
# 指定此实例的ip
eureka.instance.ip-address=
# 注册时使用ip而不是主机名
eureka.instance.prefer-ip-address=true
参考https://blog.csdn.net/neosmith/article/details/53126924
# 如果项目配置有 server.servlet.context-path 属性,想要被 spring boot admin 监控,就要配置以下属性
instance:
metadata-map:
management:
context-path: /gateway/actuator
health-check-url: http://localhost:${server.port}/gateway/actuator/health
status-page-url: http://localhost:${server.port}/gateway/actuator/info
home-page-url: http://localhost:${server.port}/