搭建服务测试环境
测试中,服务发现层采用Netflix的Eureka搭建。
主要步骤如下:
1.引入Eureka所需依赖
<!--eureka服务端--> <dependency>
<groupId>org.springframework.cloud </groupId>
<artifactId>spring-cloud-starter-eureka-server </artifactId>
</dependency>
<!--客户端-->
<dependency>
<groupId>org.springframework.cloud </groupId>
<artifactId>spring-cloud-starter-eureka </artifactId>
</dependency>
2.修改配置文件
服务端:
eureka: instance:
hostname: eureka9001.com #eureka服务端的实例名称
instance-id: eureka9001
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false # #false 表示自己就是注册中心,职责就是维护服务实例,并不需要去检索服务
service-url:
defaulteZone: http://127.0.0.1:9001
客户端1:
server: port: 8002
spring:
application:
name: licensingservice
eureka:
instance:
instance-id: licensing-service-8002
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:9001/eureka/,
客户端2:
server: port: 8002
spring:
application:
name: licensingservice
eureka:
instance:
instance-id: licensing-service-8002
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:9001/eureka/,
一组微服务的不同实例采服务名相同,不同的实例Id区分,分别对应,spring.application.name 和eureka.instance.instance-id。
3.启动服务
服务端:
@SpringBootApplication@EnableEurekaServer
public cla