从零搭建springboot+springcloud+euerka+feign项目

从零搭建springboot+springcloud+euerka+feign项目

前言

        最近准备写个小项目,想用springboot+springcloud,注册中心选用euerka,对于服务调用选用了风格比较符合平时开发习惯的feign,选择的原因很简单,之前没用过练习一下(手动滑稽),内容比较全,遇到的各种小坑都找到了对应的解决方案,本次搭建过程中遇见的坑全部红色加粗提示,废话不说,开始搭建。

准备

1. maven: 3.5.3

2. jdk: 1.8

3. 开发工具: Idea

目录结构

虽然四个project放在一起了,但我是各自维护pom.xml的

一、搭建注册中心euerka(这步没有坑,很简单就搭好了

1. 修改pom.xml


   
   
   
   
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. 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. <groupId>com.joab.anime groupId>
  6. <artifactId>one-for-all-eureka artifactId>
  7. <version>1.0.0-SNAPSHOT version>
  8. <parent>
  9. <groupId>org.springframework.boot groupId>
  10. <artifactId>spring-boot-starter-parent artifactId>
  11. <version>2.0.3.RELEASE version>
  12. <relativePath/>
  13. parent>
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8 project.build.sourceEncoding>
  16. <project.reporting.outputEncoding>UTF-8 project.reporting.outputEncoding>
  17. <java.version>1.8 java.version>
  18. <spring-cloud.version>Finchley.RELEASE spring-cloud.version>
  19. properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud groupId>
  23. <artifactId>spring-cloud-starter-netflix-eureka-server artifactId>
  24. dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot groupId>
  27. <artifactId>spring-boot-starter-test artifactId>
  28. <scope>test scope>
  29. dependency>
  30. dependencies>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.cloud groupId>
  35. <artifactId>spring-cloud-dependencies artifactId>
  36. <version>${spring-cloud.version} version>
  37. <type>pom type>
  38. <scope>import scope>
  39. dependency>
  40. dependencies>
  41. dependencyManagement>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot groupId>
  46. <artifactId>spring-boot-maven-plugin artifactId>
  47. plugin>
  48. plugins>
  49. build>
  50. project>

2. 编辑application.yml


   
   
   
   
  1. server:
  2. port: 8761
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. registerWithEureka: false
  8. fetchRegistry: false
  9. serviceUrl:
  10. defaultZone: http: //${eureka.instance.hostname}:${server.port}/eureka/
  11. spring:
  12. application:
  13. name: demo-eureka

3. 编辑EurekaApplication.java


   
   
   
   
  1. @EnableEurekaServer
  2. @SpringBootApplication
  3. public class EurekaApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaApplication.class);
  6. }
  7. }

4. 启动EurekaApplication,浏览器输入http://localhost:8761/,显示如下证明euerka搭建成功

二、提供服务接口api(该步骤目的是接口与实现分离

1. 修改pom.xml


   
   
   
   
  1. xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0 modelVersion>
  6. <groupId>com.joaby.springclouddemo groupId>
  7. <artifactId>api artifactId>
  8. <version>1.0-SNAPSHOT version>
  9. <parent>
  10. <groupId>org.springframework.boot groupId>
  11. <artifactId>spring-boot-starter-parent artifactId>
  12. <version>2.0.3.RELEASE version>
  13. <relativePath/>
  14. parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8 project.build.sourceEncoding>
  17. <project.reporting.outputEncoding>UTF-8 project.reporting.outputEncoding>
  18. <java.version>1.8 java.version>
  19. properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot groupId>
  23. <artifactId>spring-boot-starter-web artifactId>
  24. dependency>
  25. dependencies>
  26. project>

2. 提供api接口


   
   
   
   
  1. public interface IDemoApi {
  2. /**
  3. * 入参为字段,返回值为字段
  4. */
  5. @RequestMapping(value= "/demo-api/test", method=RequestMethod.GET)
  6. String test(@RequestParam(value = "test") String test);
  7. /**
  8. * 入参为对象,返回值为对象
  9. */
  10. @RequestMapping(value= "/demo-api/user", method=RequestMethod.POST)
  11. User user(@RequestBody User user);
  12. }

注意:

1)不支持@GetMapping @PostMapping,只能用@RequestMapping,通过method指定请求方式;

2)参数传递必须用@RequestParam(value = "test") 注解修饰;

3)传递的参数为对象,必须用@RequestBody修饰;

4)返回值若为对象,对象必须序列化,且必须提供public修饰的无参构造方法(默认是提供的),会报错com.fasterxml.jackson.databind.exc.InvalidDefinitionException,原因是jackson的反序列化需要无参构造函数

*5)尽管路径均为“/demo-api/xxx”,但是笔者在类上使用注解@RequestMapping(value="/demo-api")就无法调通,请读者补充

二、提供服务提供者实现

