1.1服务治理
管理每个服务于服务之间一依赖关系比较复杂,利用服务治理可以很好的管理服务之间的依赖关系。可以实现服务的调用,负载均衡,容错等,实现服务的注册与发现。
1.2服务注册与发现
eureka采用的是cs的设计架构,eureka作为服务注册功能的服务器。系统中的其他微服务使用Eureka的客户端连接到Eurka server并维持心跳连接。这样系统的维护人员就可以通过Eurka server来监控系统中各个微服务是否正常运行。
系统中存在三个微服务的部分:左边的是消费者微服务,右边的是服务提供者微服务集群,上面的部分是Eureka Server注册中心集群。
1.3Eureka组件
Eureka Server提供服务注册服务
各个微服务节点通过配置启动后,会在Eureka Server中注册,这样就会存储所有可用的服务信息,服务节点的信息可以在界面中很直观的看到
Eureka Client通过注册中心访问
微服务启动后,消费者和服务提供者都会对Eureka Server发送心跳(默认周期为30s),如果Eureka Server在多个周期内没有接收到某个节点传递来的心跳,Eureka Server就会把该服务移除(默认为90s)
2.1单机版的Eureka构建步骤
首先创建一个eurekaServer端服务的module
- 改pom文件(版本号是通过父目录的dependencyManagement标签决定的,在父窗口写好了版本号这里可以直接使用)
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
- 写yml
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址,这个地址代表自己
- 主启动
@SpringBootApplication
@EnableEurekaServer //服务注册的注解
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
- 然后测试
EurekaClient端cloud-provider-payment8001将注册进EurekaServer成为服务提供者provide
同样的步骤,新建一个服务提供者的module
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
server:
port: 8001
#端口号
spring:
application:
name: cloud-payment-service #服务名字
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
#数据库配置
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka
instance:
instance-id: payment8001 #指定在注册中心显示的名字
prefer-ip-address: true #开启左下角落显示的ip
#这里注册进eureka里面
主启动的时候需要加上@EnableEurekaClient注解
然后测试,先要启动EurekaServer,
有时候会有这种警告文字:这是eureka的自我保护机制:当服务掉线之后不会立马撤销服务注册,在持续接收不到心跳之后才会撤销服务的注册。
EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer
- 建module
- 改pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
- 写yml
server: port: 80 spring: application: name: cloud-order-service eureka: client: register-with-eureka: true fetchRegistry: true service-url: defaultZone: http://localhost:7001/eureka
- 主启动@EnableEurekaClient
- 测试
bug总结:用户端采用的80端口是http服务调用的默认端口,所以访问的时候http://localhost/...就行,用户不需要知道端口特点。然后80端口很容易被占用。
被占用的原因:sqlserver服务占用
当然可能会有其他理由,例如使用cmd查看
显示被4占用,这个是系统默认访问的位置,不一定有影响,本人先更改了这个的一个设置。
2.2集群版的构建
集群版的概念以及为什么用集群?
- 集群就是相同的内容构建好几份,写几个相同module
- 因为如果所有的服务都在同一个module上,万一这个module故障会导致所有服务瘫痪,会产生单点故障
- 因此,提供服务的服务端和注册中心都要使用集群
集群版的使用
首先新建一个同样功能的服务提供8002,注册中心7002
注册中心的互相守望:7001和7002的配置文件
ps:这里需要修改windows系统文件,找到C:\Windows\System32\drivers\etc路径下的hosts文件
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7002.com:7002/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
服务提供者的注册:两个module使用同一个服务名称,通过服务名称访问
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
instance:
instance-id: payment8002
prefer-ip-address: true
测试
消费者通过服务名称对两个模块访问
通过RestTemplate来进行远程访问,使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.lb.LoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.net.URI;
import java.util.List;
@RestController
@Slf4j
public class OrderController {
public static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
@Resource
private RestTemplate restTemplate;
@Resource
private LoadBalancer loadBalancer;
@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
}
@GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id")Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
@GetMapping(value = "/consumer/payment/getEntity/{id}")
public CommonResult<Payment> getPayment2(@PathVariable("id")Long id){
ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
if(forEntity.getStatusCode().is2xxSuccessful()){
return forEntity.getBody();
}else {
return new CommonResult<>(444,"");
}
}
@GetMapping(value = "/consumer/payment/lb")
public String getPaymentLB(){
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
if (instances == null || instances.size() <= 0){
return null;
}
ServiceInstance serviceInstance = loadBalancer.instances(instances);
URI uri = serviceInstance.getUri();
return restTemplate.getForObject(uri+"/payment/lb",String.class);
}
}
Ribbon和Eureka整合后Consumer可以直接调用服务而不用再关心地址和端口号,且该服务还有负载功能了