—、Eureka Server服务的搭建
1、pom中添加依赖
<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-test</artifactId>
<scope>test</scope>
</dependency>
2、添加启动代码中添加@EnableEurekaServer
注解
3、配置文件
在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,在application.properties
添加以下配置:
spring.application.name=spring-cloud-eureka
server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
4、启动工程后,访问:http://localhost:8000/, 这样一个最基础的eureka服务就可以启动起来了,但是在分布式系统中,一个注册服务中心未免太少了,如果只有一个注册中心,那么如果这个注册中心挂了,那么整个系统就会崩掉,所以为了防止这个情况,我们可以通过集群的方式。
5、Eureka Server集群搭建,我这里以3台Eureka Server为例
1)删除application.properties文件,再新建application.yml文件
2) Eureka Server集群是通过服务间的相互调用来实现的,配置如下:
application.yml
---
server:
port: 8001
spring:
profiles: peer1
application:
name: eureka-ha
eureka:
instance:
hostname: peer1
client:
#register-with-eureka和fetch-registry可以不写,默认就为true
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://peer2:8002/eureka/,http://peer3:8003/eureka/
---
server:
port: 8002
spring:
profiles: peer2
application:
name: eureka-ha
eureka:
instance:
hostname: peer2
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://peer1:8001/eureka/,http://peer3:8003/eureka/
---
server:
port: 8003
spring:
profiles: peer3
application:
name: eureka-ha
eureka:
instance:
hostname: peer3
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/
3)还需要去改你本机的hosts文件,如下:
这样一个Eureka Server集群就搭建完毕,然后可以通过jvm启动参数分别启动项目,如下所示:
效果图如下:
这里可以看出,Eureka Server集群就已经搭起来了,这里需要注意下,注册在Eureka Server上面的服务会默认30秒自动向 Eureka Server服务发送心跳,如果心跳停止,那么该服务的服务实例就会显示down。
二、Eureka Client的搭建
1、在这个项目上面新建一个module
2、引入依赖
//新建model必须引入这个web依赖,要不然项目无法启动
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
</dependency>
3、启动类中添加@EnableDiscoveryClient
注解
4、配置文件
spring.application.name=spring-cloud-producer
server.port=8004
eureka.client.serviceUrl.defaultZone=http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
5、服务调用(feign)
1)引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>${spring-cloud-feign}</version>
</dependency>
2)新建TestController
package com.example.producer.test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+",this is first messge";
}
}