前面我们已经搭建完成了服务端,现在我们要搭一个建客户,在多模块的项目结构中,一个模块就代表一个功能块,所以我们新建一个子模块。注意该模块是一个客户端,所以我们导包要导入eureka-client的客户端,由于我们启动需要tomcat,所以要导入springboot 包 下的starter-web包,不然启动会报错。前面忘了提新建的模块需要删除build和properties的配置,暂时不需要其他插件,java1.8版本我们在父模块就配置了,对所有子模块都有效
下面是pom需要引入的jar包
<dependencies>
<dependency> <!--该依赖集成了spring的web,webmvc,tomcat等一系列的jar包-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
新建一个主配置类
@SpringBootApplication //声明主配置类
@EnableEurekaClient //自动开启服务端的功能
public class UserServerApplication {
public static void main(String[] args) {
SpringApplication.run(UserServerApplication.class);
}
}
在生成环境下,所有的子模块都可以可以根据实际需求来配置集群,支持容错,提高性能,避免高并发的情况服务器的堵塞导致服务器崩溃
我这里也模拟配置两个客户端
resources 目录下application.yml 配置文件:
eureka:
client:
serviceUrl:
defaultZone: http://peer1:1010/eureka/,http://peer2:1011/eureka/,http://peer3:1012/eureka/
instance:
prefer-ip-address: true #使ip地址注册
spring:
application:
name: user-server #公共的服务名
profiles:
active: user-peer1 #当前环境,这里可以切换环境
---
server:
port: 1020 #端口
eureka:
instance:
instance-id: user-server:1020 #实列id地址
spring:
profiles: user-peer1 #环境名
---
server:
port: 1021
eureka:
instance:
instance-id: user-server:1021
spring:
profiles: user-peer2
配置完成后分别启动 user-server:1021,user-server:1022
浏览器访问 peer1:1010 , peer2:1011, peer3:1012
注册成功。