1.首先来建一个Eureka server
然后是配置文件application.yml
server:
port : 9993
ip: localhost
#注册中心配置
eureka:
instance:
hostname : localhost
client:
registerWithEureka : false #属性表示是否将自己注册到Eureka Server, 默认为true。 由于当前应用就是Eureka Server, 因此设为 false;
fetchRegistry : false #表示是否从Eureka Server获取注册信息,默认为true。 如果这是一个单点的 Eureka Server,不需要同步其他节点的数据,可以设为false。
serviceUrl:
# defaultZone: http://eureka-server-peer52:8761/eureka/
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enableSelfPreservation: false # 测试时关闭自我保护机制,保证不可用服务及时踢出
evictionIntervalTimerInMs: 4000 #清理间隔(单位毫秒,默认是60*1000)
然后再启动类上面加上注解@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
然后启动,就可以用http://localhost:9993/去访问了,下面是还没有client的样子
接下我们来建一个client
application.yml
server:
port: 8888
eureka:
client:
serviceUrl:
defaultZone : http://localhost:9993/eureka/
spring:
application:
name : demo3
启动类加上注解@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class Demo3Application {
public static void main(String[] args) {
SpringApplication.run(Demo3Application.class, args);
}
}
到这么,本以为都ok了,但是,我启动报错了。
Completed shut down of DiscoveryClient
ERROR 4376 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The Tomcat connector configured to listen on port 8888 failed to start. The port may already be in use or the connector may be misconfigured.
Action:
Verify the connector's configuration, identify and stop any process that's listening on port 8888, or configure this application to listen on another port.
百度到时说要在pom文件中加载
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
说是缺失启动容器。参考链接:https://blog.csdn.net/CodeFarmer_/article/details/80592388(评论里面别人问原因)
然后启动,哦了,