文章目录
服务治理:Spring Cloud Eureka
Eureka服务治理
构建服务注册中心
服务注册:
每个服务单元向注册中心登记自己提供的服务,将主机与端口号、版本号、通信协议等一些附加信息告知注册中心,注册中心按服务名分类组织服务清单。
服务注册中心会以心跳的方式监测清单中的服务是否可用。
服务发现:
服务间的调用通过向服务名发起请求调用而非通过指定具体的实例地址。
服务注册中心构建流程
- 引入依赖坐标
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 开启服务注册功能
@EnableEurekaServer//此注解用于开启服务注册中心
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
- 配置服务注册属性
spring.application.name=eureka-server //服务名称
server.port=1111//服务端口号
eureka.instance.hostname=localhost//服务主机名
eureka.client.register-with-eureka=false//默认true 设置为false代表不向注册中心注册自己(注册中心集群设置true)
eureka.client.fetch-registry=false//默认true 是否检索服务,由于注册中心的职责为维护服务实例故而为false则不拉取提供者服务(注册中心集群设置true)
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ # 服务的url localhost:1111
高可用注册中心
Eureka的服务治理设计中,所有节点既是服务提供方,也是服务消费方,服务注册中心也不例外
Eureka Server的高可用实际上就是将自身作为服务向其他服务注册中心注册自己,这样便可形成一组互相注册的服务注册中心,以实现服务清单的互相同步,达到高可用的效果。
让服务注册中心注册自己 a,b皆有
eureka.client.register-with-eureka=true//默认true
eureka.client.fetch-registry=true//默认true
启动类注解@EnableEurekaServer
注册中心a的配置
spring.application.name=eureka-server # 此处可不同
server.port=1111
eureka.instance.hosthome=a #设置主机名为a 需要在/etc/hosts文件中添加对a和b的转换让配置的host形式serviceUrl能在本地正确访问到;Window系统路径为c;\Window\System32\drivers\etc\hosts 127.0.0.1 a 127.0.0.1 b
eureka.client.serviceUrl.defaultZone=http://b:1112/eureka/ #指定服务注册中心的地址
注册中心b的配置
spring.application.name=eureka-server
server.port=1112
eureka.instance.hosthome=b #设置主机名为b
eureka.client.serviceUrl.defaultZone=http://a:1111/eureka/ #指定服务注册中心的地址
设置了多节点的服务注册中心之后,服务提供方需要配置多个节点的地址
spring.application.name=hello-server
eureka.client.serviceUrl.defaultZone=http://a:1111/eureka/,http://b:1112/eureka/
注意:若不想用主机名定义注册中心地址,亦可使用IP地址的形式,需要在配置文件中增加配置参数
eureka.instance.prefer-ip-address=true//该值默认为false
服务注册与服务发现
常用注解:
@EnableDiscoveryClient:服务发现注解(服务提供者)
@EnableEurekaClient:服务发现注解
@EnableEurekaServer:启动服务注册中心
服务提供者:
- 依赖坐标
只改变这个eureka-server改成eureka
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
- 注入DiscoveryClient对象可以获得服务的相关内容
- 在主类(启动类)添加@EnableDiscoveryClient or @EnableEurekaClient注解,激活Eureka中的DiscoveryClient实现
@EnableEurekaClient和@EnableDiscoveryClient的区别:spring cloud中discovery service有许多种实现(eureka、consul、zookeeper等等)
- @EnableDiscoveryClient基于spring-cloud-commons
- @EnableEurekaClient基于spring-cloud-netflix
总结:就是如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient,
如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient。
- 配置属性
命名服务名称
spring.application.name=hello-service
指定服务注册中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
# 分配端口号
server.port=8081
# eureka注册中心每个服务的status显示ip地址的设置
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
注意:springboot2.0则为instance-id: ${spring.cloud.client.ip-address}:${server.port}
注意:以下可以获得服务实例
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private Registration registration;
List<ServiceInstance> instances = discoveryClient.getInstances(registration.getServiceId());// 获取当前本地服务的实例
if (instances != null && instances.size() > 0) {
for (ServiceInstance instance : instances) {
if (instance.getPort() == 8081) {
hostName = instance.getHost();
serviceId = instance.getServiceId()
// host:196.xx.xx.xx service_id:USER 获得USER服务的实例
}
}
}
服务消费者:
服务消费者有两个目标,发现服务以及消费服务
- Ribbon
- Feign
很多时候,客户端既是服务提供者也是服务消费者
Ribbon:
Ribbon是一个基于HTTP和TCP的客户端负载均衡器,它可以通过客户端中配置的ribbonServerList服务端列表取轮询访问以达到均衡负载的作用
导入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
在应用主类Application上添加注解:
// @EnableDiscoveryClient注解让该应用为Eureka客户端应用,获得服务发现的能力
// 在主类创建RestTemplate的SpringBean实例,通过@LoadBalanced注解开启客户端负载均衡
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
Controller类的接口的调用:注意,访问的地址是服务名USER不是一个具体的地址
@RestController
@RequestMapping("/ribbon")
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/consumer")
public String helloConsumer() {
return restTemplate.getForEntity("http://USER/user/hello", String.class).getBody();
}
}
配置文件:
spring.application.name=ribbon-consumer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/,http://localhost:1113/eureka/
# eureka注册中心每个服务的status显示ip地址的设置
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddr