1.用Maven创建new project
2.将新项目里面的src文件删除
3.然后新建两个module文件,一个为eureka_server,另一个为hello_service如图:
4.父工程的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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_cloud_demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka_server</module>
<module>hello_service</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
5.点击Maven进行加载,自动引入依赖文件
6.在EurekaServer子模块的pom.xml增加如下代码
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>
7.配置application.yml:在EurekaServer子模块的resources文件夹下创建application.yml, 配置如下:
server:
port: 9000 #端口号
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #是否将自己注册到注册中心
fetch-registry: false #是否从eureka中获取注册信息
service-url:#配置暴露给eureka client的请求信息
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
8.创建启动类:在java下建包eureka,在包eureka下建启动类EurekaServerApplication EurekaServerApplication类的代码如下:
@SpringBootApplication
@EnableEurekaServer //激活Eureka Server端配置
public class EurekaServerApplication {
public static void main (String[] args){
SpringApplication.run(EurekaServerApplication.class,args);
}
}
9.打开注册中心管理后台:先运行启动类(右键RUN),然后在浏览器地址栏输入: localhost:9000
10. 配置hello_service子模块的pom.xml 增加如下代码:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<!-- 引入eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
11.配置application.yaml文件:(缩进可能会有问题,注意字符)
spring:
application:
name: helloservice
eureka:
client:
service-url: # eureka server的路径
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用ip注册
12.在java下建包hello,在包hello下建启动类HelloServiceApplication HelloServiceApplication启动类代码如下:
@SpringBootApplication
//激活EurekaClient 从Spring Cloud Edgware版本开始可省略
//@EnableDiscoveryClient
//@EnableEurekaClient
public class HelloServiceApplication {
public static void main(String[] args){
SpringApplication.run(HelloServiceApplication.class,args);
}
}
13.启动HelloServiceApplication服务:启动成功后,在注册中心管理查看服务
接下来基于上面的项目进行搭建高可用Eureka集群:
1.在.idea/workspace.xml增加如下信息:(注意,直接在idea进行修改可能会自动删除,最好的方式是关闭idea,然后在文件管理器里面用txt格式打开该文件,然后进行修改和保存。)
<component name="RunDashboard">
<option name="configurationTypes">
<set>
<option value="SpringBootApplicationConfigurationType" />
</set>
</option>
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
2.第一步做完后重启idea,然后按如下步骤开启Run DashBoard(Services)面板
3.然后重点来了,第一次启动。找到eureka_server模块的application.yml.将内容修改为:
#peer1的配置信息
spring:
application:
name: eureka server
server:
port: 8000 #端口号
eureka:
client:
service-url: #配置暴露给eureka client的请求信息
defaultZone: http://127.0.0.1:8001/eureka/
然后启动。
4。待第一次启动后,再将eureka_server模块的application.yml.将内容修改为:
#peer2的配置信息
spring:
application:
name: eureka server
server:
port: 8001 #端口号
eureka:
client:
service-url: #配置暴露给eureka client的请求信息
defaultZone: http://127.0.0.1:8000/eureka/
准备进行第二次启动。
5.第二次启动:
找到下方的services,EurekaApplication右键按照下图步骤,创建第二个运行。
修改名字
创建完成后,然后启动第二个启动类(在services面板中启动)
6.通过端口号,进入第一次和第二次的注册中心管理后台。
集群搭建基本结束。
服务注册到多个eureka server:
1.修改service中的src/main/resources/application.yml,每个eureka server地址用逗号隔开
修改后启动服务
监控页面显示服务的ip及端口号:
1.修改service中的src/main/resources/application.yml,增加如下代码: instance-id: ${spring.cloud.client.ip-address}:${server.port}
eureka搭建结束,后面springcloud知识会继续更新