微服务文件结构
启动eureka
初始时,已有两个写好的服务,分别为orderservice和userservice,eurekaserver是一个新建的maven项目模块,对于eurekaserver部分,所需要的操作为:
添加依赖
<dependencies>
<!--eureka服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
添加代码正文以及配置文件
@EnableEurekaServer启动支持,另一个则是application正常启用
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
server:
port: 10086 # 服务端口
spring:
application:
name: eurekaserver # eureka的服务名称
eureka:
client:
service-url: # eureka的地址信息
defaultZone: http://127.0.0.1:10086/eureka
eureka配置完成
启动服务,打开网页
配置服务注册
为两个服务添加依赖
<!--eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
进行配置:
注意:配置的格式要对,让文件可以识别
对eureka进行单独的注册:
eureka:
client:
service-url: # eureka的地址信息
defaultZone: http://127.0.0.1:10086/eureka
服务名称
spring:
application:
name: xxxserver # xxx(order或者user)的服务名称
配置完成
启动多个实例
在最下方的server中,选中右键,复制配置
弹出窗口选择修改选项
添加虚拟机选项
输入-Dserver.port指定不冲突的端口(-D代表参数)