搭建服务注册中心:
首先,创建一个基础的spring boot 工程,并在pom文件中添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
通过 @EnableEurekaServer 注解启动一个服务注册中心提供给其他应用进行对话。这一步非常简单,只需要在普通的spring boot应用中添加上这个注解就能开启此功能。
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudDemoApplication.class, args);
}
}
默认设置下,该服务注册中心也会将自己作为客户端来尝试注册自己,所以我们要禁用它的客户端注册行为。只需要在application.yml:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false #代表不想注册中心注册自己
fetchRegistry: false #由于注册中心就是维护实例,所以不需要检索
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
然后跑起来项目: