1.搭建环境:JDK1.8+IDEA2017+MySQL8.0
2.项目模块结构
3.springcloud-eureka模块(服务器端)
(1)导入eureka sever服务依赖
<?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.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-eureka</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
</project>
(2)模块结构图
(3)编写yml文件进行配置
server:
port: 7001
#配置eureka的服务端实例名称
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #是否向注册中心进行注册
fetch-registry: false #false表示这是一个注册中心
service-url: #监控页面
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
(4)编写启动类加注解(springcloud_eureka_7001)
package com.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //开启EurekaServer服务端
public class springcloud_eureka_7001 {
/*导入依赖
* 编写配置文件yml
* 加@Eable。。。注解启动
* 编写启动类*/
public static void main(String[] args) {
SpringApplication.run ( springcloud_eureka_7001.class,args );
}
}
4.编写客户端(即将提供者模块注册到eureka注册中心)
(1)导入依赖
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<!--springboot中自带的监视器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
(2)编写yml文件
server:
port: 8001
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: com.cloud.pojo
spring:
application:
name: springcloud-provider-dept
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db01?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance: #修改默认描述信息
instance-id: springcloud-provider-dept-8001
info:
app.name: com.springcloud
company.name: springboot.actuator
(3)修改启动类,添加注解
package com.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient//表示该实例启动后会自动注册到eureka注册中心
public class Springcloudprovider8001 {
public static void main(String[] args) {
SpringApplication.run ( Springcloudprovider8001.class,args );
}
}
(4)测试,先打开7001,再打开8001进行注册
5.集群
(1)结构图
(2)各eureka1结构图
(3)eureka的yml文件
server:
port: 7001
#配置eureka的服务端实例名称,eureka7003.com,修改的文件地址"C:\Windows\System32\drivers\etc\hosts"
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false #是否向注册中心进行注册
fetch-registry: false #false表示这是一个注册中心
service-url: #监控页面
#单机:http://${eureka.instance.hostname}:${server.port}/eureka/
#集群:关联
defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7002.com:7002/eureka/
(4)eureka7002的yml文件
server:
port: 7002
#配置eureka的服务端实例名称
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: false #是否向注册中心进行注册
fetch-registry: false #false表示这是一个注册中心
service-url: #监控页面
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
(5)eureka7003的yml文件
server:
port: 7003
#配置eureka的服务端实例名称
eureka:
instance:
hostname: eureka7003.com
client:
register-with-eureka: false #是否向注册中心进行注册
fetch-registry: false #false表示这是一个注册中心
service-url: #监控页面
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
(6)将8001提供者项目部署到集群,修改。yml文件即可
server:
port: 8001
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: com.cloud.pojo
spring:
application:
name: springcloud-provider-dept
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db01?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance: #修改默认描述信息
instance-id: springcloud-provider-dept-8001
info:
app.name: com.springcloud
company.name: springboot.actuator
(7)测试