<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                將feign脫離微服務,作為一個類似于RestTemplate和httpclient的http請求工具,但是不需要配置 ## 1. maven依賴 ~~~ <dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-core</artifactId> <version>8.18.0</version> </dependency> ~~~ ## 2. 自定義接口 client端口接口中寫出server的地址、請求參數 ~~~ import feign.Param; import feign.RequestLine; public interface RemoteService { @RequestLine("GET /users/list?name={name}") String getOwner(@Param(value = "name") String name); } ~~~ 通過`@RequestLine`指定HTTP協議及URL地址 ## 3. 配置類 ~~~ RemoteService service = Feign.builder() .options(new Options(1000, 3500)) .retryer(new Retryer.Default(5000, 5000, 3)) .target(RemoteService.class, "http://127.0.0.1:8085"); ~~~ 1. options方法指定連接超時時長及響應超時時長, 2. retryer方法指定重試策略 3. target方法綁定要調用接口類與服務端地址 4. 調用返回結果類型為綁定的接口類型,對象類型就按照鍵名配置 ## 調用 使用配置類(包含接口信息) ~~~ String result = service.getOwner("scott"); ~~~ 與調用本地方法相同的方式調用feign包裝的接口,直接獲取遠程服務提供的返回值。 #### 附:服務生產者 ~~~ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value="users") public class UserController { @RequestMapping(value="/list",method={RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT}) @ResponseBody public String list(@RequestParam String name) throws InterruptedException{ return name.toUpperCase(); } } ~~~ ## 更進一步 在項目中,服務消費端與生產端之間交換的數據往往是一或多個對象,feign同樣提供基于json的對象轉換工具,方便我們直接以對象形式交互。 ### 業務接口 ~~~kotlin public interface RemoteService { @Headers({"Content-Type: application/json","Accept: application/json"}) @RequestLine("POST /users/list") User getOwner(User user); } ~~~ 加入@Headers注解,指定Content-Type為json ### 配置 ~~~cpp RemoteService service = Feign.builder() .encoder(new JacksonEncoder()) .decoder(new JacksonDecoder()) .options(new Options(1000, 3500)) .retryer(new Retryer.Default(5000, 5000, 3)) .target(RemoteService.class, "http://127.0.0.1:8085"); ~~~ encoder指定對象編碼方式,decoder指定對象解碼方式。這里用的是基于Jackson的編、解碼方式,需要在pom.xml中添加Jackson的依賴 ~~~xml <dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-jackson</artifactId> <version>8.18.0</version> </dependency> ~~~ ### 調用 ~~~undefined User result = service.getOwner(u); ~~~ #### 附:服務生產者 ~~~css @Controller @RequestMapping(value="users") public class UserController { @RequestMapping(value="/list",method={RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT}) @ResponseBody public User list(@RequestBody User user) throws InterruptedException{ System.out.println(user.getUsername()); user.setId(100L); user.setUsername(user.getUsername().toUpperCase()); return user; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看