Eureka在springcloud的技术体系中主要起到了服务注册和服务发现的功能;
Eureka 采用了CS的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心,而系统中的其它微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接,这样系统的维护人员就可以通过Eureka server 来监控系统中各个微服务是否正常运行, 在服务注册与发现中,有一个注册中心,当服务器启动的时候,会把当前自己服务器的信息, 比如服务地址,通信地址,等以别名方式注册到注册中心,另一方(消费者|服务提供者),以该别名的形式去注册中心获取到实际的服务通信地址,然后再实现本地RPC调用RPC远程调用框架设计思想:在于注册中心,因为使用注册中心,管理每个服务与服务之间的依赖关系,在任何的 RPC远程调用框架中都会有一个注册中心.
Eureka server 提供服务注册的服务
各个微服务结点通过配置启动后,会在Eureka Server中的服务注册表中存储所有可用服务结点的信息,服务结点的信息可以在界面上直观的看到
EurekaClient 通过注册中心进行访问
是一个java客户端,用于简化EurekaServer的交互,使用轮询负载算法的负载均衡器,在应用启动后,(默认30s)将会向Eureka Server中发送心跳,如果EurekaServer在多个心跳周期内没有收到某个结点的心跳,Eureka Server 会从服务注册表中将该服务结点移除(默认周期为90s)
修改server的心跳时间
eureka.instance.lease-renewal-interval-in-seconds: 4 #应用启动后发送心跳检测
eureka.instance.lease-expiration-duration-in-seconds: 10 # 应用运行中发送心跳检测的时间
实例演示:
- 首先注册eureka Server服务
application.yml
server:
port: 7001
spring:
application:
name: cloud_eureka_server
eureka:
instance:
hostname: eureka7001.com # 修改本地host文件配置
client:
register-with-eureka: false # 本身就是一个server端 所以不需要再注册
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ #把自己作为服务端
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 5000
# defaultZone: http://eureka7002.com:7002/eureka/ # 配置集群
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>Spring Cloud学习</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud_eureka_server</artifactId>
<dependencies>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>org.example</groupId>
<artifactId>Entities</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 一般通用配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
服务端搭建ok
访问localhost:7001访问服务端查看注册的服务,之前创建的服务
eureka常用的配置常用配置详解
客户端使用
业务:支付服务注册到注册中心
application.yml
server:
port: 8002
spring:
application:
name: Payment-service # 服务的名字
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=GMT
username: root
password: 19980120
eureka:
client:
register-with-eureka: true #标识是否将自己注册到注册中心
fetch-registry: true #是否从注册中心抓取已有的注册信息 单节点 没有必要 集群必须配合ribbon使用负载均衡
service-url:
defaultZone: http://eureka7001.com:7001/eureka #将服务注册到注册中心中
# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #将服务注册到注册中心中
instance:
instance-id: payment8002
prefer-ip-address: true #注册中心显示ip
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.sofency.top.entities
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>Spring Cloud学习</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Payment8002</artifactId>
<dependencies>
<!--将服务提供者注册到eureka服务中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入自己的maven依赖-->
<dependency>
<groupId>org.example</groupId>
<artifactId>Entities</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 包含了sleuth zipkin 数据链路追踪-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
启动类注意加上@EnableDiscoveryClient
@MapperScan("com.sofency.top.dao")
@SpringBootApplication
//@EnableEurekaClient //注册到注册中心
@EnableDiscoveryClient
public class PaymentApplication8002 {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication8002.class, args);
}
}
编写业务代码和controller
RestTemplate的使用
在我们的微服务中,可能只有一个服务是面向用户的,其它的服务是内部逻辑的使用,服务间的调用使用RestTemplate进行调用,
那么负载均衡就可以对RestTemplate进行使用
我们在配置文件中注入负载均衡的Restemplate 默认的负载策略是轮询
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced //增加负载均衡机制 默认的
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
还可以自定义负载均衡机制 自定义负载策略 注意不要和启动类在一个包下
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MySelfRule {
@Bean
public IRule createMyRule(){
return new RandomRule();//定义为随机的
}
}
在启动类上使用如下注解@RibbonClient,在业务类中直接注入RestTemplate 这样就实现了随机访问的负载策略
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "PAYMENT-SERVICE",configuration= MySelfRule.class) //name是要访问的服务,
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}