第七章 SpringCloud之非声明式RestClient:Feign
时间:2019-03-18 23:01:49
收藏:0
阅读:207
1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test</groupId> <artifactId>eureka-client-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-client-feign</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <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-server</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> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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>
2、添加application.yml
spring:
application:
name: eureka-client-feign #应用名
logging: #logging日志配置
level:
root: INFO
org.hibernate: INFO
server:
port: 8666 #服务端口
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8661/eureka
3、启动类添加注解
package com.test.eurekaclientfeign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class EurekaClientFeignApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientFeignApplication.class, args); } }
4、创建UserFeignClient.java,这个类配置Feign远程调用的URL
package com.test.eurekaclientfeign.feign; import com.test.eurekaclientfeign.entity.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient("eureka-client-user") //指明feign远程调用的虚拟主机名 public interface UserFeignClient { //远程服务自己设置的urn,目前feign只支持RequestMapping注解 @RequestMapping(method = RequestMethod.GET, value = "/user/{id}") User findById(@PathVariable("id") Long id); }
5、创建MovieController.java,这里是本机提供服务的类,也是调用远程服务的调用方。
package com.test.eurekaclientfeign.controller; import com.test.eurekaclientfeign.entity.User; import com.test.eurekaclientfeign.feign.UserFeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class MovieController { @Autowired private UserFeignClient userFeignClient; /* @Autowired private FeignClient1 feignClient1;*/ @GetMapping(value = "/movie/{id}") public User findById(@PathVariable("id") Long id) { return this.userFeignClient.findById(id); } /*@GetMapping(value = "/{serviceName}") public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName) { System.out.println("11111111"); return this.feignClient1.findServiceInfoFromEurekaByServiceName(serviceName); }*/ }
6、创建User.java

package com.test.eurekaclientfeign.entity; import java.io.Serializable; import java.math.BigDecimal; public class User implements Serializable { private Long id; private String name; private String username; private BigDecimal balance; private short age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public BigDecimal getBalance() { return balance; } public void setBalance(BigDecimal balance) { this.balance = balance; } public short getAge() { return age; } public void setAge(short age) { this.age = age; } }
7、访问URL
http://localhost:8661/ #服务发现 http://192.168.137.1:8666/movie/1 #Feign测试接口
至此,已实现Feign的基本调用。
原文:https://www.cnblogs.com/ywjfx/p/10555816.html
评论(0)