使用Spring Boot v1.5.x,可以将一个服务同时用作Spring Boot Admin Server和Eureka Discovery Server。使用Spring Boot 2.x,似乎Spring Boot Admin Server使用Spring响应API(webflux,Netty Server等),而Netflix Eureka Discovery Server仍使用Tomcat。我想知道是否可以将Netty用于Eureka Server或以某种方式在一项服务中同时使用Netty和Tomcat。下面的示例代码
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableAdminServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaSpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaSpringBootAdminApplication.class, args);
}
}
下面的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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.foo.bar</groupId>
<artifactId>eureka-springadmin-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-springadmin-service</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.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-boot-admin.version>2.1.1</spring-boot-admin.version>
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
</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>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
更新: 所以我终于把这个工作了!!首先,让我们看一下下面的pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.1</spring-boot-admin.version>
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-reactor-netty</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.8.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</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>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
上面是修改后的pom.xml。进行了两项更改。首先,我添加了spring-boot-starter-webflux
依赖性,然后从spring-boot-starter-reactor-netty
中排除了spring-boot-starter-webflux
依赖性。第二,我从io.projectreactor中引入了反应堆核心和反应堆净值。 SBA 2.1.1依赖于反应式库,特别是HttpClient类。这满足了SBA的所有需求。 Eureka Admin Server已经具备了运行所需的一切。
现在,让我们看一下.properties来配置自我发现和注册表获取。
#PROJECT INFORMATION
spring.application.name= Eureka and SBA Service
info.app.name=@project.name@
info.app.description=@project.description@
info.app.version=@project.version@
#SERVER CONFIGURATION
server.port=10761
server.servlet.context-path=/
#MANAGEMENT CONFIGURATION
management.server.port=10769
management.server.servlet.context-path=/admin
##SPRING SECURITY CONFIGURATION
spring.security.user.name=admin
spring.security.user.password=adminadmin
#EUREKA DISCOVERY CONFIGURATION
eureka.instance.hostname=localhost
eureka.client.registry-fetch-interval-seconds=5
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://admin:adminadmin@localhost:10761/eureka
#SPRING BOOT ADMIN CONFIGURATION
spring.boot.admin.discovery.enabled=true
spring.boot.admin.context-path=/spring-admin/
以上属性文件显示了Eureka和SBA的配置。真正重要的部分是获取注册表,使用eureka注册,eureka.client.service-url.defaultZone,spring.boot.admin.discovery.enabled和spring.boot.admin.context-path。
获取注册表道具不仅允许客户向Eureka进行注册,还可以向SBA进行注册。缺点是该服务将在服务完全启动之前尝试获取注册表。因此,您将在服务启动时看到异常。
向eureka注册,允许该服务向Eureka和SBA注册自己。
eureka.client.service-url.defaultZone 告诉服务要注册的位置。在这种情况下,它就是它自己。该相同的配置需要应用于希望向Eureka和SBA注册的任何客户端服务器。
spring.boot.admin.discovery.enabled 允许对SBA进行云发现。如果将此设置为true,则在注册表提取中SBA将不会注册任何客户端。
spring.boot.admin.context-path 设置SBA的上下文路径。这是必需的,这样您就可以避免和与Eureka发生网址冲突。
在主应用程序类中,确保存在以下批注
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableAdminServer
@EnableDiscoveryClient
@EnableEurekaServer
@SpringBootApplication
public class EurekaSbaApplication{
public static void main(String[] args) {
SpringApplication.run(EurekaSbaApplication.class, args);
}
}
我还添加了一个禁用CSRF的网络安全配置类,但我确定是否确实有必要。
更新:
我忘记提及客户端成功注册后有时引发的异常。引发IllegalStateException,并显示以下消息:“调用assncError()对于具有异步状态的请求无效”。我还没有弄清楚为什么会发生这种情况,但似乎并没有破坏任何东西。
2018-12-05 13:33:05.845 ERROR 19424 --- [io-10761-exec-9] o.a.catalina.connector.CoyoteAdapter : Exception while processing an asynchronous request
java.lang.IllegalStateException: Calling [asyncError()] is not valid for a request with Async state [MUST_DISPATCH]
at org.apache.coyote.AsyncStateMachine.asyncError(AsyncStateMachine.java:440) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:512) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.Request.action(Request.java:430) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.AsyncContextImpl.setErrorState(AsyncContextImpl.java:382) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:239) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProcessor.dispatch(AbstractProcessor.java:241) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.13.jar:9.0.13]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_172]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_172]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.13.jar:9.0.13]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_172]
答案 0 :(得分:1)
可以在依赖项上排除Tomcat并添加Reactor Netty:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
并添加Reactor Netty的依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-reactor-netty</artifactId>
<version>{{version-needed}}</version>
</dependency>