一、 单机版Eureka Server
1. pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. application.properties配置
server.port=9081
#服务注册中心实例的主机名
eureka.instance.hostname=localhost
# 设置spring应用命名,非必要
spring.application.name=eureka-server
# 是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
# 是否从eureka中获取服务注册信息,默认为true
eureka.client.fetch-registry=false
# 服务注册中心的配置内容,指定服务注册中心的位置
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
3. 启动类加@EnableEurekaServer 注解来开启eureka服务
Eureka Client
Application Service 服务提供方,注册到Eureka Server中的服务
1. pom文件同server
2. application配置文件
server.port=9082
# 设置spring应用命名,非必要
spring.application.name=client1
eureka.client.service-url.defaultZone=http://localhost:9081/eureka/
3. 启动类加@EnableEurekaClient 注解
4. 添加服务
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam(value = "name") String name) {
return "hello " + name;
}
}
Application Client 服务消费方,通过Eureka Server发现服务,并消费
1. pom文件添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2. application配置文件
server.port=9083
# 设置spring应用命名,非必要
spring.application.name=client2
eureka.client.service-url.defaultZone=http://localhost:9081/eureka/
3. 启动类加@EnableFeignClients 注解
4. Remote接口
@FeignClient(name = "client1")
public interface HelloRemote {
@RequestMapping("/hello")
String getRemoteName(@RequestParam(value = "name") String name);
}
5. Controller调用
@RestController
public class HelloController {
@Autowired
HelloRemote helloRemote;
@RequestMapping("/hello")
public String hello(@RequestParam(value = "name") String name) {
return helloRemote.getRemoteName(name);
}
}
浏览器调用 http://localhost:9083/hello?name=eureka
页面输出: hello eureka
二、 集群版Eureka Server
配置hosts(C:\Windows\System32\drivers\etc目录下)
127.0.0.1 localhost
127.0.0.1 eureka-service1
127.0.0.1 eureka-service2
127.0.0.1 eureka-service3
不配hosts文件搭建集群并不会出错,但是注册服务时会出错,无法识别域名错误
搭建三个Eureka Server 配置文件eureka.client.service-url.defaultZone 互相注册
server.port=9081
#服务注册中心实例的主机名
eureka.instance.hostname=eureka-service1
# 设置spring应用命名,非必要
spring.application.name=eureka
# 是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
# 是否从eureka中获取服务注册信息,默认为true
eureka.client.fetch-registry=false
# 服务注册中心的配置内容,指定服务注册中心的位置
eureka.client.service-url.defaultZone=http://eureka-service2:9084/eureka/,http://eureka-service3:9085/eureka/
server.port=9084
#服务注册中心实例的主机名
eureka.instance.hostname=eureka-service2
# 设置spring应用命名,非必要
spring.application.name=eureka1
# 是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
# 是否从eureka中获取服务注册信息,默认为true
eureka.client.fetch-registry=false
# 服务注册中心的配置内容,指定服务注册中心的位置
eureka.client.service-url.defaultZone=http://eureka-service1:9081/eureka/,http://eureka-service3:9085/eureka/
server.port=9085
#服务注册中心实例的主机名
eureka.instance.hostname=eureka-service3
# 设置spring应用命名,非必要
spring.application.name=eureka2
# 是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
# 是否从eureka中获取服务注册信息,默认为true
eureka.client.fetch-registry=false
# 服务注册中心的配置内容,指定服务注册中心的位置
eureka.client.service-url.defaultZone=http://eureka-service1:9081/eureka/,http://eureka-service2:9084/eureka/
浏览器效果
No active profile set, falling back to default profiles: default
Tomcat initialized with port(s): 8080 (http) 客户端启动一直不通过server.port=9082然后一连串问题com.netflix.discovery.shared.transport.TransportException: Cannot enecute request on any know server 。。。找了一天问题。。。百度的方法都用了,,,没有一个能解决。。。
后来我把client1服务的application.properties改了个名字,,application-client1.properties然后 太阳出来了,,,
然后我又改回去,,,也没复现之前的错,,,天杀的。
服务提供方和消费方修改一个地方
eureka.client.service-url.defaultZone=http://eureka-service1:9084/eureka/,http://eureka-service1:9081/eureka/,http://eureka-service1:9085/eureka/