• spring-cloud-eureka-client-consumer


    服务注册中心eureka-server已经搭好,并且SPRING-CLOUD-NETFLIX-EUREKA-CLIENT-APPLICATION提供一个hello服务

    编写一个eureka-client-consumer服务消费者,去消费该服务

    一、新建module,选择对应的springcloud模块,pom.xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>lf.liyouyou</groupId>
            <artifactId>spring-cloud-netflix-demo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <groupId>lf.youyou</groupId>
        <artifactId>spring-cloud-eureka-client-consumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-cloud-eureka-client-consumer</name>
        <description>Demo project for Spring Boot</description>
    
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</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>
        </dependencies>
    
    </project>

    二、启动类添加注解

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

    三、编写调用hello服务代码

      首先编写一个可以远程调用服务feign接口,name属性配置需要调用的服务名称

    package lf.youyou.lf.com;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient(name="spring-cloud-netflix-eureka-client-application")
    public interface HelloRemote {
    
        @RequestMapping(value = "/hello")
        public String hello(@RequestParam(value = "name") String name);
    }

      Controller

      

    package lf.youyou.lf.com;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @Autowired
        HelloRemote helloRemote;
    
        @RequestMapping("/hello/{name}")
        public String index(@PathVariable("name") String name) {
            return helloRemote.hello(name);
        }
    }

    四、配置application.properties

    spring.application.name=spring-cloud-netflix-eureka-client-consumer
    server.port=9090
    eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

    启动服务,

    访问注册中心 http://localhost:8000/ 查看eureka面板

    再访问http://localhost:9090/hello/lf

    返回:hello lf,nice to meet you!

  • 相关阅读:
    12.7 Test
    51Nod.1766.树上最远点对(树的直径 RMQ 线段树/ST表)
    BZOJ.3675.[APIO2014]序列分割(DP 斜率优化)
    BZOJ.4515.[SDOI2016]游戏(树链剖分 李超线段树)
    BZOJ.3165.[HEOI2013]Segment(李超线段树)
    Linux系统CentOS进入单用户模式和救援模式详解
    KVM 管理界面挂载多UKEY
    挂载银行前置机Ukey到windows server2012虚拟机的操作记录
    为什么服务器做了raid 系统文件还会丢失?
    LVS+Keepalived深度理解,阐述你不知道的坑点
  • 原文地址:https://www.cnblogs.com/flgb/p/13802515.html
Copyright © 2020-2023  润新知