版本信息:
springboot 2.2.2.RELEASE;springcloud Hoxton.SR1
集群版入口
目录结构如下:
首先配置聚合工程,父模块的pom如下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.2.2.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<fork>truefork>
configuration>
plugin>
plugins>
build>
pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
yml文件:
server:
port: 8080
eureka:
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false 表示自己端就是注册中心,我的职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://localhost:8080/eureka
spring:
application:
name: eureka-server
主启动类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain {
public static void main(String[] args) {
SpringApplication.run(EurekaMain.class,args);
}
}
pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
yml文件:
server:
port: 9090
spring:
application:
name: eureka-client
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka
主启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClient9090 {
public static void main(String[] args) {
SpringApplication.run(EurekaClient9090.class,args);
}
}
加上controller的业务逻辑,获取端口号
@RestController
public class EurekaController {
@Value("${server.port}")
private String port;
@GetMapping("getPort")
public String getPort(){
return port;
}
}
类似的,再配置两个客户端,三台客户端也就是三个提供者,端口分别为9090、9091、9092,再配置一个消费者,端口为80,pom与yml文件类似生产者配置,配置80的controller
@RestController
public class EurekaController {
private static String url="http://EUREKA-CLIENT"; //EUREKA-CLIENT为提供者应用名称
@Autowired
private RestTemplate restTemplate; //注入restTemplate用来远程调用
@GetMapping("get")
public String getPort(){
String port = restTemplate.getForObject(url + "/getPort", String.class);
System.out.println(port);
return port;
}
}
配置一个配置类把RestTemplate 加入到IOC容器
@Configuration
public class MyConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
先启动Eureka服务端,在启动3个提供者和1个消费者,启动完成后可以在http://localhost:8080查看服务注册情况,4个服务均已注册到Eureka中。
首先访问任一一个提供者查看是否健康,访问http://localhost:9091/getPort,返回正确的端口号,说明提供者正常。
接下来访问消费者,http://localhost/get,发现报错了
原因:消费者通过RestTemplate去调用提供者,但是提供者有3个,不知道调用哪一个提供者,发生了错误。
解决办法:在配置类中加入@LoadBalanced 使其赋予负载均衡的能力。
@Configuration
public class MyConfig {
@Bean
@LoadBalanced //加入负载均衡的注解
public RestTemplate restTemplate(){
return new RestTemplate();
}
}