在Rest微服务实例的基础上,实现一个基于SpringCloud框架的工程项目,如下图所示,
Eureka Server,用于提供服务注册和发现。
provider(服务提供方),将自身注册到Eureka Server,使得服务消费方可以找到。provider本质上是一个Eureka Client。
consumer(服务消费方),从Eureka Server获取注册服务列表,并消费服务。consumer本质上也是一个Eureka Client。
1. 新建Module:子模块server
修改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">
<parent>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>server</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
</project>
在java目录下新建包:com.example.cloud,在该包下新建主类:EurekaServerApplication
,如下所示,
package com.example.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
在resources目录下新建配置文件application.yml
,配置内容如下所示,
server:
port: 7001
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动server子模块,然后浏览器访问 http://localhost:7001/,看到的就是注册中心的信息面板。
当前,注册中心里尚无可用实例,所以No instances available。
2. 将子模块provider注册到Eureka Server
首先,修改pom.xml,主要是添加依赖:spring-cloud-starter-netflix-eureka-client,修改后的内容如下所示。
<?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">
<parent>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
</project>
然后,修改配置文件application.yml,修改后的内容如下。
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
type-aliases-package: com.example.cloud.entities
mapper-locations: classpath:mybatis/mapper/**/*.xml
spring:
application:
name: provider-dept
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloudd001
username: root
password: Changeme_123
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: service-provider
prefer-ip-address: true
其中,eureka.client.service-url.defaultZone
设置为http://localhost:7001/eureka/
,相当于告诉provider,http://localhost:7001/eureka/
是Eureka Server的地址,到这个地址去注册。
最后,在子模块provider的启动类中添加注解@EnableEurekaClient
,这样,服务一启动就会自动注册进Eureka Server。
package com.example.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
}
}
先后启动server模块和provider模块。
在服务注册中心的信息面板上可以看到provider这个微服务,可见它已经注册进了Eureka Server。
3. 子模块provider提供服务发现
首先,修改DeptController
,修改后如下。
package com.example.cloud.controller;
import com.example.cloud.entities.Dept;
import com.example.cloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/dept/add",method = RequestMethod.POST)
public boolean add(@RequestBody Dept dept){
return deptService.add(dept);
}
@RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
public Dept get(@PathVariable("id") Long id){
return deptService.get(id);
}
@RequestMapping(value = "/dept/list",method = RequestMethod.GET)
public List<Dept> list(){
return deptService.findAll();
}
@RequestMapping(value = "/dept/discovery",method = RequestMethod.GET)
public Object discovery(){
List<String> list = client.getServices();
System.out.println("服务列表:"+list);
List<ServiceInstance> instances = client.getInstances("PROVIDER-DEPT");
for(ServiceInstance instance:instances){
System.out.println(instance.getServiceId() + "\t" +
instance.getHost() + "\t" +
instance.getPort() + "\t" +
instance.getUri()
);
}
return this.client;
}
}
主要有如下两点改动:
1)依赖注入DiscoveryClient client
。
2)添加接口/dept/discovery
。
然后,修改启动类ProviderApp
,添加@EnableDiscoveryClient
注解,使得provider子模块对外暴露,提供服务发现能力。
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class DeptProvider8001_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_App.class,args);
}
}
最后,先后启动server模块、provider模块,并访问接口 localhost:8001/dept/discovery。
但是因为依赖jackson-databind、jackson-core版本冲突,导致没有查询到服务。
现在来解决依赖冲突问题,解决方案如下所示,
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.0.4</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
重复最后一步:先后启动server模块、provider模块,然后访问接口 localhost:8001/dept/discovery。
接口返回的信息如下所示:
IEDA控制台打印出如下内容:
服务列表:[provider-dept]
PROVIDER-DEPT 192.168.137.1 8001 http://192.168.137.1:8001
4. 其他
- Eureka的心跳机制
一个Eureka Client启动后,会默认以30s为周期(一个心跳周期)向Eureka Server发送信息请求,告诉Eureka Server,当前Eureka Client还存活着,以完成心跳续约。
如果Eureka Server在多个心跳周期(默认90s)内没有接收到Eureka Client(一个Eureka Client就是一个微服务实例)的心跳,Eureka Server就会注销该实例,即将该实例从服务注册表中删除。 - Eureka的自我保护机制
如果在服务注册中心的信息面板上出现这么一段红色字体:
Emergency! Eureka may be incorrectly claiming instances are up when they’re not.Renewals are lesser than threshold and hence the instances are not being expired just to be safe.
不要紧张!它不是异常,也不是错误,是Eureka进入了自我保护模式。
上面我们提到了Eureka的心跳机制:超过90s,微服务实例仍未向Eureka Server发送信息完成心跳续约,Eureka Server将注销该实例。
但是,如果微服务实例本身是活着的、健康的,是因为其他原因,比如网络故障,导致微服务实例无法和Eureka Server进行通信,导致无法完成心跳续约,这时,Eureka Server是不是不应该义无反顾地将微服务实例注销掉?
为此,Eureka具有自我保护机制。当Eureka Server在短时间内丢失过多Eureka Client时(可能因为网络故障),Eureka Server会进入自我保护模式。一旦进入自我保护模式,Eureka Server会保护服务注册表中的信息,不会删除服务注册表中的数据,也就是不会注销任何微服务实例。当网络恢复后,Eureka Server会自动退出自我保护模式。
Eureka自我保护模式的设计哲学是,宁可保留错误的注册信息,也不会盲目注销任何可能健康的服务实例。
Eureka自我保护模式是应对网络异常的安全保护措施,它使得Eureka集群更加健壮、稳定。