【Springcloud】(02)中级搭建-加入Eureka注册中心
(1)创建Eureka注册中心类:cloud-eureka-server7001
(1.1)创建注册中心流程如下
(1)创建module,改pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud工作区</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server7001</artifactId>
<dependencies>
<!--新版本eureka,注意这里是eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--引入自己定义的API通用包,可以使用Payment支付的Entity-->
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<!--devtools-->
<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>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
(2)yml
#访问地址就是http://localhost:7001/
server:
port: 7001
eureka:
instance:
hostname: localhost #euraka服务端的实例名称
client:
#false表示不往注册中心注册自己
regiter-with-eureka: false
#fasle表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
(3)启动类
@SpringBootApplication
@EnableEurekaServer//表示这里就是Eureka的注册中心
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
(4)测试进入注册中心
测试地址:#访问地址就是http://localhost:7001/
(1.2)把其他类加入注册中心流程如下
(1)修改pom
都加入如下依赖
<!--表示把这个模块注册到注册中心里去-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
(2)修改yml
server:
port: 80 #服务端口
spring:
application:
name: cloud-consumer-order #服务名,也就是放进注册中心的名字
eureka:
client:
#表示是否把自己注册到EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true此案呢过配合ribbon使用负载均衡
fetchRegistry: true
#注册中心的地址
service-url:
defaultZone: http://localhost:7001/eureka
server:
port: 8001 #服务端口
spring:
application:
name: cloud-payment-service #服务名,也就是放进注册中心的名字
datasource:
type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver #数据库驱动包
url: jdbc:mysql://localhost:3306/cloud?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
username: root
password: admin
devtools:
restart:
enabled: true #是否支持热部署
eureka:
client:
#表示是否把自己注册到EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true此案呢过配合ribbon使用负载均衡
fetchRegistry: true
#注册中心的地址
service-url:
defaultZone: http://localhost:7001/eureka
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.allen.springcloud.entities #所有entity别名所在包
(3)修改启动类,加入注解
(4)再次运行,测试结果
两个模块已经加入了注册中心