128、springcloud实践eureka-client

eureka-client 是一个springboot服务,

会完成相应的业务模块功能的时候,

也会与eureka的注册中心进行心跳通信

1、springboot的主类

package com.springcloudtest.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication

public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }

}

2、pom.xml文件中添加eureka-client的maven依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


        <!-- feign依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>



    </dependencies>

 

 

 

3、application.yml文件中添加eureka的注册中心的地址

将当前的服务注册到eureka的注册中心上

还要写上当前服务的名称

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8181/eureka
spring:
  application:
    name: eureka-client-goods
server:
  port: 8183

 

4、IndexController提供查询所有商品信息的功能

package com.springcloudtest.eurekaclient.controller;


import com.springcloudtest.eurekaclient.domain.GoodsFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/queryGoods")
public class IndexController {

    @Autowired
    public GoodsFactory goodsFactory;

    private static Logger logger = LoggerFactory.getLogger(IndexController.class);

    @ResponseBody
    @RequestMapping(value = "/allGoods", method = RequestMethod.GET)
    public Object allGoods() {
        return goodsFactory.getGoodsMap();
    }


}

 

 

4、启动当前服务,会到当前服务在中心进行注册

 

可以看到当前,有两个Springboot服务注册到了eureka的注册中心上面

一个服务的名称是eureka-client-goods

另外一个服务的名称是eureka-client-orders

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值