SpringCloud Eureka微服务(二)

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。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值