业务需求
- eureka简介
- 引入依赖
- 修改配置
- 创建provider和consumer
- 测试
1.eureka简介
- Spring Cloud Eureka 是Spring Cloud Netflix 微服务套件中的一部分, 它基于Netflix Eureka 做了二次封装, 主要负责完成微服务架构中的服务治理功能。
- Spring Cloud 通过为Eureka 增加了Spring Boot 风格的自动化配置,我们只需通过简单引入依赖和注解配置就能让Spring Boot 构建的微服务应用轻松地与Eureka 服务治理体系进行整合。
- Eureka 提供的服务端, 提供服务注册与发现的功能, 一般被称作eureka-server。
2.目录结构
3.创建cloud-eureka-server7001
Server端引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--自定义api通用包-->
<dependency>
<groupId>com.study.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
#如果单纯的引入Eureka server相关的依赖,不用引入这个依赖,这个依赖是我本地的commons模块,自己测试用的
<dependency>
<groupId>com.study.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
#lombok插件
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
# 用于swagger,方便测试
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.6</version>
</dependency>
# 用于swagger,方便测试
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
创建模块,并且修改yaml文件
yaml配置文件
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com#eureka服务端实例名称
client:
register-with-eureka: false #表示不像注册中心注册自己
fetch-registry: false #false表示自己就是注册中心,我的职责就是维护服务实例,并不区检索服务
service-url:
defaultZone: http://eureka7002.com:7002/eureka/ #集群模式下,向别的eureka注册
# defaultZone: http://eureka7001.com:7001/eureka/ 单机模式下,向自己注册
# server:
# enable-self-preservation: false # 关闭自我保护机制 保证不可用服务及时清除
# eviction-interval-timer-in-ms: 2000
创建启动类,并且添加注解
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);
}
}
4.创建cloud-eureka-server7002(类比7001创建)
引入依赖
引入的依赖和7001的依赖相同
创建模块,并且修改yaml配置文件
yaml配置文件
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端实例名称
client:
register-with-eureka: false #表示不向注册中心注册自己
fetch-registry: false #false表示自己就是注册中心,我的职责就是维护服务实例,并不区检索服务
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
创建启动类,并且添加注解
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);
}
}
5.创建cloud-provider-payment8001模块
引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改yaml配置文件
server:
port: 8001
servlet:
context-path: /v1payp
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2021?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password:
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.study.cloud.entities # 所有Entity别名类所在包
eureka:
client:
#表示是否将自己注册金eurekaserver
register-with-eureka: true
#是否从eureka抓取自己的注册信息,默认为true,集群必须为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/
instance:
instance-id: payment8001 #主机名称
prefer-ip-address: true #表示访问路径是否显示ip地址
创建启动类并添加注解
@EnableEurekaClient
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
6.类比cloud-provider-payment8001,创建8002
7.创建cloud-provider-consumer80模块
引入依赖
<dependencies>
#如果单纯的引入Eureka server相关的依赖,不用引入这个依赖,这个依赖是我本地的commons模块,自己测试用的
<dependency>
<groupId>com.study.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
#lombok插件
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
# 用于swagger,方便测试
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.6</version>
</dependency>
# 用于swagger,方便测试
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
修改yaml配置文件
yaml配置文件
server:
port: 80
servlet:
context-path: /v1payc
spring:
application:
name: cloud-consumer-order80
eureka:
client:
#表示是否将自己注册金eurekaserver
register-with-eureka: true
#是否从eureka抓取自己的注册信息,默认为true,集群必须为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/
创建启动类并添加注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
测试结果
可以看出是以轮询的方式分别访问8001和8002端口。当关闭其中一个server以后,还是这样的结果。
总结
1.eureka集群,其实就是创建多个eureka,然后相互之间注册 2.我创建的只是抽象出来的一些东西,一些具体的业务逻辑代码没有展示出来。 3.最主要的就是yaml配置文件。
|