Java 无法使用netflix eureka创建多个实例
我通过docker compose部署我的spring boot微服务项目。我有三个服务,第一个是eureka服务器,第二个是配置服务器,最后一个是客户端服务 当我运行它时,配置服务和客户端服务都注册到一个配置服务名称中 这是我的配置服务器属性 application.ymlJava 无法使用netflix eureka创建多个实例,java,spring-boot,docker,docker-compose,netflix-eureka,Java,Spring Boot,Docker,Docker Compose,Netflix Eureka,我通过docker compose部署我的spring boot微服务项目。我有三个服务,第一个是eureka服务器,第二个是配置服务器,最后一个是客户端服务 当我运行它时,配置服务和客户端服务都注册到一个配置服务名称中 这是我的配置服务器属性 application.yml server: port: 8888 spring: profiles: active: native cloud: config: discovery: en
server:
port: 8888
spring:
profiles:
active: native
cloud:
config:
discovery:
enabled: true
server:
bootstrap: true
native:
search-locations:
classpath: config/organization
spring:
application:
name: CONFIG
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761
server:
wait-time-in-ms-when-sync-empty: 5
eureka:
instance:
preferIpAddress: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/
fetch-registry: true
register-with-eureka: true
spring:
application:
name: OrganizationCSDF
profiles:
active: default
cloud:
config:
enabled: true
bootstrap.yml
server:
port: 8888
spring:
profiles:
active: native
cloud:
config:
discovery:
enabled: true
server:
bootstrap: true
native:
search-locations:
classpath: config/organization
spring:
application:
name: CONFIG
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761
server:
wait-time-in-ms-when-sync-empty: 5
eureka:
instance:
preferIpAddress: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/
fetch-registry: true
register-with-eureka: true
spring:
application:
name: OrganizationCSDF
profiles:
active: default
cloud:
config:
enabled: true
Dockerfile
FROM openjdk:11-jre-slim
RUN apt-get update &&apt-get -y upgrade && apt-get -y install apt-utils && apt-get -y install netcat-openbsd && apt-get -y install curl&& apt-get -y install unzip
RUN mkdir -p /usr/local/configserver
ADD @project.build.finalName@.jar /usr/local/configserver/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
FROM openjdk:11-jre-slim
RUN apt-get update && apt-get -y upgrade && apt-get -y install netcat-openbsd
RUN mkdir -p /usr/local/eurekaserver
ADD @project.build.finalName@.jar /usr/local/eurekaserver/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
FROM openjdk:11-jre-slim
RUN apt-get update && apt-get -y upgrade && apt-get -y install netcat-openbsd
RUN mkdir -p /usr/local/organization
ADD organization-0.0.1-SNAPSHOT.jar /usr/local/organization/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
run.sh
#!/bin/sh
echo "********************************************************"
echo "Waiting for the eureka server to start on port $EUREKASERVER_PORT"
while ! `nc -z eurekaserver $EUREKASERVER_PORT`; do sleep 3; done
echo ">>>>>>>>>> Eureka Server has started"
echo "********************************************************"
echo "Starting Configuration Service with Eureka Endpoint: $EUREKASERVER_URI";
echo "********************************************************"
java -Djava.security.egd=file:/dev./urandom -Deureka.client.serviceUrl.defaultZone=$EUREKASERVER_URI -jar /usr/local/configserver/@project.build.finalName@.jar
#!/bin/sh
echo "********************************************************"
echo "Starting the Eureka Server"
echo "********************************************************"
java -Djava.security.egd=file:/dev/./urandom -jar /usr/local/eurekaserver/@project.build.finalName@.jar
#!/bin/sh
echo "********************************************************"
echo "Waiting for the eureka server to start on port $EUREKASERVER_PORT"
echo "********************************************************"
while ! `nc -z eurekaserver $EUREKASERVER_PORT`; do sleep 3; done
echo "******* Eureka Server has started"
echo "********************************************************"
echo "Waiting for the database server to start on port $DATABASESERVER_PORT"
echo "********************************************************"
while ! `nc -z database $DATABASESERVER_PORT`; do sleep 3; done
echo "******** Database Server has started "
echo "********************************************************"
echo "Waiting for the configuration server to start on port $CONFIGSERVER_PORT"
echo "********************************************************"
while ! `nc -z configserver $CONFIGSERVER_PORT`; do sleep 3; done
echo "******* Configuration Server has started"
echo "********************************************************"
echo "Starting Organization Service "
echo "********************************************************"
java -Djava.security.egd=file:/dev/./urandom -Dserver.port=$SERVER_PORT \
-Deureka.client.serviceUrl.defaultZone=$EUREKASERVER_URI \
-Dspring.cloud.config.uri=$CONFIGSERVER_URI \
-Dspring.profiles.active=$PROFILE \
-jar /usr/local/organization/@project.build.finalName@.jar
配置类
@SpringBootApplication
@EnableConfigServer
public class SkillUpApplication {
public static void main(String[] args) {
SpringApplication.run(SkillUpApplication.class, args);
}
}
@SpringBootApplication
@EnableEurekaServer
public class EurekasvrApplication {
public static void main(String[] args) {
SpringApplication.run(EurekasvrApplication.class, args);
}
}
@SpringBootApplication
@EnableEurekaClient
public class OrganizationApplication {
public static void main(String[] args) {
SpringApplication.run(OrganizationApplication.class, args);
}
}
Eureka服务器属性
application.yml
server:
port: 8888
spring:
profiles:
active: native
cloud:
config:
discovery:
enabled: true
server:
bootstrap: true
native:
search-locations:
classpath: config/organization
spring:
application:
name: CONFIG
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761
server:
wait-time-in-ms-when-sync-empty: 5
eureka:
instance:
preferIpAddress: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/
fetch-registry: true
register-with-eureka: true
spring:
application:
name: OrganizationCSDF
profiles:
active: default
cloud:
config:
enabled: true
Dockerfile
FROM openjdk:11-jre-slim
RUN apt-get update &&apt-get -y upgrade && apt-get -y install apt-utils && apt-get -y install netcat-openbsd && apt-get -y install curl&& apt-get -y install unzip
RUN mkdir -p /usr/local/configserver
ADD @project.build.finalName@.jar /usr/local/configserver/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
FROM openjdk:11-jre-slim
RUN apt-get update && apt-get -y upgrade && apt-get -y install netcat-openbsd
RUN mkdir -p /usr/local/eurekaserver
ADD @project.build.finalName@.jar /usr/local/eurekaserver/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
FROM openjdk:11-jre-slim
RUN apt-get update && apt-get -y upgrade && apt-get -y install netcat-openbsd
RUN mkdir -p /usr/local/organization
ADD organization-0.0.1-SNAPSHOT.jar /usr/local/organization/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
run.sh
#!/bin/sh
echo "********************************************************"
echo "Waiting for the eureka server to start on port $EUREKASERVER_PORT"
while ! `nc -z eurekaserver $EUREKASERVER_PORT`; do sleep 3; done
echo ">>>>>>>>>> Eureka Server has started"
echo "********************************************************"
echo "Starting Configuration Service with Eureka Endpoint: $EUREKASERVER_URI";
echo "********************************************************"
java -Djava.security.egd=file:/dev./urandom -Deureka.client.serviceUrl.defaultZone=$EUREKASERVER_URI -jar /usr/local/configserver/@project.build.finalName@.jar
#!/bin/sh
echo "********************************************************"
echo "Starting the Eureka Server"
echo "********************************************************"
java -Djava.security.egd=file:/dev/./urandom -jar /usr/local/eurekaserver/@project.build.finalName@.jar
#!/bin/sh
echo "********************************************************"
echo "Waiting for the eureka server to start on port $EUREKASERVER_PORT"
echo "********************************************************"
while ! `nc -z eurekaserver $EUREKASERVER_PORT`; do sleep 3; done
echo "******* Eureka Server has started"
echo "********************************************************"
echo "Waiting for the database server to start on port $DATABASESERVER_PORT"
echo "********************************************************"
while ! `nc -z database $DATABASESERVER_PORT`; do sleep 3; done
echo "******** Database Server has started "
echo "********************************************************"
echo "Waiting for the configuration server to start on port $CONFIGSERVER_PORT"
echo "********************************************************"
while ! `nc -z configserver $CONFIGSERVER_PORT`; do sleep 3; done
echo "******* Configuration Server has started"
echo "********************************************************"
echo "Starting Organization Service "
echo "********************************************************"
java -Djava.security.egd=file:/dev/./urandom -Dserver.port=$SERVER_PORT \
-Deureka.client.serviceUrl.defaultZone=$EUREKASERVER_URI \
-Dspring.cloud.config.uri=$CONFIGSERVER_URI \
-Dspring.profiles.active=$PROFILE \
-jar /usr/local/organization/@project.build.finalName@.jar
Eurekaserver类
@SpringBootApplication
@EnableConfigServer
public class SkillUpApplication {
public static void main(String[] args) {
SpringApplication.run(SkillUpApplication.class, args);
}
}
@SpringBootApplication
@EnableEurekaServer
public class EurekasvrApplication {
public static void main(String[] args) {
SpringApplication.run(EurekasvrApplication.class, args);
}
}
@SpringBootApplication
@EnableEurekaClient
public class OrganizationApplication {
public static void main(String[] args) {
SpringApplication.run(OrganizationApplication.class, args);
}
}
客户属性“组织”
application.yml
server:
port: 8888
spring:
profiles:
active: native
cloud:
config:
discovery:
enabled: true
server:
bootstrap: true
native:
search-locations:
classpath: config/organization
spring:
application:
name: CONFIG
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761
server:
wait-time-in-ms-when-sync-empty: 5
eureka:
instance:
preferIpAddress: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/
fetch-registry: true
register-with-eureka: true
spring:
application:
name: OrganizationCSDF
profiles:
active: default
cloud:
config:
enabled: true
bootstrap.yml
server:
port: 8888
spring:
profiles:
active: native
cloud:
config:
discovery:
enabled: true
server:
bootstrap: true
native:
search-locations:
classpath: config/organization
spring:
application:
name: CONFIG
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761
server:
wait-time-in-ms-when-sync-empty: 5
eureka:
instance:
preferIpAddress: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/
fetch-registry: true
register-with-eureka: true
spring:
application:
name: OrganizationCSDF
profiles:
active: default
cloud:
config:
enabled: true
Dockerfile
FROM openjdk:11-jre-slim
RUN apt-get update &&apt-get -y upgrade && apt-get -y install apt-utils && apt-get -y install netcat-openbsd && apt-get -y install curl&& apt-get -y install unzip
RUN mkdir -p /usr/local/configserver
ADD @project.build.finalName@.jar /usr/local/configserver/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
FROM openjdk:11-jre-slim
RUN apt-get update && apt-get -y upgrade && apt-get -y install netcat-openbsd
RUN mkdir -p /usr/local/eurekaserver
ADD @project.build.finalName@.jar /usr/local/eurekaserver/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
FROM openjdk:11-jre-slim
RUN apt-get update && apt-get -y upgrade && apt-get -y install netcat-openbsd
RUN mkdir -p /usr/local/organization
ADD organization-0.0.1-SNAPSHOT.jar /usr/local/organization/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
run.sh
#!/bin/sh
echo "********************************************************"
echo "Waiting for the eureka server to start on port $EUREKASERVER_PORT"
while ! `nc -z eurekaserver $EUREKASERVER_PORT`; do sleep 3; done
echo ">>>>>>>>>> Eureka Server has started"
echo "********************************************************"
echo "Starting Configuration Service with Eureka Endpoint: $EUREKASERVER_URI";
echo "********************************************************"
java -Djava.security.egd=file:/dev./urandom -Deureka.client.serviceUrl.defaultZone=$EUREKASERVER_URI -jar /usr/local/configserver/@project.build.finalName@.jar
#!/bin/sh
echo "********************************************************"
echo "Starting the Eureka Server"
echo "********************************************************"
java -Djava.security.egd=file:/dev/./urandom -jar /usr/local/eurekaserver/@project.build.finalName@.jar
#!/bin/sh
echo "********************************************************"
echo "Waiting for the eureka server to start on port $EUREKASERVER_PORT"
echo "********************************************************"
while ! `nc -z eurekaserver $EUREKASERVER_PORT`; do sleep 3; done
echo "******* Eureka Server has started"
echo "********************************************************"
echo "Waiting for the database server to start on port $DATABASESERVER_PORT"
echo "********************************************************"
while ! `nc -z database $DATABASESERVER_PORT`; do sleep 3; done
echo "******** Database Server has started "
echo "********************************************************"
echo "Waiting for the configuration server to start on port $CONFIGSERVER_PORT"
echo "********************************************************"
while ! `nc -z configserver $CONFIGSERVER_PORT`; do sleep 3; done
echo "******* Configuration Server has started"
echo "********************************************************"
echo "Starting Organization Service "
echo "********************************************************"
java -Djava.security.egd=file:/dev/./urandom -Dserver.port=$SERVER_PORT \
-Deureka.client.serviceUrl.defaultZone=$EUREKASERVER_URI \
-Dspring.cloud.config.uri=$CONFIGSERVER_URI \
-Dspring.profiles.active=$PROFILE \
-jar /usr/local/organization/@project.build.finalName@.jar
组织类
@SpringBootApplication
@EnableConfigServer
public class SkillUpApplication {
public static void main(String[] args) {
SpringApplication.run(SkillUpApplication.class, args);
}
}
@SpringBootApplication
@EnableEurekaServer
public class EurekasvrApplication {
public static void main(String[] args) {
SpringApplication.run(EurekasvrApplication.class, args);
}
}
@SpringBootApplication
@EnableEurekaClient
public class OrganizationApplication {
public static void main(String[] args) {
SpringApplication.run(OrganizationApplication.class, args);
}
}
最后,docker compose.yml)
所以我希望eureka分别注册两个spring引导服务,但不知道如何注册