存档2020年1月10日

Feign/RestTemplate

  1. 增加依赖
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
  1. 在启动主类增加注解
@EnableFeignClients
  1. 定义feign调用商品微服务的接口
访问PRODUCT服务下的接口
@FeignClient(name = "PRODUCT")
public interface ProductClient {

    /**
     * 从商品微服务中获取一个测试信息
     * @return
     */
    @GetMapping("/msg")
    String productMsg();

    /**
     * 根据一组商品ID获取商品列表
     * @param productIdList
     * @return
     */
    @PostMapping("/listForOrder")
    List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList);

    /**
     * 扣库存
     * @param decreaseStockInputList
     */
    @PostMapping("/decreaseStock")
    void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList);
}

  1. 调用feign接口
@Api(tags="RestTemplate/feign调用微服务API")
@RestController
@Slf4j
public class ClientController {
    /*第二种方式*/
   /* @Autowired
    private LoadBalancerClient loadBalancerClient;*/

    /* 第三种方式
     @Autowired
    private RestTemplate restTemplate;*/

    /*第四种方式feign*/
    @Autowired
    private ProductClient productClient;

    @ApiOperation("获取一个测试信息")
    @GetMapping("/msg")
    public String getProductMsg(){
        //第一种方式 直接使用RestTemplate,URL固定
        //RestTemplate restTemplate = new RestTemplate();
        //String response = restTemplate.getForObject("http://localhost:8080/productserver/msg",String.class);

        //第二种方式 直接使用RestTemplate,URL利用LoadBalancerClient获取
        /*RestTemplate restTemplate = new RestTemplate();
        ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT");
        String url = String.format("http://%s:%s/msg",serviceInstance.getHost(),serviceInstance.getPort());
        String response = restTemplate.getForObject(url,String.class);*/

        //第三种,将 RestTemplate 作为一个Bean 配制到项目,使用注解@LoadBalanced,在restTemplate直接使用应用名字
        //String response = restTemplate.getForObject("http://PRODUCT/msg",String.class);
        //4.feign
        String response = productClient.productMsg();
        log.info("response={}",response);
        return response;
    }
    }
    第三种方式使用注解@LoadBalanced的配制文件
    @Component
    public class RestTemplateConfig {
     @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    or
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        //return new RestTemplate();
        return new RestTemplateBuilder().basicAuthentication("oauth", "123").build();
    }
    }

HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add(“grant_type”, “password”);
map.add(“client_id”, “oauth”);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, header);
MyToken myToken = restTemplate.postForObject(“http://192.168.88.108:30002/oauth/token”,request,MyToken.class);

    // build http headers
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization","bearer "+myToken.getAccess_token());
    ResponseEntity<ClientUser[]> responseEntity = restTemplate.exchange(RBAC_SERVER+POST_ALL_USER, HttpMethod.GET,new HttpEntity<String>(headers),ClientUser[].class);

Ribbon 客户端负载均衡

更改负载均衡规则

PRODUCT:
  ribbon:
    NFLoadBalancerRuleClassName: com.loadbalancer.RandomRule

PRODUCT为应用名,此处改为随机方式,注意写完整的Class路径,不改默认为轮询,一般也够用了,其它参考官方说明文档

Hystrix熔断

具有服务降级(HystrixCommand注解指定/fallbackMethod回退函数实现降级逻辑),服务熔断,依赖隔离,监控的作用,防雪崩的利器

        <!--添加Hystrix依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
           <!-- <version>2.0.2.RELEASE</version>-->
        </dependency>
        <!--添加Hystrix dashboard依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            <!-- <version>2.0.2.RELEASE</version>-->
        </dependency>
        <!--如果有了不用再引入-->
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>-->
在主函数上添加注解
@EnableCircuitBreaker //启动Hystrix
@EnableHystrixDashboard

@HystrixCommand
如果我们使用的是@HystrixCommand注解,那么可以在注解中直接指定超时时间,如下:

@HystrixCommand(fallbackMethod="fallback",
    commandProperties = {
         @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000" )
    }
)

当然也可以指定commandKey,然后在配置文件中配置超时时间,如下:

@HystrixCommand(fallbackMethod="fallback",commandKey="userGetKey")
配置文件给commandKey配置超时时间:
hystrix.command.userGetKey.execution.isolation.thread.timeoutInMilliseconds = 13000

全局配置
如果只是想全局的配置,可以配置默认的超时时间:

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000

接口级别配置
假如我们的Feign Client定义如下:

@FeignClient(value = "user-service", fallbackFactory = UserRemoteClientFallbackFactory.class)
public interface UserRemoteClient {

    @GetMapping("/user/get")
    public ResponseData<UserDto> getUser(@RequestParam("id") Long id);

}

那么配置如下:

hystrix.command.UserRemoteClient#getUser(Long).execution.isolation.thread.timeoutInMilliseconds = 300

为什么要配置成上面的方式呢?

其实就是对commandKey进行配置,只要我们知道commandKey的生成规则就可以对接口级别进行配置,接口级别的规则是 Client名称#方法名(参数类型)

源码在feign.hystrix.SetterFactory.Default中:

String commandKey = Feign.configKey(target.type(), method);
服务级别配置
1.在Zuul中针对服务级别的话,直接配置service-id,如下:

hystrix.command.service-id.execution.isolation.thread.timeoutInMilliseconds=3000
Zuul中之所以要配置service-id原因是commandKey就是用的service-id, 通过源码分析可以得到结论。

首先进入的是RibbonRoutingFilter中的run方法,然后我们看核心的forward方法:

ClientHttpResponse response = forward(commandContext);
在forward中有下面的代码:

RibbonCommand command = this.ribbonCommandFactory.create(context);
通过create可以定位到具体的实现,这边就看你用的什么Http客户端,默认有三种实现,默认定位到org.springframework.cloud.netflix.zuul.filters.route.apache.HttpClientRibbonCommandFactory.create(RibbonCommandContext)方法。
所以service-id就是commandKey。

2.在Feign中针对服务级别的话,需要对commandKey进行定制,可以用service-id, 也可以用Feign Client Name,如下:

@Bean
@Scope("prototype")
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "feign.hystrix.enabled")
public Feign.Builder feignHystrixBuilder() {
    return HystrixFeign.builder().setterFactory(new SetterFactory() {

        @Override
        public Setter create(Target<?> target, Method method) {
            String groupKey = target.name();
            String commandKey = Feign.configKey(target.type(), method);
            return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                        //.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
                        //.andCommandKey(HystrixCommandKey.Factory.asKey(groupKey))
                        .andCommandKey(HystrixCommandKey.Factory.asKey(target.type().getSimpleName()));
            }
    });
}
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
默认的接口方式

