springcloud学习(三)eureka client定位与接口API

eureka client

eureka作为注册中心,任何注册到其上面的服务,都可以被称之为client。不管是服务的提供者,还是服务的消费者。

搭建client步骤

  1. 搭建工程,比如user-service 或者payment-service。(用户服务、支付服务)
  2. 引用eureka client的包。
  3. 配置yml。
  4. 在springboot启动类增加注解
    @EnableEurekaClient

yml配置参考

server:
  port: 9002
spring:
  application:
    name: hello-server
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
eureka:
  instance:
    status-page-url: http://localhost:${server.port}/swagger-ui/index.html
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

调用接口


调用接口需要先进行引用,新建接口类:

@FeignClient("hello-server")
public interface UserClients {
    @GetMapping("/user/getUserList")
    String findOrderList();
}

FeignClient中的hello-server是接口被调用工程的工程名,findOrderList()方法上的注解,代表要调用的接口方法。

其中如下代码配置是为了解决springboot 2.6.x版本启动swagger3.0.0 报null错误

mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

swagger接口API

因为在eureka的监控界面看不到每个euraka client到底注册了哪些接口?所以

笔者找了maven仓库最新的3.0.0的swagger与springboot进行集成,提供接口API,并集成到http://localhost:8761/  的界面中。

pom配置

<!--集成swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!-- API获取的包 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!-- 官方UI包 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!--集成swagger end-->

其中swagger.version  = 3.0.0

启动client后,可以访问http://localhost:9002/swagger-ui/index.html 看到接口API界面。

client yml配置中增加如下配置即可。

status-page-url: http://localhost:${server.port}/swagger-ui/index.html

此时在eureka界面点击下图中的地址即可跳转到API接口界面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java技术之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值