【springcloud】eureka 服务治理入门
//表示服务注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
server:
port: 7001
spring:
application:
name: cloud-eureka-service
eureka:
instance:
hostname: eureka7001
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与 eureka server 交互的地址查询服务和注册服务都依赖这个地址
defaultZone: http://{server.port}/eureka/
======================================================================================
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
//eureka-7001
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//表示服务注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
//eureka-7002
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//表示服务注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
7001
server:
port: 7001
spring:
application:
name: cloud-eureka-service
eureka:
instance:
hostname: eureka7001
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与 eureka server 交互的地址查询服务和注册服务都依赖这个地址
defaultZone: http://eureka7002:7002/eureka/
7002
server:
port: 7002
spring:
application:
name: cloud-eureka-service
eureka:
instance:
hostname: eureka7002
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与 eureka server 交互的地址查询服务和注册服务都依赖这个地址
defaultZone: http://eureka7001:7001/eureka/
四、Eureka client 部署(服务消费者和服务提供者)
=================================================================================================
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class,args);
}
}
server:
port: 8888
eureka:
cli
ent:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka
instance:
prefer-ip-address: true
instance-id: payment-8888
=================================================================================
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka
instance:
prefer-ip-address: true #显示主机 ip 名称
instance-id: payment-8888 #实例 id
management:
endpoint:
health:
show-details: always #显示详细信息
endpoints:
web:
exposure:
exclude: env #排除 actuator 暴露的接口
include: ["health","info","mappings"] #设置暴露的 actuator 接口
评论