一  Eureka单机版

新建子工程->eureka 

pom.xml


<dependencies>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.mengxiangnongfu</groupId>
<artifactId>commons</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>
<!-- 一般通用配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


application.yml


server:
port: 7001
eureka:
instance:
hostname: eureka1.com
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要这个地址
defaultZone: http://127.0.0.1:7001/eureka
spring:
application:
name: eureka


主启动类 注意:这里使用

@EnableEurekaServer注解标识为Eureka服务端


package com.mengxiangnongfu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplicationMain {

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

}



下面将服务注册进Eureka服务中心 以生产者PAYMENT服务为实例

增加依赖


<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>


修改application.yml


server:
port: 80
spring:
application:
name: order
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:7001/eureka #单机版
#defaultZone: http://127.0.0.1:7001/eureka,http://127.0.0.1:7002/eureka #集群版


修改启动类 注意增加:

@EnableEurekaClient标识为一个Eureka的客户端


package com.mengxiangnongfu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class OrderApplicationMain {

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

}



二  Eureka集群版

前言:建立Eureka1 和 Eureka2 分别使用两个端口,因为本地测试为同一台电脑,可以修改

hosts文件映射两个不同的域名


搭建:建立两个子工程 Eureka1 和 Eureka2 pom文件粘贴单机版,配置文件修改如下参考

① eureka2 7002 端口的application.yml 在7002端口下注册到7001 7001注册到7002形成集群,相互注册

name: eureka
server:
port: 7002
eureka:
instance:
hostname: eureka2.com
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要这个地址
defaultZone: http://127.0.0.1:7001/eureka #集群版
spring:
application:
name: eureka

②eureka1 7001端口的application.yml


server:
port: 7001
eureka:
instance:
hostname: eureka1.com
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要这个地址
defaultZone: http://eureka2.com:7002/eureka
spring:
application:
name: eureka


Eureka注册中心的其他代码和单机版相同

在客户端Client方面进行修改 注意在  defaultZone 注册地址为两个Eureka的地址 生产者消费者相同


server:
port: 80
spring:
application:
name: order
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
#defaultZone: http://127.0.0.1:7001/eureka #单机版
defaultZone: http://127.0.0.1:7001/eureka,http://127.0.0.1:7002/eureka #集群版