使用IDEA搭建springcloud微服务(四)----微服务消费方cloud-client

「爱情、让人受尽委屈。」 2022-02-17 11:00 442阅读 0赞

一、工具及说明

开发工具:IntelliJ IDEA 2018.2.2 (Ultimate Edition)
框架:spring boot 2.0.8、spring cloud Finchley.SR2

以通用户ID获取用户信息为例,搭建一套spring cloud微服务系统。
需要搭建一个父工程spring-cloud,一个服务注册中心eureka-server,两个微服务cloud-client,cloud-provider。
两个微服务均注册到服务注册中心。

二、微服务消费方的搭建" class="reference-link">watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NzdXp4bTIwMDA_size_16_color_FFFFFF_t_70 二、微服务消费方的搭建

消费方cloud-client的搭建,基本和服务方cloud-provider一样,还是写全吧。

1.File—>New—>Module

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NzdXp4bTIwMDA_size_16_color_FFFFFF_t_70 1
2.选择Spring Initializr,选择对应的JDK,
Choose Initializr Server URL 选择 default。
Next。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NzdXp4bTIwMDA_size_16_color_FFFFFF_t_70 2
3.输入项目组Group:com.cloud。
组件名称Artifact:cloud-client。
Type:选择Maven Project。
修改自动生成的Package。
Next。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NzdXp4bTIwMDA_size_16_color_FFFFFF_t_70 3
4.dependencies选择Cloud Discovery—>Eureka Discovery,Cloud Routing—>Feign。
Spring Boot选择你需要的版本,我这选择2.0.8。
Next。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NzdXp4bTIwMDA_size_16_color_FFFFFF_t_70 4
5.Project Name一般不做修改,和组件名称Artifact一样。
Content root、Module file location 均按自动生成,不做修改。
Finish。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NzdXp4bTIwMDA_size_16_color_FFFFFF_t_70 5
6.配置。
将自动生成的application.properties更改为application.yml文件,个人习惯使用yml文件。
rename的快捷键是Shift+F6。
在application.yml中加入以下配置:

  1. server:
  2. port: 8082
  3. spring:
  4. application:
  5. name: cloud-client
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://user:123456@localhost:8080/eureka/ #服务注册中信地址,含有BASIC认证的用户名和密码
  10. instance:
  11. prefer-ip-address: true #将IP注册到服务注册中心
  12. #放开所有节点
  13. management:
  14. endpoints:
  15. web:
  16. exposure:
  17. include: '*'

7.修改pom文件。
可以发现,pom文件中已自动引入了Eureka客户端、Feign模块依赖。
将按以下修改,使用父工程spring-cloud的spring boot依赖。
如果需要使用/health进行健康检查,则加入健康检查模块。
如果需使用Tomcat运行,需要加入tomcat支持模块和web模块。

  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"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <!-- 引入父工程的spring boot依赖 -->
  6. <parent>
  7. <groupId>com.cloud</groupId>
  8. <artifactId>spring-cloud</artifactId>
  9. <version>1.0</version>
  10. </parent>
  11. <groupId>com.cloud</groupId>
  12. <artifactId>cloud-client</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>cloud-client</name>
  15. <description>Demo project for Spring Boot</description>
  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>Finchley.SR2</spring-cloud.version>
  21. </properties>
  22. <dependencies>
  23. <!--web依赖-->
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <!-- 注册中心客户端模块 -->
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  32. </dependency>
  33. <!-- 健康检查模块 -->
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-actuator</artifactId>
  37. </dependency>
  38. <!--Feign模块-->
  39. <dependency>
  40. <groupId>org.springframework.cloud</groupId>
  41. <artifactId>spring-cloud-starter-openfeign</artifactId>
  42. </dependency>
  43. <!-- tomcat支持 -->
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-tomcat</artifactId>
  47. <scope>provided</scope>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-test</artifactId>
  52. <scope>test</scope>
  53. </dependency>
  54. </dependencies>
  55. <dependencyManagement>
  56. <dependencies>
  57. <dependency>
  58. <groupId>org.springframework.cloud</groupId>
  59. <artifactId>spring-cloud-dependencies</artifactId>
  60. <version>${spring-cloud.version}</version>
  61. <type>pom</type>
  62. <scope>import</scope>
  63. </dependency>
  64. </dependencies>
  65. </dependencyManagement>
  66. <build>
  67. <plugins>
  68. <plugin>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-maven-plugin</artifactId>
  71. </plugin>
  72. </plugins>
  73. </build>
  74. </project>

