前言
上一小节我们学习了在SpringCloud微服务架构下使用自定义的FeignClient来完成各个模块间的通信。本小节来学习一下如何使用SpringCloud微服务架构下使用自定义的FeignClient来完成微服务模块之外的通信。
本小节案例基于 SpringCloud微服务 之Feign(二-Customize)
场景说明:在SpringCloud微服务架构下使用自定义的FeignClient来完成微服务模块之外的通信,以访问Eureka注册表中的节点信息为例,同时实现使用自定义的FeignClient来完成微服务模块之外通信的authentication。
案例
-
Eureka Server端编写:
-
项目结构
-
CoreCode
@SpringBootApplication @EnableEurekaServer public class MicroserviceDealEurekaAuthenticationApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDealEurekaAuthenticationApplication.class, args); } }
@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { /** * 高版本的丢弃了 * * security: basic: enabled: true * * 配置,应该使用以下方式开启 * * @param http * @throws Exception */ @Override protected void configure(HttpSecurity http) throws Exception { // Configure HttpSecurity as needed (e.g. enable http basic). http.sessionManagement().sessionCreationPolicy( SessionCreationPolicy.NEVER); http.csrf().disable(); // 注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ // 这种方式登录,所以必须是httpBasic, // 如果是form方式,不能使用url格式登录 http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); } }
spring: security: basic: enabled: true # 开启基于HTTP basic的认证 user: name: Dustyone # 配置登录的账号是Dustyone password: bai5331359 # 配置登录的密码是bai5331359 server: port: 8080 # 指定该Eureka实例的端口 eureka: server: enableSelfPreservation: true client: registerWithEureka: false #Server端不做自我注册 fetchRegistry: false serviceUrl: #defaultZone: http://localhost:8080/eureka/ defaultZone: http://Dustyone:bai5331359@localhost:8080/eureka/ healthcheck: enabled: true #开启健康检查
-
-
Eureka Client端服务提供方编写。
-
项目结构
-
CordeCode
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients //开启FeignClient注解 public class MicroserviceDealBrokerFeignCustomizedExternalApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDealBrokerFeignCustomizedExternalApplication.class, args); } }
@Configuration public class FeignConfiguration { /** * Feign默认使用的是SpringMVC的contract并支持所有SpringMVC contract支持的注解 * Feign默认使用的是Feign自己封装的feignContract(MVCcontract)若是用Feign的Contract则自定义的Feign interface中需要使用Feign自己的mvc contract * @return */ @Bean public Contract feignContract() { return new feign.Contract.Default(); } /** * 将RequestInterceptor添加到RequestInterceptor的集合中 * @return */ @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("Dustyone", "bai5331359"); } }
/** * 使用FeignClient访问外部连接是需要在@FeignClient总添加url节点,若访问的外部链接需要做身份认证则需要Feign先实现BasicAuthRequestInterceptor * @author Dustyoned * */ @FeignClient(name="Deom",url="http://localhost:8080",configuration=FeignConfiguration.class) public interface ExternalFeignClientInterface { @RequestLine("GET /eureka/apps/{serviceName}") public String findServiceInfoFromEurekaByServiceName(@Param(value="serviceName") String serviceName); }
@RestController public class BrokerController { @Autowired private ExternalFeignClientInterface externalFeignClientInterface; @GetMapping("/findServicenInfo/{serviceName}") public String findServiceInfoByServiceName(@PathVariable("serviceName") String serviceName){ return this.externalFeignClientInterface.findServiceInfoFromEurekaByServiceName(serviceName); } }
server: port: 8082 spring: application: name: microservice-deal-broker-cloud-feign-cusomized-external eureka: client: serviceUrl: #defaultZone: http://localhost:8080/eureka/ defaultZone: http://Dustyone:bai5331359@localhost:8080/eureka/ instance: prefer-ip-address: true #使用Feign时必须添加以下两项配置 ribbon: eureka: enabled: true #设置feign的hystrix响应超时时间(必须) hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 5000 feign: httpclient: 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