SpringCloud 简介
Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线)。分布式系统的协调导致了样板模式, 使用Spring Cloud开发人员可以快速地支持实现这些模式的服务和应用程序。他们将在任何分布式环境中运行良好,包括开发人员自己的笔记本电脑,裸机数据中心,以及Cloud Foundry等托管平台。
Eureka 介绍
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
Eureka包含两个组件:Eureka Server 和Eureka Client 。Eureka Server提供服务注册服务,各个微服务节点通过配置启动后,会在Eureka Server中进行注册,这样Eureka Server中的服务注册表中将会存储所有可用服务节点的信息,服务节点信息可以在界面中直接看到;Eureka Client通过注册中心进行访问,是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就是一个内置的、使用轮询(round-robin)负载算法的负载均衡器。
Eureka Server 搭建
建立server model 模块
修改pom 文件
cloud2020
com.springcloud
1.0-SNAPSHOT
4.0.0
cloud-eureka-server7001
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
com.atguigu
cloud-api-commons
${project.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-test
test
junit
junit
修改yml 文件
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
新建程序启动类
在主启动类上添加@EnableEurekaServer
代表启动服务注册
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
测试
在浏览器地址栏输入http://localhost:7001
,出现以下界面,证明EurekaServer搭建成功
Eureka Client 搭建
新建生产者模块(model)
修改pom文件
cloud2020
com.atguigu
1.0-SNAPSHOT
4.0.0
cloud-provider-payment8001
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
com.atguigu
cloud-api-commons
${project.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.mybatis.spring.boot
mybatis-spring-boot-starter
com.alibaba
druid-spring-boot-starter
1.1.10
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
修改yml文件
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/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entites # 所有Entity别名类所在包
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka #Eureka server 地址
启动类
在启动类上添加@EnableEurekaClient
注解 ,表示此程序为Eureka Client 端
@SpringBootApplication
@EnableEurekaClient
public class PayMent8001 {
public static void main(String[] args) {
SpringApplication.run(PayMent8001.class, args);
}
}
测试
先启动EurekaServer服务端(7001),再启动EurekaClient,在浏览器地址上http://localhost:7001
,看到如下页面则EurekaClient成功
至此 Eureka 搭建完毕了。