server:
port: 7001
spring:
application:
name: cloud-eureka-service
复制代码
eureka:
instance:
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与eureka server交互的地址查询服务和注册服务都依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码
[](https://gitee.com/vip204888/java-p7)4\. 测试
-------------------------------------------------------------------------
![](https://static001.geekbang.org/infoq/60/60d579e8f017c8fafaeeae6ebd86dc46.png)
[](https://gitee.com/vip204888/java-p7)三、Eureka Server集群部署
======================================================================================
[](https://gitee.com/vip204888/java-p7)1\. pom依赖引用(和单机版一致)
--------------------------------------------------------------------------------------
复制代码
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--web相关-->
<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>
复制代码
[](https://gitee.com/vip204888/java-p7)2\. 配置启动类
----------------------------------------------------------------------------
复制代码
//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);
}
复制代码
}
[](https://gitee.com/vip204888/java-p7)3\. yml配置
----------------------------------------------------------------------------
复制代码
7001
server:
port: 7001
spring:
application:
name: cloud-eureka-service
复制代码
eureka:
instance:
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:
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与eureka server交互的地址查询服务和注册服务都依赖这个地址
defaultZone: http://eureka7001:7001/eureka/
复制代码
[](https://gitee.com/vip204888/java-p7)4\. 测试
-------------------------------------------------------------------------
![](https://static001.geekbang.org/infoq/33/330c92e2ba211e35573a29e4f8462a74.png)
![](https://static001.geekbang.org/infoq/c6/c676698b7f873386796861c319316201.png)
[](https://gitee.com/vip204888/java-p7)四、Eureka client部署(服务消费者和服务提供者)
=================================================================================================
[](https://gitee.com/vip204888/java-p7)1\. pom依赖引入
------------------------------------------------------------------------------
复制代码
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--web相关-->
<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>
复制代码
[](https://gitee.com/vip204888/java-p7)2\. 配置主启动类
-----------------------------------------------------------------------------
复制代码
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);
}
复制代码
}
[](https://gitee.com/vip204888/java-p7)3\. yml配置
----------------------------------------------------------------------------
复制代码
server:
port: 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
instance-id: payment-8888
复制代码
[](https://gitee.com/vip204888/java-p7)4\. 测试
-------------------------------------------------------------------------
![](https://static001.geekbang.org/infoq/0e/0e378281f596ec3c150384cb472767f1.png)
[](https://gitee.com/vip204888/java-p7)五、配置actuator信息
=================================================================================
复制代码
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka
复制代码
最后
小编精心为大家准备了一手资料
**点击这里免费领取**以上 Java 高级架构资料、源码、笔记、视频。Dubbo、Redis、设计模式、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术
【附】架构书籍赠送
BAT 面试的 20 道高频数据库问题解析
Java 面试宝典
Netty 实战
算法
BATJ 面试要点及 Java 架构师进阶资料
评论