Spring Cloud 简单入门教程 之 Eureka (二)

服务发现是基于微服务架构的关键原则之一。尝试配置每个客户端或某种形式的约定可能非常困难,可以非常脆弱。Netflix服务发现服务器和客户端是Eureka。可以将服务器配置和部署为高可用性,每个服务器将注册服务的状态复制到其他服务器。
简而言之,Eureka是一个服务注册中心,所有的服务都在这里注册
IJ IDEA File->New->Project->Spring ->initializr
Spring Cloud 简单入门教程 之 Eureka (二)_第1张图片
填写Group ,Artifact ->next
Spring Cloud 简单入门教程 之 Eureka (二)_第2张图片
填项目名称->FInish
生成的项目的目录结构如下:
Spring Cloud 简单入门教程 之 Eureka (二)_第3张图片
pom文件如下:



	4.0.0

	com.heyuanjun
	myeurekaserver
	0.0.1-SNAPSHOT
	jar

	myeurekaserver
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.8.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.cloud
			spring-cloud-starter-eureka-server
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Dalston.SR4
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
		

		
			repository.springframework.maven.snapshot
			Spring Framework Maven Snapshot Repository
			http://repo.spring.io/snapshot/
		
	

注意:pom文件的这个属性,


	org.springframework.boot
	spring-boot-starter-parent
	1.5.8.RELEASE
	 

1.5.8.RELEASE这里的版本要和

		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Dalston.SR4
				pom
				import
			
		

这里面的版本对应
Dalston.SR4
快照的远程仓库作如下配置


	
		spring-milestones
		Spring Milestones
		https://repo.spring.io/milestone
	

	
		repository.springframework.maven.snapshot
		Spring Framework Maven Snapshot Repository
		http://repo.spring.io/snapshot/
	

不然有可能有些引用无法引用到
生成的项目中会默认有一个启动类

@EnableEurekaServer
@SpringBootApplication
public class MyeurekaserverApplication {

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







在启动类上加@EnableEurekaServer这个注解,如果这时上述的版本不对,或者快照远程仓库没有起作用,可能无法导入EnableEurekaServer这个注解类,编译就会出错。
项目目录中会有一个application.property文件,可以保留不变,也可以将其重构成application.yml,两者之间解析方式不同,使用方式也不同。这里将application.property修改成application.yml,
文件中加如以下内容:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
    preferIpAddress: true
  client:
    registerWithEureka: false
    fetchRegistry: false
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

注意值前面的占位符
启动项目,浏览器访问localhost:8761
Spring Cloud 简单入门教程 之 Eureka (二)_第4张图片
说明服务注册中心正确启动了,Eureka有很多有用的配置属性,具体见https://springcloud.cc/spring-cloud-dalston.html#_service_discovery_eureka_clients Spring Cloud的中文文档,翻译的还不是很完善,英文好的可以访问英文版的。

各种IT书籍书目及下载链接
https://blog.csdn.net/dh1027/article/details/89327978

你可能感兴趣的:(学习笔记,java,开发)