1.maven搭建父工程
pom.xml中添加springcloud的依赖
2.搭建服务端工程 eureka-server
1.pom.xml中添加eureka server的依赖
2.application.yml中配置端口号等信息
server:
port: 8761
eureka:
instance:
hostname:localhost
client:
register-with-eureka:false 不需要向自己注册和检索
fetch-registry:false
service-url:
default Zone:
Demo: http://${eureka.instance.hostname}:${server.port}/eureka/
3.修改服务端java代码
@SpringbootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
4.应用启动
localhost:8761 可看到eureka的信息面板
3.搭建客户端工程 eureka-user
1.添加maven工程依赖
2.编写配置文件application.yml
server:
port:8000 #
eureka:
instance:
prefer-ip-address:true #显示主机地址ip
client:
server-url:
default Zone: http://localhost:8761/eureka/ #指定eureka服务器地址
spring:
application:
name: eureka-user #指定应用名称
3.修改客户端代码
@SpringbootApplication
@EnableEurekaClient
@RestController
public class EurekaApplication {
@RequstMapping("/test")
public String test() {
return "hello world!";
}
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
启动应用,通过localhost:8761查看client是否注册的服务;
注意eureka server的自我保护机制会触发警告