.andCommandKey(HystrixCommandKey.Factory.asKey(groupKey))
service-id方式

.andCommandKey(HystrixCommandKey.Factory.asKey(target.type().getSimpleName()));
Feign Client Name方式

配置的话根据不同的配置填写不通的commandKey就可以了:

hystrix.command.Feign Client Name.execution.isolation.thread.timeoutInMilliseconds=3000

swagger2

1、pom.xml 添加 Maven 依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
​
 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger2</artifactId>
       <version>2.9.2</version>
    </dependency>
</dependencies>

2、创建 Swagger2Configuration.java

@Configuration
@EnableSwagger2
public class Swagger2Configuration {
    //api接口包扫描路径
    public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.hzdtech.order.server";
    public static final String VERSION = "1.0.0";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                .paths(PathSelectors.any())// 可以根据url路径设置哪些请求加入文档,忽略哪些请求
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("订单微服务") //设置文档的标题
                .description("单词计数服务 API 接口文档") // 设置文档的描述
                .version(VERSION) // 设置文档的版本信息-> 1.0.0 Version information
                .termsOfServiceUrl("http://shanpai.video") // 设置文档的License信息->1.3 License information
                .build();

    }
}

3、API 接口编写

@RestController
@Api(tags="接口所在的类")
@RequestMapping ("/my")
public class MyController {

    @RequestMapping(value="/list", method=RequestMethod.POST)
    @ApiOperation(value = "接口名", notes = "接口描述", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "length",value = "参数1", required = true, paramType = "path"),
            @ApiImplicitParam(name = "size",value = "参数2", required = true, paramType = "query"),
            @ApiImplicitParam(name = "page",value = "参数3", required = true, paramType = "header"),
            @ApiImplicitParam(name = "total",value = "参数4", required = true, paramType = "form"),
            @ApiImplicitParam(name = "start",value = "参数5",dataType = "string", paramType = "body")
    })
    public String register(){
        return "has permission";
    }
}
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:描述一个请求参数,可以配置参数的中文含义,还可以给参数设置默认值
@ApiImplicitParams:描述由多个 @ApiImplicitParam 注解的参数组成的请求参数列表

4、启动 SpringBoot 应用

SpringBoot 启动成功后,访问 http://localhost:8080/swagger-ui.html

###5 5、在 Security 中的配置

Spring Boot 项目中如果集成了 Spring Security,在不做额外配置的情况下,Swagger2 文档会被拦截。解决方法是在 Security 的配置类中重写 configure 方法添加白名单即可:

@Override
public void configure ( WebSecurity web) throws Exception {
    web.ignoring()
      .antMatchers("/swagger-ui.html")
      .antMatchers("/v2/**")
      .antMatchers("/swagger-resources/**");
}