EurekaServer服务及微服务(80、8001)入驻
EurekaServer服务
和之前一样也是一个Springboot项目。
一、pom.xml
最主要的是这里需要引入eureka的服务依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
二、application.yml
server:
port: 7001
#eureka服务端的实例名称
eureka:
instance:
hostname: eureka7001.com
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去搜索服务
fetch-registry: false
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
三、启动类
这里需要添加注解@EnableEurekaServer
注明这是一个EurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
启动项目访问http://eureka7001.com:7001看到下面的界面证明Eureka服务启动成功。
支付微服务8001入驻EurekaServer
一、pom.xml
添加eureka-client依赖包
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
二、application.yml
添加eureka的配置参数
eureka:
client:
#表示是否将自己注册进eurekaServer 默认为true
register-with-eureka: true
#是否从eurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka/
instance:
instance-id: payment8001
prefer-ip-address: true
三、启动类
这里需要添加注解是@EnableEurekaClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaClient
@EnableDiscoveryClient
public class PaymentMain8001 {
public static void main(String[] args){
SpringApplication.run(PaymentMain8001.class, args);
}
}
订单微服务80入驻EurekaServer
这里和上面同样的操作,添加对应的注解以及依赖就ok。
测试
先启动EurekaServer服务,再启动微服务8001和微服务80。访问http://eureka7001.com:7001看到界面显示就证明我们的微服务已经入驻进EurekaServer。