项目github地址:https://github.com/SpanishSoap/spring-cloud-example,本章用到的项目模块,见下图
Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。
Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud cli等项目。
由于Spring Cloud为服务治理做了一层抽象接口,所以在Spring Cloud应用中可以支持多种不同的服务治理框架,比如:Netflix Eureka、Consul、Zookeeper等,本文主要介绍单机版的Eureka,下章介绍高可用的版本。
spring Cloud使用 Finchley.SR3
spring boot使用 2.0.4.RELEASE
官方文档传送门:
https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-eureka-server.html spring cloud注册中心文档
https://spring.io/guides/gs/service-registration-and-discovery/#initial spring cloud 官方例子
本系列所用的都是maven的父子工程架构,父工程pom
4.0.0
org.friends
centralpark
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
eureka-server-standalone
eureka-client
1.8
Finchley.SR3
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
子工程pom引入netflix的 eureka-server包
4.0.0
org.friends
centralpark
1.0-SNAPSHOT
com.friends
eureka-server-standalone
0.0.1-SNAPSHOT
eureka-server-standalone
Demo project for Spring Cloud
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
在启动类上加入注解 @EnableEurekaServer 如下
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerStandaloneApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerStandaloneApplication.class, args);
}
}
application.yml配置文件中 因为EurekaServer作为服务端的同时也会作为客户端进行注册,所有在单机版关闭他的默认注册行为将registerWithEureka和fetchRegistry设置为false,将serviceUrl指向自己
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: EurekaServerStandalone
启动访问localhsot:8761见如下管理界面,代表启动成功
官方文档传送门:https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi__service_discovery_eureka_clients.html
客户端pom文件
4.0.0
com.friends
eureka-client
0.0.1-SNAPSHOT
eureka-client
Demo project for Spring Cloud
org.friends
centralpark
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
在启动类上加入注解 @EnableDiscoveryClient如下
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
application.yml配置文件中,进行多客户端配置,注册都上述eureka server上
spring:
profiles:
active: EurekaClient8001
---
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8000
spring:
application:
name: EurekaClient8000
profiles: EurekaClient8000
---
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 800
spring:
application:
name: EurekaClient8001
profiles: EurekaClient8001
启动访问localhsot:8761见下图,刚启动的客户端服务注册成功
上一篇:Spring Cloud版本信息一网打尽 https://blog.csdn.net/NDKHBWH/article/details/94437354
下一篇:Spring Cloud注册中心,Eureka集群,高可用版本(Finchley版本)https://blog.csdn.net/NDKHBWH/article/details/94734211