一。Eureka服务器注册
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>Project1</artifactId><version>1.0</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.SR1</spring-cloud.version><springboot.version>2.0.5.RELEASE</springboot.version></properties><dependencies><!--springboot支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!--Eureka服务端支持--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${springboot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>
2.application.yaml
spring:profiles:active: peer0application:name: eureka-server
eureka:instance:hostname: localhostclient:# 当前服务不会从eureka中获取到其他服务地址等信息,用于特殊情况,比如当前服务已经知道要调用的服务的信息# fetch-registry: false#不向eureka注册自己,即当前服务不会注册在eureka,通常用在特殊情况,比如当前服务只作为消费者# register-with-eureka: falseservice-url:# 这里配置两个地址,eureka在启动时会寻找能注册的地址,把自己注册,# 在这里就是注册到本服务的7000端口(这时候eureka已经完成加载,等待服务注册,所以可以注册自己了)# 书上说的互相注册是指本服务分别启动在7001和7000端口,两个eureka服务之间可以共享服务的上线、下线、续约,但是我实现不了,会造成两个的服务都发生错误,比如下线了没有反应defaultZone: http://localhost:7001/eureka,http://localhost:7000/eureka
3.EurekaApplication启动即可
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客户端
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>Project1</artifactId><version>1.0</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.SR1</spring-cloud.version><springboot.version>2.0.5.RELEASE</springboot.version></properties><dependencies><!--springboot支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!--Eureka服务端支持--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${springboot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>
2.application.yaml
spring:profiles:active: peer1application:name: USER-SERVICE #服务名
# eureka地址,去哪里注册服务,这里有两个地址,会向能访问的注册,如果两个都能访问,则两个都注册
# 如果只有一个能访问,则启动时会报错,不会影响能访问的服务
eureka:client:service-url:defaultZone: http://localhost:7001/eureka,http://localhost:7000/eureka#向注册中心中发送本服务信息instance:ip-address: 127.0.0.1
3.UserApplication
@SpringBootApplication
public class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class, args);}
}
启动后,访问eureka地址 localhost:7000: