SpringCloud微服务 之Feign(三-Customize)

淩亂°似流年 2022-05-14 15:57 351阅读 0赞

前言

上一小节我们学习了在SpringCloud微服务架构下使用自定义的FeignClient来完成各个模块间的通信。本小节来学习一下如何使用SpringCloud微服务架构下使用自定义的FeignClient来完成微服务模块之外的通信。

本小节案例基于 SpringCloud微服务 之Feign(二-Customize)

场景说明:在SpringCloud微服务架构下使用自定义的FeignClient来完成微服务模块之外的通信,以访问Eureka注册表中的节点信息为例,同时实现使用自定义的FeignClient来完成微服务模块之外通信的authentication。

案例

  • Eureka Server端编写:

    • 项目结构
      在这里插入图片描述
    • CoreCode

      1. @SpringBootApplication
      2. @EnableEurekaServer
      3. public class MicroserviceDealEurekaAuthenticationApplication {
      4. public static void main(String[] args) {
      5. SpringApplication.run(MicroserviceDealEurekaAuthenticationApplication.class, args);
      6. }
      7. }
      8. @Configuration
      9. @EnableWebSecurity
      10. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
      11. /**
      12. * 高版本的丢弃了
      13. *
      14. * security: basic: enabled: true
      15. *
      16. * 配置,应该使用以下方式开启
      17. *
      18. * @param http
      19. * @throws Exception
      20. */
      21. @Override
      22. protected void configure(HttpSecurity http) throws Exception {
      23. // Configure HttpSecurity as needed (e.g. enable http basic).
      24. http.sessionManagement().sessionCreationPolicy(
      25. SessionCreationPolicy.NEVER);
      26. http.csrf().disable();
      27. // 注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/
      28. // 这种方式登录,所以必须是httpBasic,
      29. // 如果是form方式,不能使用url格式登录
      30. http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
      31. }
      32. }
      33. spring:
      34. security:
      35. basic:
      36. enabled: true # 开启基于HTTP basic的认证
      37. user:
      38. name: Dustyone # 配置登录的账号是Dustyone
      39. password: bai5331359 # 配置登录的密码是bai5331359
      40. server:
      41. port: 8080 # 指定该Eureka实例的端口
      42. eureka:
      43. server:
      44. enableSelfPreservation: true
      45. client:
      46. registerWithEureka: false #Server端不做自我注册
      47. fetchRegistry: false
      48. serviceUrl:
      49. #defaultZone: http://localhost:8080/eureka/
      50. defaultZone: http://Dustyone:bai5331359@localhost:8080/eureka/
      51. healthcheck:
      52. enabled: true #开启健康检查
  • Eureka Client端服务提供方编写。
  • 项目结构
    在这里插入图片描述
  • CordeCode

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. @EnableFeignClients //开启FeignClient注解
    4. public class MicroserviceDealBrokerFeignCustomizedExternalApplication {
    5. public static void main(String[] args) {
    6. SpringApplication.run(MicroserviceDealBrokerFeignCustomizedExternalApplication.class,
    7. args);
    8. }
    9. }
    10. @Configuration
    11. public class FeignConfiguration {
    12. /**
    13. * Feign默认使用的是SpringMVC的contract并支持所有SpringMVC contract支持的注解
    14. * Feign默认使用的是Feign自己封装的feignContract(MVCcontract)若是用Feign的Contract则自定义的Feign interface中需要使用Feign自己的mvc contract
    15. * @return
    16. */
    17. @Bean
    18. public Contract feignContract() {
    19. return new feign.Contract.Default();
    20. }
    21. /**
    22. * 将RequestInterceptor添加到RequestInterceptor的集合中
    23. * @return
    24. */
    25. @Bean
    26. public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
    27. return new BasicAuthRequestInterceptor("Dustyone", "bai5331359");
    28. }
    29. }
    30. /**
    31. * 使用FeignClient访问外部连接是需要在@FeignClient总添加url节点,若访问的外部链接需要做身份认证则需要Feign先实现BasicAuthRequestInterceptor
    32. * @author Dustyoned
    33. *
    34. */
    35. @FeignClient(name="Deom",url="http://localhost:8080",configuration=FeignConfiguration.class)
    36. public interface ExternalFeignClientInterface {
    37. @RequestLine("GET /eureka/apps/{serviceName}")
    38. public String findServiceInfoFromEurekaByServiceName(@Param(value="serviceName") String serviceName);
    39. }
    40. @RestController
    41. public class BrokerController {
    42. @Autowired
    43. private ExternalFeignClientInterface externalFeignClientInterface;
    44. @GetMapping("/findServicenInfo/{serviceName}")
    45. public String findServiceInfoByServiceName(@PathVariable("serviceName") String serviceName){
    46. return this.externalFeignClientInterface.findServiceInfoFromEurekaByServiceName(serviceName);
    47. }
    48. }
    49. server:
    50. port: 8082
    51. spring:
    52. application:
    53. name: microservice-deal-broker-cloud-feign-cusomized-external
    54. eureka:
    55. client:
    56. serviceUrl:
    57. #defaultZone: http://localhost:8080/eureka/
    58. defaultZone: http://Dustyone:bai5331359@localhost:8080/eureka/
    59. instance:
    60. prefer-ip-address: true
    61. #使用Feign时必须添加以下两项配置
    62. ribbon:
    63. eureka:
    64. enabled: true
    65. #设置feign的hystrix响应超时时间(必须)
    66. hystrix:
    67. command:
    68. default:
    69. execution:
    70. isolation:
    71. thread:
    72. timeoutInMilliseconds: 5000
    73. feign:
    74. httpclient:
    75. enabled:true
  • 访问:http://localhost:8082/findServicenInfo/microservice-deal-broker-cloud-feign-cusomized-external
    在这里插入图片描述

小结

  • Eureka 使用 spring-boot-starter-security 来对前来注册的服务节点做身份校验,需要引入spring-boot-starter-security依赖,并且在较高的SpringCloud和SpringBoot版本中需要对Eureka Server端做额外的声明:即对前来注册的服务节点做基于httpBasic的校验而不是以form表单的方式来实现的。参考WebSecurityConfig.java。
  • 在SpringCloud微服务架构下使用自定义的FeignClient来完成微服务模块之外的通信需要的Feign的Client端做一些特殊声明即在@FeignClient注解的节点上做url的声明,若存在URL的声明FeignClient将优先定URL提供的通信连接而此时@FeignClient的name或者value节点的声明权重让位给url。
  • 若Eureka使用 spring-boot-starter-security 来对前来注册的服务节点做身份校验,前来注册的服务节点需要提供principals and credentials即认证要求的用户名和密码。
  • 本小节案例用到了:microservice-deal-eureka-authentication、microservice-deal-broker-cloud-feign-cusomized-external

发表评论

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

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

相关阅读