SpringBoot Eureka集群配置
0、公共pom文件,各项目都需要
配置hosts
文件,路径 C:\Windows\System32\drivers\etc\hosts
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
# ::1 localhost
127.0.0.1 server7001
127.0.0.1 server7002
127.0.0.1 server7003
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
服务端配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
提供者或消费者配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
1、EurekaServer_7001
server:
port: 7001 #服务注册中心端口号
spring:
application:
name: eureka-server-cluster
eureka:
instance:
hostname: server7001 #服务注册中心IP地址
client:
registerWithEureka: false #是否向服务注册中心注册自己
fetchRegistry: false #是否检索服务
serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://server7001:7001/eureka/,http://server7002:7002/eureka/,http://server7003:7003/eureka/
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //服务端的启动类,可以接受别人注册进来
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class, args);
}
}
2、EurekaServer_7002
server:
port: 7002 #服务注册中心端口号
spring:
application:
name: eureka-server-cluster
eureka:
instance:
hostname: server7002 #服务注册中心IP地址
client:
registerWithEureka: false #是否向服务注册中心注册自己
fetchRegistry: false #是否检索服务
serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://server7001:7001/eureka/,http://server7002:7002/eureka/,http://server7003:7003/eureka/
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //服务端的启动类,可以接受别人注册进来
public class EurekaServer_7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7002.class, args);
}
}
2、EurekaServer_7003
server:
port: 7003 #服务注册中心端口号
spring:
application:
name: eureka-server-cluster
eureka:
instance:
hostname: server7003 #服务注册中心IP地址
client:
registerWithEureka: false #是否向服务注册中心注册自己
fetchRegistry: false #是否检索服务
serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://server7001:7001/eureka/,http://server7002:7002/eureka/,http://server7003:7003/eureka/
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //服务端的启动类,可以接受别人注册进来
public class EurekaServer_7003 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7003.class, args);
}
}
4、EurekaProvider_8001
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://server7001:7001/eureka/
server:
port: 8001 #服务端口号
spring:
application:
name: service-provider #服务名称--调用的时候根据名称来调用该服务的方法
@SpringBootApplication
@EnableEurekaClient
public class EurekaProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(EurekaProvider_8001.class, args);
}
}
@RestController
public class TestController {
@RequestMapping("/get")
public String get() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return "provider-8001 " + sdf.format(new Date());
}
}
5、EurekaProvider_8002
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://server7001:7001/eureka/
server:
port: 8002 #服务端口号
spring:
application:
name: service-provider #服务名称--调用的时候根据名称来调用该服务的方法
@SpringBootApplication
@EnableEurekaClient
public class EurekaProvider_8002 {
public static void main(String[] args) {
SpringApplication.run(EurekaProvider_8002.class, args);
}
}
@RestController
public class TestController {
@RequestMapping("/get")
public String get() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return "provider-8002 " + sdf.format(new Date());
}
}
6、EurekaProvider_8003
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://server7001:7001/eureka/
server:
port: 8003 #服务端口号
spring:
application:
name: service-provider #服务名称--调用的时候根据名称来调用该服务的方法
@SpringBootApplication
@EnableEurekaClient
public class EurekaProvider_8003 {
public static void main(String[] args) {
SpringApplication.run(EurekaProvider_8003.class, args);
}
}
@RestController
public class TestController {
@RequestMapping("/get")
public String get() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return "provider-8003 " + sdf.format(new Date());
}
}
7、EurekaConsumer_9001
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://127.0.0.1:7001/eureka/
server:
port: 9001 #服务端口号
spring:
application:
name: service-consumer #服务名称--调用的时候根据名称来调用该服务的方法
@SpringBootApplication
@EnableEurekaClient
public class EurekaConsumer_9001 {
@Bean
@LoadBalanced //启用负载均衡机制
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaConsumer_9001.class, args);
}
}
@RestController
public class TestController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/get")
public String getUser() {
return restTemplate.getForObject("http://service-provider/get", String.class);
}
}