Eureka注册中心的搭建
首先以7001为例
首先导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
然后再配置文件(application.yml)中编写Eureka的依赖
server:
port: 7001
eureka:
instance:
hostname: Eureka7001.com #eureka服务的注册名称
client:
register-with-eureka: false #表示是否想eureka注册中心注册自己
fetch-registry: false #为false表示自己是注册中心
service-url: #监控页面
defaultZone: http://Eureka7002.com:7002/eureka/,http://Eureka7003.com:7003/eureka/#链接其余的两个注册中心形成集群。
spring:
application:
name: springcloud-eureka-7001
最后再启动类上增加@EnableEurekaServer 注解,该注解可以将项目作为SpringCloud中的注册中心。
package com.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @description:
* @author: 陈声铭
* @time: 2021/3/27 16:15
*/
@SpringBootApplication
@EnableEurekaServer //Eureka服务端启动类注解
public class EurekaServer_7001 {
public static void main(String[] args){
try{
SpringApplication.run(EurekaServer_7001.class, args);
} catch(Exception e) {
System.out.println("SpringApplication----->" + e.getMessage());
}
}
}
7002和7003与之类似,但是要注意service-url.defaultZone的配置是有相同的
7002的配置为http://Eureka7001.com:7001/eureka/,http://Eureka7003.com:7003/eureka/
7003的配置为http://Eureka7001.com:7001/eureka/,http://Eureka7002.com:7002/eureka/
Eureka服务端
首先生产者的配置(application.yml)
server:
port: 8002
eureka:
instance:
instance-id: provider_8002
prefer-ip-address: true
client:
register-with-eureka: true #表示是否想eureka注册中心注册自己
# fetch-registry: false #为false表示自己是注册中心
service-url: #监控页面
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
info:
app.name: 生产者-8002
company.name: provider_8002
spring:
application:
name: provider
datasource:
url: jdbc:mysql://localhost:3306/springcloudlearning?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
#数据原
type: com.alibaba.druid.pool.DruidDataSource
#数据库驱动
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
mybatis:
config-location: classpath:mybatis\mybatis-config.xml
mapper-locations: classpath:mybatis\mapper\*.xml
type-aliases-package: com.eo
再启动类上添加@EnableEurekaClient注解
package com.springcloud.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @description:
* @author: 陈声铭
* @time: 2021/2/24 1:18
*/
@SpringBootApplication
//开启Eureka客户端
@EnableEurekaClient
public class spring_cloud_provider_8002 {
public static void main(String[] args){
try{
SpringApplication.run(spring_cloud_provider_8002.class, args);
} catch(Exception e) {
System.out.println("SpringApplication----->" + e.getMessage());
}
}
}
消费者的配置(application.yml)
server:
port: 80
eureka:
client:
service-url: #监控页面
defaultZone: http://Eureka7001.com:7001/eureka/,http://Eureka7002.com:7002/eureka/,http://Eureka7003.com:7003/eureka/
消费者的启动类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @description:
* @author: 陈声铭
* @time: 2021/3/27 15:02
*/
@SpringBootApplication
//开启Eureka客户端
@EnableEurekaClient
public class DepatSpringBootApplication {
public static void main(String[] args){
try{
SpringApplication.run(DepatSpringBootApplication.class, args);
} catch(Exception e) {
System.out.println("DepatSpringBootApplication----->" + e.getMessage());
}
}
}