美文网首页
02、SpringCloud Eureka Client

02、SpringCloud Eureka Client

作者: adced06edef5 | 来源:发表于2020-03-29 13:08 被阅读0次

    一、代码实例

    说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5
    Euraka Client是将提供的服务注册到Eureka供消费者调用,主要以REST方式
    1.maven依赖

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <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>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    

    2.application.yml配置

    server:
      port: 8001
    spring:
      application:
        name: client
    eureka:
      instance:
        hostname: localhost
        prefer-ip-address: true
        instance-id: client-8001
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka/
    #info信息
    info:
      app:
        name: client-8001
      company:
        name: www.xxx.com
      build:
        artifactId: ${project.artifactId}
        version: ${project.version}
    

    说明一下:此版本的artifactId可以通过${xxx}或@xxx@方式获取,建议用${xxx}方式,@xxx@后续动态获取配置时会报一个错误,之前版本应该是$xxx$方式
    3.启动类
    此版本无需添加@EnableEurekaClient

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

    4、controller类
    提供rest服务

    @RestController
    public class HelloController {
        @GetMapping("/hello/{name}")
        public String hello(@PathVariable String name){
            return "hello,"+name;
        }
    }
    

    二、测试验证

    先后启动server、client服务访问http://eureka7001:7001/

    image.png
    服务注册成功
    image.png
    鼠标点击client-8001可以查看application.yml中配置的info信息
    image.png
    测试client提供的服务
    访问http://localhost:8001/hello/zs
    结果如下:
    image.png
    参考:
    https://blog.csdn.net/forezp/article/details/70148833
    http://www.itmuch.com/spring-cloud/spring-cloud-index/
    还有尚硅谷周阳老师的视频

    相关文章

      网友评论

          本文标题:02、SpringCloud Eureka Client

          本文链接:https://www.haomeiwen.com/subject/autwshtx.html