[bigdata-108] spring-cloud-04 分布式服务的feign消费者

来源:互联网 发布:adobe reader百度软件 编辑:程序博客网 时间:2024/05/17 08:25

1. 本例,以feign模式,建立一个调用分布式服务的web服务。这个服务调用前三个步骤建立起来的分布式服务。


2. 参考文献

https://github.com/dyc87112/SpringCloudBook/tree/master/feign-consumer


3. 源码目录结构

├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── brian
│   │   │           └── demo
│   │   │               └── helloserviceconsumer
│   │   │                   ├── App.java
│   │   │                   └── HelloService.java
│   │   └── resources
│   │       └── application.properties


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@SpringBootApplicationpublic class App {@AutowiredHelloService 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);}}


6. HelloService.java

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-consumerserver.port=9001eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/


8. 编译打包

mvn clean package


9. 运行

 java -jar target/helloserviceconsumer-0.0.1-SNAPSHOT.jar


10. 查看

http://localhost:9001/feign-consumer

能看到这里返回的字符串是前述部署的服务返回的字符串。



阅读全文
0 0
原创粉丝点击