标签:ide public disco 自己 基础上 应用 dea 设置 开源
本次学习基于spring cloud Greenwich SR1 版本
Spring Boot/Spring Cloud应用开发套路
Eureka是Netflix开源的服务发现组件,本身是一个基于REST的服务,包含Server和Client两部分,Spring Cloud将它集成在子项目Spring Cloud Netflix中
遵循开发套路
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
server:
port: 8001
spring:
application:
name: microservice-discovery-eureka
eureka:
client:
service-url:
#erueka server的地址,记住/eureka不要掉了
defaultZone: http://localhost:8001/eureka
# 是否从eureka server注册,这里我们选择false
fetch-registry: false
# 是否从eureka server 中拉取服务
register-with-eureka: false
启动项目,访问http://localhost:8001/
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
@SpringBootApplication
@EnableDiscoveryClient
public class ProvideApplication {
public static void main(String[] args) {
SpringApplication.run(ProvideApplication.class, args);
}
}
在Greenwich SR1版本中可以省略@EnableEurekaClient和@EnableDiscoveryClient注解,但为了养成好习惯,建议加上相应注解
@EnableDiscoveryClient: 可以配合不同的服务发现server 使用
@EnableEurekaClient: 只能配合 Eureka server 使用
server:
port: 9001
spring:
application:
name: microservice-provide-user
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud-study?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&allowMultiQueries=true
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
instance:
# 是否显示ip,如果不设置那么就会显示主机名,默认false
prefer-ip-address: true
启动项目,可以发现项目已经被注册进eureka
完整代码:
### eureka 深入
Eureka包含两个组件:Eureka Server 和 Eureka Client:
30s
,如果server在一定时间内没有收到client的心跳,那么server会注销实例90s
在host中添加
127.0.0.1 peer1 peer2
可以再microservice-discovery-eureka
的基础上修改application.yml
spring:
application:
name: microservice-discovery-eureka-cluster
eureka:
client:
serviceUrl:
defaultZone: http://peer2:8002/eureka/,http://peer1:8003/eureka/
---
spring:
profiles: peer1
server:
port: 8002
eureka:
instance:
hostname: peer1
---
spring:
profiles: peer2
server:
port: 8003
eureka:
instance:
hostname: peer2
修改microservice-provide-user
的application.yml
server:
port: 9002
spring:
application:
name: microservice-provide-user
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud-study?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&allowMultiQueries=true
username: root
password: root
jpa:
show-sql: true
eureka:
client:
service-url:
defaultZone: http://peer1:8002/eureka/,http://peer2:8003/eureka/
instance:
prefer-ip-address: true
启动服务
spring cloud(Greenwich SR)- Eureka
标签:ide public disco 自己 基础上 应用 dea 设置 开源
原文地址:https://www.cnblogs.com/amberbar/p/10995395.html