8.启动类CloudClientApplication 。
在启动类上加入@EnableDiscoveryClient注解,声明该微服务注册到服务注册中心。
加入@EnableFeignClients,声明使用Feign调用接口。

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  4. @EnableFeignClients
  5. @EnableDiscoveryClient
  6. @SpringBootApplication
  7. public class CloudClientApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(CloudClientApplication.class, args);
  10. }
  11. }

9.使用Tomcat启动,需创建类ServletInitializer。

  1. import org.springframework.boot.builder.SpringApplicationBuilder;
  2. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  3. public class ServletInitializer extends SpringBootServletInitializer {
  4. @Override
  5. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  6. return application.sources(CloudClientApplication .class);
  7. }
  8. }

10.创建获取用户信息的接口,并请求服务提供方cloud-provider
10.1 项目结构

10.2 UserController

  1. import com.cloud.client.user.entity.User;
  2. import com.cloud.client.user.feign.UserFeignClient;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class UserController {
  9. @Autowired
  10. private UserFeignClient userFeignClient;
  11. @GetMapping("/user/{id}")
  12. public User findById(@PathVariable Long id) {
  13. User user = this.userFeignClient.findById(id);
  14. return user;
  15. }
  16. }

10.3 User

  1. public class User {
  2. private Long id;
  3. private String username;
  4. private String name;
  5. private Integer age;
  6. private Double balance;
  7. private String requestId;
  8. public Long getId() {
  9. return this.id;
  10. }
  11. public void setId(Long id) {
  12. this.id = id;
  13. }
  14. public String getUsername() {
  15. return this.username;
  16. }
  17. public void setUsername(String username) {
  18. this.username = username;
  19. }
  20. public String getName() {
  21. return this.name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public Integer getAge() {
  27. return this.age;
  28. }
  29. public void setAge(Integer age) {
  30. this.age = age;
  31. }
  32. public Double getBalance() {
  33. return this.balance;
  34. }
  35. public void setBalance(Double balance) {
  36. this.balance = balance;
  37. }
  38. public String getRequestId() {
  39. return requestId;
  40. }
  41. public void setRequestId(String requestId) {
  42. this.requestId = requestId;
  43. }
  44. }

10.4 UserFeignClient

  1. import com.cloud.client.user.entity.User;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.cloud.openfeign.FeignClient;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import feign.hystrix.FallbackFactory;
  10. @FeignClient(name = "cloud-provider", fallbackFactory = FeignClientFallbackFactory.class)
  11. public interface UserFeignClient {
  12. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  13. public User findById(@PathVariable("id") Long id);
  14. }
  15. /**
  16. * UserFeignClient的fallbackFactory类,该类需实现FallbackFactory接口,并覆写create方法
  17. */
  18. @Component
  19. class FeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {
  20. private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientFallbackFactory.class);
  21. @Override
  22. public UserFeignClient create(Throwable cause) {
  23. return new UserFeignClient() {
  24. @Override
  25. public User findById(Long id) {
  26. FeignClientFallbackFactory.LOGGER.info("fallback; reason was:", cause);
  27. User user = new User();
  28. user.setId(-1L);
  29. user.setUsername("默认用户");
  30. user.setAge(0);
  31. user.setBalance((double) 0);
  32. return user;
  33. }
  34. };
  35. }
  36. }

发表评论

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

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

相关阅读

    相关 springcloud服务-项目

    前言:      乐优商城这个视频还可以,于是拿来练练手,我对着视频搭环境一直在service服务模块卡住了,注册中心和网关可以启动,服务模块却一直启动不了,报各种奇怪的错,

    相关 SpringCloud服务框架

    一、微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群