1. 修改pom.xml


   
   
   
   
  1. xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0 modelVersion>
  6. <groupId>com.joaby.springclouddemo groupId>
  7. <artifactId>provider artifactId>
  8. <version>1.0-SNAPSHOT version>
  9. <parent>
  10. <groupId>org.springframework.boot groupId>
  11. <artifactId>spring-boot-starter-parent artifactId>
  12. <version>2.0.3.RELEASE version>
  13. <relativePath/>
  14. parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8 project.build.sourceEncoding>
  17. <project.reporting.outputEncoding>UTF-8 project.reporting.outputEncoding>
  18. <java.version>1.8 java.version>
  19. <spring-cloud.version>Finchley.RELEASE spring-cloud.version>
  20. properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>com.joaby.springclouddemo groupId>
  24. <artifactId>api artifactId>
  25. <version>1.0-SNAPSHOT version>
  26. dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud groupId>
  29. <artifactId>spring-cloud-starter-netflix-eureka-client 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. dependencies>
  41. <dependencyManagement>
  42. <dependencies>
  43. <dependency>
  44. <groupId>org.springframework.cloud groupId>
  45. <artifactId>spring-cloud-dependencies artifactId>
  46. <version>${spring-cloud.version} version>
  47. <type>pom type>
  48. <scope>import scope>
  49. dependency>
  50. dependencies>
  51. dependencyManagement>
  52. <build>
  53. <plugins>
  54. <plugin>
  55. <groupId>org.springframework.boot groupId>
  56. <artifactId>spring-boot-maven-plugin artifactId>
  57. plugin>
  58. plugins>
  59. build>
  60. project>

2. 编辑application.yml


   
   
   
   
  1. server:
  2. port: 8091
  3. eureka:
  4. #eueka 主机名
  5. instance:
  6. hostname: localhost
  7. client:
  8. #提供者和消费者的注册地址
  9. service-url:
  10. defaultZone: http: //${eureka.instance.hostname}:8761/eureka/
  11. spring:
  12. application:
  13. name: demo-provider

3. 实现服务


   
   
   
   
  1. @RestController
  2. public class DemoProvider implements IDemoApi {
  3. @Override
  4. public String test(String test) {
  5. return "test: " + test;
  6. }
  7. @Override
  8. public User user(@RequestBody User user) {
  9. if (user == null) {
  10. user = new User( 10, "Joab-Y");
  11. }
  12. return user;
  13. }
  14. }

3. 编辑ProviderApplication.java


   
   
   
   
  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class ProviderApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ProviderApplication.class);
  6. }
  7. }

4. 启动ProviderApplication,浏览器输入http://localhost:8091/demo-api/test?test=123,显示如下,服务提供成功

注意:

1)实现类必须用@RestController注解修饰,不然他会去找模板;

2)传递参数如果为对象,该对象必须再次用@RequestBody修饰,不然字段带不过来,仍为null;

四、搭建消费者

1. 修改pom.xml


   
   
   
   
  1. xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0 modelVersion>
  6. <groupId>com.joaby.springclouddemo groupId>
  7. <artifactId>customer artifactId>
  8. <version>1.0-SNAPSHOT version>
  9. <parent>
  10. <groupId>org.springframework.boot groupId>
  11. <artifactId>spring-boot-starter-parent artifactId>
  12. <version>2.0.3.RELEASE version>
  13. <relativePath/>
  14. parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8 project.build.sourceEncoding>
  17. <project.reporting.outputEncoding>UTF-8 project.reporting.outputEncoding>
  18. <java.version>1.8 java.version>
  19. <spring-cloud.version>Finchley.RELEASE spring-cloud.version>
  20. properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot groupId>
  24. <artifactId>spring-boot-starter-web artifactId>
  25. dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot groupId>
  28. <artifactId>spring-boot-starter-test artifactId>
  29. <scope>test scope>
  30. dependency>
  31. <dependency>
  32. <groupId>org.springframework.cloud groupId>
  33. <artifactId>spring-cloud-starter-netflix-eureka-client artifactId>
  34. dependency>
  35. <dependency>
  36. <groupId>org.springframework.cloud groupId>
  37. <artifactId>spring-cloud-starter-openfeign artifactId>
  38. dependency>
  39. <dependency>
  40. <groupId>com.joaby.springclouddemo groupId>
  41. <artifactId>api artifactId>
  42. <version>1.0-SNAPSHOT version>
  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. project>

2. 编辑application.yml


   
   
   
   
  1. server:
  2. port: 8081
  3. spring:
  4. application:
  5. name: demo-customer
  6. eureka:
  7. #eueka 主机名
  8. instance:
  9. hostname: localhost
  10. client:
  11. #提供者和消费者的注册地址
  12. serviceUrl:
  13. defaultZone: http: //${eureka.instance.hostname}:8761/eureka/
  14. feign:
  15. hystrix:
  16. enabled: true

3. 编辑DemoFeignService接口,继承服务api


   
   
   
   
  1. // name: 服务者application.yml中的spring.application.name
  2. // fallback: 断路器执行方法,即方法执行失败调用
  3. @FeignClient(name= "demo-provider", fallback = DemoServiceFallback.class)
  4. public interface DemoFeignService extends IDemoApi {
  5. }

4. 实现断路器类


   
   
   
   
  1. @Component
  2. public class DemoServiceFallback implements DemoFeignService {
  3. @Override
  4. public String test(String test) {
  5. return "error";
  6. }
  7. @Override
  8. public User user(User user) {
  9. return null;
  10. }
  11. }

5. 前端调用


   
   
   
   
  1. @RestController
  2. public class DemoController {
  3. @Autowired
  4. public DemoFeignService demoFeignService;
  5. @RequestMapping( "/test")
  6. public String test() {
  7. return demoFeignService.test( "test");
  8. }
  9. @RequestMapping( "/user")
  10. public User user() {
  11. User user = new User();
  12. user.setAge( 10);
  13. user.setName( "Joab-Y");
  14. return demoFeignService.user(user);
  15. }
  16. }

6. 编辑CustomerApplicaton.java


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

7. 启动CustomerApplicaton

浏览器输入http://localhost:8081/test,显示如下

浏览器输入http://localhost:8081/user,显示如下

若断开provider,浏览器输入http://localhost:8081/test,显示如下(断路器生效)

完结撒花~

你可能感兴趣的:(从零搭建springboot+springcloud+euerka+feign项目)