当前位置: 首页 > 知识库问答 >
问题:

Spring Boot 2.1.1-Eureka发现和Spring Boot管理服务器

顾宸
2023-03-14

使用Spring Boot V1.5.x,可以将单个服务double用作Spring Boot Admin服务器和Eureka Discovery服务器。对于Spring Boot2.x,Spring Boot Admin Server似乎使用Spring reactive API(webflux,Netty Server等),而Netflix Eureka Discovery Server仍然使用Tomcat。我想知道是否有可能使用Netty为尤里卡服务器或以某种方式在一个服务中同时使用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);
    }
}
<?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>
<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-webflus依赖项,然后从spring-boot-starter-webflus中排除了spring-boot-starter-webflus依赖项。其次,我从io.projectreactor中调入了reactor-core和reactor-netty。SBA 2.1.1依赖于反应型libs,特别是HttpClient类。这满足了SBA的所有需求。尤里卡管理服务器已经具备了运行所需的一切。

现在让我们看一下配置自我发现和注册表获取的。属性。

#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配置。真正重要的部分是fetch-registry、register-with-eureka、eureka.client.service-url.defaultzone、spring.boot.admin.discovery.enabled和spring.boot.admin.context-path。

fetch-registry props不仅允许客户机使用Eureka注册,还允许客户机使用SBA注册。不利的一面是,服务将试图在服务完全启动之前获取注册表。因此,您将看到服务启动时抛出的异常。

register-with-eureka允许服务向Eureka和SBA注册自己。

service-url.DefaultZone告诉要注册的服务的位置。在这种情况下,它是和自己在一起的。同样的配置需要应用于任何希望向Eureka和SBA注册的客户机服务器。

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);
    }
}

更新:

我忘了提到在客户机注册成功后抛出的异常。抛出一个带有以下消息的IllegalStateException:“调用aysncError()对于具有异步状态的请求无效”。我还不明白为什么会发生这种情况,但它似乎没有打破任何东西。

    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]

共有1个答案

满俊楠
2023-03-14

可以排除依赖项上的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>
 类似资料:
  • 如何包含Eureka服务器 要在项目中包含Eureka服务器,请使用组org.springframework.cloud和工件id spring-cloud-starter-eureka-server的启动器。有关 使用当前的Spring Cloud发布列表设置构建系统的详细信息,请参阅Spring Cloud项目页面。 如何运行Eureka服务器 示例eureka服务器; @SpringBoot

  • 准备工作 在生产环境下,我们往往会为每个应用配置一个host,使用host而非IP进行访问。为了更加贴近生产环境,以及后文Docker章节的讲解,我们首先配置一下Host 127.0.0.1 discovery 代码示例 创建一个Maven工程(microservice-discovery-eureka),并在pom.xml中加入如下内容: <?xml version="1.0" encoding

  • 我正在尝试建立一个基于Spring功能的简单微服务架构。我有3个简单的微服务作为起点:1配置服务器(Spring cloud config)2发现服务器(eureka Server)3网关Zuul 我配置了这些服务,以便发现服务器存储所有的.properties文件,它将自己注册到发现服务器,以便所有其他服务检索.properties文件,而不是通过静态url而是通过eureka服务器访问发现。

  • pom.xml 主应用程序类 Application.Properties eureka客户端设置 pom.xml Application.Properties 我在eureka-server仪表板(http://localhost:8761)中没有看到向Eureka server注册的micro-service-currency-exchange-service 为什么eureka客户端没有注册

  • 服务发现是基于微服务架构的关键原则之一。尝试配置每个客户端或某种形式的约定可能非常困难,可以非常脆弱。Netflix服务发现服务器和客户端是Eureka。可以将服务器配置和部署为高可用性,每个服务器将注册服务的状态复制到其他服务器。 如何包含Eureka客户端 要在您的项目中包含Eureka客户端,请使用组org.springframework.cloud和工件ID spring-cloud-st

  • 我有两个微服务, 在localhost:8081上运行的eureka-client-1 运行在localhost:8082上的eureka-client-2 客户端1的application.properties,与客户端2类似(只需更改名称,即eureka-client-2) eureka服务器的Application.Properties