文章目录
服务注册中心
在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
Dubbo架构图
Eureka
Eureka采用了CS的设计架构,Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员就可以通过Eureka Server 来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息比如服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者 | 服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
启动器@EnableEurekaServer
package cn.edu.guet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author pangjian
* @ClassName EurekaMain
* @Description EurekaServer启动器
* @date 2021/9/10 16:54
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain {
public static void main(String[] args) {
SpringApplication.run(EurekaMain.class, args);
}
}
配置yml
eureka:
instance:
# eureka服务端的实例名称
hostname: localhost
client:
# false 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自己端就是注册中心,为了维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
# 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
defaultzone: http://${eureka.instance.hostname}:${server.port}/eureka/
测试
# 访问后到Eureka官网则测试通过
http://localhost:7001/
微服务注册
将服务消费者和服务提供者注册到EurekaService,两者成为EurekaService的EurekaClient。EurekaService相当于婚介所,服务消费者是要娶老婆的男人,服务提供者是待嫁的女人。
消费者和提供者依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
启动类加@EnableEurekaClient
package cn.edu.guet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author pangjian
* @ClassName OrderMain
* @Description TODO
* @date 2021/9/10 15:12
*/
@SpringBootApplication
@EnableEurekaClient
public class OrderMain {
public static void main(String[] args) {
SpringApplication.run(OrderMain.class, args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
配置yml
spring:
application:
# 微服务名
name: cloud-order-service
eureka:
client:
# 表示是否将自己注册进EurekaServer
register-with-eureka: true
# 是否从EurekaServer抓取已有的注册信息,集群必须设置true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
测试成功注册
Eureka集群搭建
修改配置
C:\Windows\System32\drivers\etc路径下的hosts文件,添加下面配置
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
修改yml配置
EurekaServer相互注册,就可以搭建高可用集群
7002号EurekaServer
server:
port: 7002
eureka:
instance:
# eureka服务端的实例名称
hostname: eureka7002.com
client:
# false 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自己端就是注册中心,为了维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
# 将7002注册进7001
defaultZone: http://eureka7001.com:7001/eureka/
7001号EurekaServer
server:
port: 7001
eureka:
instance:
# eureka服务端的实例名称
hostname: eureka7001.com
client:
# false 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自己端就是注册中心,为了维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
# 将7001注册进7002
defaultZone: http://eureka7002.com:7002/eureka/
服务提供者集群搭建
修改yml配置
server:
port: 8002
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/cloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
druid:
test-while-idle: false
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: cn.edu.guet.pojo
eureka:
client:
# 表示是否将自己注册进EurekaServer
register-with-eureka: true
# 是否从EurekaServer抓取已有的注册信息,集群必须设置true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
基本就是复制一份服务提供者的代码,修改一下端口号
可以看到有两个服务提供者,并且当前eureka注册中心也注册进了7002,实现高可用
负载均衡
现在我们服务消费者调用服务是通过端口号和ip进行调用的,就一直只会调用一个服务,集群就不生效了,我们可以通过微服务名称和负载均衡去对服务进行访问。
package cn.edu.guet.controller;
import cn.edu.guet.pojo.Payment;
import cn.edu.guet.response.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
* @author pangjian
* @ClassName OrderController
* @Description RestTemplate提供了多种便捷访问远程Http服务的方法,
* 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
* @date 2021/9/10 15:16
*/
@RestController
@Slf4j
public class OrderController {
// public static final String PAYMENT_URL = "http://localhost:8001";
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT_SERVICE";
@Autowired
private RestTemplate restTemplate;
/**
* @Description: 通过RestTemplate调用8001端口下的服务
* @Param payment:
* @return cn.edu.guet.response.CommonResult<cn.edu.guet.pojo.Payment>
* @date 2021/9/10 15:25
*/
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment) {
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
}
开启负载均衡 @LoadBalanced
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
服务发现
开启注解@EnableDiscoveryClient
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PaymentMain {
public static void main(String[] args) {
SpringApplication.run(PaymentMain.class, args);
}
}
在服务里面注入discoveryClient并提供一个restful接口给外界访问
/**
* @author pangjian
* @ClassName PaymentController
* @Description TODO
* @date 2021/9/10 14:36
*/
@RestController
@Slf4j
public class PaymentController {
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/payment/getInfo")
public Object discovery() {
List<String> services = discoveryClient.getServices(); // 得到所有微服务
for (String ele:
services) {
log.info("ele:" + ele);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-SERVICE");
for (ServiceInstance instance : instances) {
log.info(instance.getInstanceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
}
return this.discoveryClient;
}
}