[bigdata-108] spring-cloud-04 分布式服务的feign消费者
1. 本例,以feign模式,建立一个调用分布式服务的web服务。这个服务调用前三个步骤建立起来的分布式服务。 2. 参考文献 https://github.com/dyc87112/SpringCloudBook/tree/master/feign-consumer 3. 源码目录结构 ├── pom.xml 4. pom.xml <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.brian.demo</groupId> <artifactId>helloserviceconsumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>helloserviceconsumer</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <repositories> <repository> <id>my-nexus-central</id> <name>my local nexus</name> <url>http://localhost:8081/nexus/content/repositories/central/</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> </repository> </repositories> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 5. App.java package com.brian.demo.helloserviceconsumer; 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; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class App { @Autowired HelloService helloService; @RequestMapping(value = "/feign-consumer",method = RequestMethod.GET) public String helloConsumer() { return helloService.hello(); } public static void main(String[] args) { SpringApplication.run(App.class,args); } } package com.brian.demo.helloserviceconsumer; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(value = "HELLO-SERVICE") public interface HelloService { @RequestMapping("/hello") String hello(); } 7.?application.properties spring.application.name=feign-consumer server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/ mvn clean package 9. 运行 ?java -jar target/helloserviceconsumer-0.0.1-SNAPSHOT.jar 10. 查看 http://localhost:9001/feign-consumer 能看到这里返回的字符串是前述部署的服务返回的字符串。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |