Eureka:就是服务注册中心(可以是一个集群),对外暴露自己的地址
提供者:启动后向Eureka注册自己信息(地址,提供什么服务)
消费者:向Eureka订阅服务,Eureka会将对应服务的所有提供者地址列表发送给消费者,并且定期更新
心跳(续约):提供者定期通过http方式向Eureka刷新自己的状态
一,最少依赖启动,依赖如下
pom文件
<?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>Eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<!-- springCloud -->
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
</dependencies>
</project>
问题点:遇到了版本要兼容问题
application.yml
server:
port: 8868
spring:
application:
# 应用名称,会在Eureka中作为服务的id标识(serviceId)
name: eureka-server
eureka:
client:
# EurekaServer的地址,现在是自己的地址,如果是集群,需要写其它Server的地址。
service-url:
defaultZone: http://127.0.0.1:8868/eureka
# 不注册自己
register-with-eureka: false
# 不拉取
fetch-registry: false
启动类
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);
}
}
启动
生产者项目中加入依赖
(1)pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
(2)application.yaml文件
server:
port: 8081
spring:
datasource:
username: root
password: ROOT
url: jdbc:mysql://localhost:3306/my_demo?serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
application:
name: permission-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8868/eureka
(3)启动类加入注解
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@MapperScan("com.w***n.it")
@EnableDiscoveryClient // 暴露到Eureka上
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
消费者项目
同生产者一样,只有直接调用
//获取eureka中注册的user-service实例列表
List<ServiceInstance> instances = discoveryClient.getInstances("permission-server");
ServiceInstance serviceInstance = instances.get(0);