在IDEA上使用springcloud构建单节点的服务消费者
Posted lu51211314
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在IDEA上使用springcloud构建单节点的服务消费者相关的知识,希望对你有一定的参考价值。
1、步骤
一直下一步到结束
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
修改pom.xml文件(参考:https://blog.csdn.net/quanqxj/article/details/79877719)
工程的结构
配置文件
spring.application.name=spring-cloud-consumer server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
启动工程代码
package com.demo.springcloudconsumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class SpringcloudConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudConsumerApplication.class, args); } }
feign调用实现
package com.demo.springcloudconsumer.core; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name="spring-cloud-producer") public interface HelloRemote { @RequestMapping(value = "/hello") String hello(@RequestParam(value = "name") String name); }
web层调用远程服务
package com.demo.springcloudconsumer.core; 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 ConsumerController { @Autowired HelloRemote HelloRemote; @RequestMapping("/hello/{name}") public String index(@PathVariable("name") String name) { return HelloRemote.hello(name); } }
以上是关于在IDEA上使用springcloud构建单节点的服务消费者的主要内容,如果未能解决你的问题,请参考以下文章