springcloud学习
第一章 Eureka服务注册与发现
一、Eureka基础知识
1.服务治理
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务治理。
在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
2.服务注册与发现
Eureka采用了CS的设计架构,Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息 比如 服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))
3.Eureka两组件
Eureka包含两个组件:Eureka Server和Eureka Client
Eureka Server提供服务注册服务
各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
EurekaClient通过注册中心进行访问
是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)
4.Eureka系统架构和Dubbo的架构
二、单机Eureka构建步骤
1.EurekaServer端服务注册中心,类似物业公司
建Module cloud-eureka-server7001
pom.xml
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
server:
port: 7001
eureka:
instance:
hostname: eureka7001 #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
defaultZone: http://eureka7002:7002/eureka/
主启动
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication7001.class,args);
}
}
测试:http://localhost:7001/
Eureka中没有服务被注册
2.EurekaClient端cloud-provider-payment8001将注册进EurekaServer成为服务提供者provider
建Module cloud-provider-payment8001
pom.xml
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
写application.yml
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
# mysql驱动包 com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud_alibaba?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
username: root
password: root
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka #单机版
instance:
instance-id: payment8001
prefer-ip-address: true
mybatis:
type-aliases-package: com.xiachao.springcloud.entities # 所有Entity别名类所在包
mapper-locations: classpath:mapper/*.xml
主启动
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PaymentApplication8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication8001.class,args);
}
}
测试
先启动eureka7001
http://localhost:7001/
3.EurekaClient端cloud-consumer-order81,将注册进EurekaServer成为服务消费者consumer
建Module cloud-consumer-order81
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
写application.yml
server:
port: 81
spring:
application:
name: cloud-order-service
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
主启动
@SpringBootApplication
@EnableEurekaClient
public class ConsumerOrderApplication81 {
public static void main(String[] args) {
SpringApplication.run(ConsumerOrderApplication81.class,args);
}
}
测试
先要启动EurekaServer,7001服务
再要启动服务提供者provider,8001服务
http://localhost:81/consumer/payment/get/31
三、集群Eureka构建步骤
1.新建cloud-eureka-server7002
pom.xml
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
主启动
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication7002.class,args);
}
}
2.将支付服务8001微服务发布到上面2台Eureka集群配置中
application,ynl
service-url:
# defaultZone: http://localhost:7001/eureka #单机版
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka #集群版本
将订单服务80微服务发布到上面2台Eureka集群配置中
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka #集群版本
3.支付服务提供者8001集群环境构建
参考cloud-provider-payment8001,新建cloud-provider-payment8002
controller
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
@Autowired
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@PostMapping("/create")
public CommonResult createPayment(Payment payment) {
int result = paymentService.createPayment(payment);
log.info("*****插入操作返回结果:" + result);
if (result > 0) {
return new CommonResult(200, "插入数据库成功,serverPort: " + serverPort, result);
} else {
return new CommonResult(444, "插入数据库失败,serverPort: " + serverPort, null);
}
}
@GetMapping(value = "/get/{id}")
public CommonResult<Payment> ById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
log.info("*****查询结果:{}", payment);
if (payment != null) {
return new CommonResult(200, "查询成功,serverPort: " + serverPort, payment);
} else {
return new CommonResult(444, "没有对应记录,查询Id: "
+ id + "serverPort: " + serverPort, null);
}
}
}
4.负载均衡
order81使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
ApplicationContextBean
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced //使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
cloud-consumer-order81访问cloud-provider-payment集群
OrderController
@RestController
@Slf4j
@RequestMapping("/consumer")
public class OrderController {
//public static final String PAYMENT_URL = "http://localhost:8001";
// 通过在eureka上注册过的微服务名称调用
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/payment/create")
public CommonResult<Payment> create(Payment payment) {
return restTemplate.postForObject(PAYMENT_URL + "/payment/create",
payment, CommonResult.class);
}
@GetMapping("/payment/getObject/{id}")
public CommonResult<Payment> getPaymentForObject(@PathVariable("id") Long id) {
return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id,
CommonResult.class);
}
@GetMapping("/payment/getEntity/{id}")
public CommonResult<Payment> getPaymentForEntity(@PathVariable("id") Long id) {
ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/"
+ id, CommonResult.class);
if (entity.getStatusCode().is2xxSuccessful()) {
log.info(entity.getStatusCode() + entity.toString());
return entity.getBody();
} else {
return new CommonResult(444, "操作失败");
}
}
}
测试:http://localhost:81/consumer/payment/get/11
5.服务发现Discovery
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
修改cloud-provider-payment8001的Controller
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping(value = "/discovery")
public Object getDiscovery() {
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("*******service" + service);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance element : instances) {
System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t"
+ element.getPort() + "\t" + element.getUri());
}
return this.discoveryClient;
}
8001主启动类
@EnableDiscoveryClient