Feign是一个声明式的Web Service客户端,比Ribbon好用,默认也是轮巡。我们只需要使用Feign创建一个接口,并用注解就好了。
案例编写:
一:搭建Eureka服务器
目录结构:
1.1导入依赖包
<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>org.springCloud</groupId> <artifactId>first-server</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 基于springboot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> </parent> <!-- springcloud依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- eureka服务器依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
1.2编写配配置文件 在resource目录下创建application.yml文件
# 指定默认端口 server: port: 8761 # eureka默认把自己到服务器注册会报错,这里禁止掉 eureka: client: register-with-eureka: false fetch-registry: false
1.3编写启动类
package org.wulei; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication//表示这是一个springboot应用 @EnableEurekaServer// 申明这是一个eureka服务器 public class ServerApp { public static void main(String[] args) { SpringApplication.run(ServerApp.class, args); //new SpringApplicationBuilder(ServerApp.class).web(true).run(args); } }
这时,浏览器访问localhost:8761会进入eureka管控台,出现这个页面就表示启动成功了,但这个时候还没有服务注册上来,下面我们继续编写服务端与客户端。
二:编写服务提供者
目录结构:
2.1 导入依赖包
<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>org.springCloud</groupId> <artifactId>spring_feign_police</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 基于springboot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> </parent> <!-- springcloud --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- eureka客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> </project>
2.2 编写appliaction.yml配置文件
# 设置服务名称 spring: application: name: police # 设置eureka服务器的注册地址 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
2.3 编写实体类,供接口返回json字符串使用。
public class Police { private Integer id; private String name; //记录访问路径用 private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2.4 Controller层
import javax.servlet.http.HttpServletRequest; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class PoliceController { @RequestMapping(value = "/call/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Police call(@PathVariable Integer id,HttpServletRequest req) { Police p = new Police(); p.setId(id); p.setName("吴磊"); p.setMessage(req.getRequestURL().toString()); return p; } @RequestMapping("/hello/{name}") public String hello(@PathVariable String name){ return "hello"+name; } }
2.5 编写启动类
import java.util.Scanner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient//开启eureka服务 public class PoliceServer { public static void main(String[] args) { //new SpringApplicationBuilder(PoliceServer.class).web(true).run(args); //1. 启动main方法后,在控制台输入端口号. Scanner scan = new Scanner(System.in); String port = scan.nextLine(); //以输入的端口号启动 new SpringApplicationBuilder(PoliceServer.class) .properties("server.port="+port).run(args); //2. 启动2次,分别以8080, 8081启动,浏览器访问测试一下。 } }
分别访问8080和8081端口进行测试, 刷新eureka管控台,这里看到我们的服务已经注册上去了。
三:编写服务调用者
目录结构:
3.1 导入依赖包
<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>org.crazyit.cloud</groupId> <artifactId>spring_feign_person</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 基于springboot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> </parent> <!-- springCloud --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- eureka客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- feign客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> </project>
3.2 编写application.yml配置信息
# 指定端口 server: port: 9000 # 服务名称 spring: application: name: invoker # eureka服务器注册地址 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
3.3 我们要调用服务提供者的 host:port/call/{ id } 接口返回实体类信息,所以把之前的实体复制过来,减少系统耦合度。
public class Police { private Integer id; private String name; private String message; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
3.4 用feign注解来远程调用
import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; //指定服务提供者的服务名 @FeignClient("police") public interface HelloClient { //翻译服务提供者的地址 @RequestMapping(method = RequestMethod.GET, value="/hello/{name}") String hello(@PathVariable("name") String name); //翻译服务提供者的地址 @RequestMapping(method = RequestMethod.GET, value="/call/{id}") Police getPolice(@PathVariable("id") Integer id); }
3.5 编写controller
package org.wulei; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { //把刚才的feign注解类依赖过来 @Autowired private HelloClient helloClient; @RequestMapping(method = RequestMethod.GET, value="/router") public String router() { String result = helloClient.hello("angus"); return result; } @RequestMapping(method = RequestMethod.GET, value="/police", produces = MediaType.APPLICATION_JSON_VALUE) public Police getPolice() { Police p = helloClient.getPolice(1); return p; } }
3.6 编写主函数入口
@SpringBootApplication @EnableEurekaClient//开启eureka @EnableFeignClients//开启feign public class ServerMain { public static void main(String[] args) { new SpringApplicationBuilder(ServerMain.class).web(true).run(args); } }
3.7 测试 我们调用消费者的接口,实际是用feign远程调用的服务提供者。刷新eureka管控台,可以看到消费者注册成功了。
原文地址:https://www.cnblogs.com/wlwl/p/9475135.html
时间: 2024-08-30 18:08:56