2、spring cloud服务注册中心eureka---服务消费者(第二章)

谁践踏了优雅 2022-04-18 00:46 286阅读 0赞

服务调用

本文中示例代码的引用版本:
org.springframework.boot 版本 :2.1.0.RELEASE
org.springframework.cloud 版本:Greenwich.M1
示例代码-码云 https://gitee.com/sharps/springcloud

1、pom包配置
和服务提供者一致

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.llx.springcloud</groupId>
  5. <artifactId>spring-cloud-consumer</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>spring-cloud-consumer</name>
  9. <description>Demo spring-cloud-consumer</description>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.1.0.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. <spring-cloud.version>Greenwich.M1</spring-cloud.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-openfeign</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-web</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-test</artifactId>
  38. <scope>test</scope>
  39. </dependency>
  40. <!--<dependency>-->
  41. <!--<groupId>org.springframework.boot</groupId>-->
  42. <!--<artifactId>spring-boot-autoconfigure</artifactId>-->
  43. <!--</dependency>-->
  44. </dependencies>
  45. <dependencyManagement>
  46. <dependencies>
  47. <dependency>
  48. <groupId>org.springframework.cloud</groupId>
  49. <artifactId>spring-cloud-dependencies</artifactId>
  50. <version>${spring-cloud.version}</version>
  51. <type>pom</type>
  52. <scope>import</scope>
  53. </dependency>
  54. </dependencies>
  55. </dependencyManagement>
  56. <build>
  57. <plugins>
  58. <plugin>
  59. <groupId>org.springframework.boot</groupId>
  60. <artifactId>spring-boot-maven-plugin</artifactId>
  61. </plugin>
  62. </plugins>
  63. </build>
  64. <repositories>
  65. <repository>
  66. <id>spring-milestones</id>
  67. <name>Spring Milestones</name>
  68. <url>https://repo.spring.io/milestone</url>
  69. <snapshots>
  70. <enabled>false</enabled>
  71. </snapshots>
  72. </repository>
  73. </repositories>
  74. </project>

2、配置文件
application.properties配置如下:

  1. spring.application.name=spring-cloud-consumer
  2. server.port=9091
  3. eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/

3、启动类
启动类添加@EnableDiscoveryClient和@EnableFeignClients注解。

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableFeignClients
  4. public class ConsumerApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ConsumerApplication.class, args);
  7. }
  8. }

@EnableDiscoveryClient :启用服务注册与发现
@EnableFeignClients:启用feign进行远程调用
Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

4、feign调用实现

  1. @FeignClient(name= "spring-cloud-producer")
  2. public interface HelloRemote {
  3. @RequestMapping(value = "/hello")
  4. public String hello(@RequestParam(value = "name") String name);
  5. }

name:远程服务名,及spring.application.name配置的名称
此类中的方法和远程服务中contoller中的方法名和参数需保持一致。

5、web层调用远程服务
将HelloRemote注入到controller层,像普通方法一样去调用即可。

  1. @RestController
  2. public class ConsumerController {
  3. @Autowired
  4. HelloRemote HelloRemote;
  5. @RequestMapping("/hello/{name}")
  6. public String index(@PathVariable("name") String name) {
  7. return HelloRemote.hello(name);
  8. }
  9. }

到此,最简单的一个服务注册与调用的例子就完成了。

####测试
简单调用
依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目

先输入:http://localhost:9000/hello?name=llx 检查spring-cloud-producer服务是否正常

返回:hello llx,this is first messge

说明spring-cloud-producer正常启动,提供的服务也正常。

浏览器中输入:http://localhost:9091/hello/llx

返回:hello llx,this is first messge

说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。

发表评论

表情:
评论列表 (有 0 条评论,286人围观)

还没有评论,来说两句吧...

相关阅读