Spring Cloud之Eureka注册中心案例

spring cloud已经帮我实现了服务注册中心,我们只需要很简单的几个步骤就可以完成

1、pom中添加依赖


  	org.springframework.boot
  	spring-boot-starter-parent
  	1.5.6.RELEASE
  	
  
  
  	
	  	
	  		org.springframework.cloud
	  		spring-cloud-dependencies
	  		Dalston.SR4
	  		pom
	  		import
	  	 		
  	
  
  
  
    
      junit
      junit
      test
    
    
		org.springframework.cloud
		spring-cloud-starter
	
	
		org.springframework.cloud
		spring-cloud-starter-eureka-server
	
	
		org.springframework.boot
		spring-boot-starter-test
		test
	
	  
      
        javax.servlet  
        javax.servlet-api  
        provided  
    
  

2、添加启动代码中添加@EnableEurekaServer注解

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudEurekaApplication.class, args);
	}
}

3、配置文件

在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,在application.properties添加以下配置:

spring.application.name=spring-cloud-eureka

server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
  • eureka.client.register-with-eureka :表示是否将自己注册到Eureka Server,默认为true。
  • eureka.client.fetch-registry :表示是否从Eureka Server获取注册信息,默认为true。
  • eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。

启动工程后,访问:http://localhost:8000/,可以看到下面的页面,其中还没有发现任何服务

Spring Cloud之Eureka注册中心案例_第1张图片

你可能感兴趣的:(Spring,Cloud)