当前位置:   article > 正文

Eureka快速入门——创建服务提供者_euekaclient创建依赖

euekaclient创建依赖

关于服务提供者的创建较简单,小编就少说点废话,直接上代码,跟着敲一敲相信聪明的小伙伴们就能一目了然了。

创建服务提供者

1.创建eureka-client模块并添加依赖依赖

	<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-web</artifactId>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.创建main方法:

/**
 *  Create By 小飞龙
 */
@SpringBootApplication
@EnableDiscoveryClient //服务发现注解 从注册中心拉取注册列表
public class EurekaClientApplication {
    public static void main(String[] args) {
        
        new SpringApplicationBuilder(EurekaClientApplication.class)
                .web(WebApplicationType.SERVLET)
                .run(args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.创建Entity:

@Data
public class User {
    private String name;
    private String port;
}
  • 1
  • 2
  • 3
  • 4
  • 5

4.创建Controller:

@RestController
@Slf4j
public class Controller {

    @Value("${server.port}")
    private String port;

    @GetMapping("/sayHi")
    public String sayHi() {
        return "This is " + port;
    }

    @PostMapping("/sayHi")
    public User sayHiPost(@RequestBody User user) {
        log.info("You are " + user.getName());
        user.setPort(port);
        return user;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

5.添加配置文件:

spring:
  application:
    name: eureka-client

server:
  port: 30000
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:20000/eureka/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

6.启动注册中心与服务提供者在这里插入图片描述

7.访问localhost:20000

在这里插入图片描述
可以看到我们的服务提供者已经注册进来了。

8.打开Postman测试接口

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/903452
推荐阅读
相关标签
  

闽ICP备14008679号