当前位置: 首页 > 知识库问答 >
问题:

无法让假装客户端为基本示例工作

白飞飙
2023-03-14

在Github和在线上,我看到了多个版本的Feign客户端Spring-Cloud、OpenFeign、Netflix.Feign都有不同的版本。谁能描述一下在生产中应该使用的最好的和稳定的假客户是什么?

package com.paa.controllers;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient (name="test-service",url="https://www.reddit.com/r")
public interface GetFeignClient {

     @RequestMapping(method = RequestMethod.GET, value = "/java.json")
     public String posts();
}

Controller:

@RestController
@RequestMapping("/some/api")
public class TestWLCController {

  @Autowired
  private GetFeignClient getFeignClient;

  .. some stuff


    @RequestMapping(value="/postSomething",method = RequestMethod.POST)
    @ApiOperation(value = "Configures something",
            notes = "basic rest controller for testing feign")

    public ResponseEntity<SomeResponse> feignPost(
            UriComponentsBuilder builder,
            @ApiParam(name = "myRequest", 
            value = "request for configuring something", 
            required = true)
            @Valid @RequestBody SomeRequest someRequest) {

        String resp = null;
        try {
            resp = getFeignClient.posts();
        } catch (Exception er) {
            er.printStackTrace();
        }

    }
}

应用:

我尝试了所有可能的注释排列,认为它可以解决自动操作的东西,但还是失败了

@Configuration
@ComponentScan
@EnableAutoConfiguration
//@EnableEurekaClient
@EnableFeignClients

//@SpringBootApplication
//@EnableFeignClients
//@EnableFeignClients(basePackages = {"com.paa.xenia.controllers", "com.paa.xenia.services"})
public class ServiceApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(XeniaServiceApplication.class);
    }

    public static void main(String[] args) {

        SpringApplication.run(ServiceApplication.class, args);
    }
}

共有1个答案

孙书
2023-03-14

我不确定你是否最终自己发现了这一点,但为了其他人的利益,下面是一个你正在尝试做的工作示例。我将首先指出一些不正确的,或者至少,不希望在您的代码中出现的东西,然后展示我的代码。

>

  • 您应该尽量不要使用url属性。相反,使用bootstrap.yml(或bootstrap.properties)中的 .ribbon.listofservers 设置服务器列表。这允许客户端负载平衡,因为ListofServers可以是逗号分隔的列表。
  • 对于HTTPS连接使用功能区,您需要指定HTTP连接不需要的两个东西。端口,它是ListofServers .ribbon.isSecure:true 的一部分。如果没有端口,则连接到端口80,如果没有issecure,则使用HTTP。
  • 使用curl进行测试时,我发现Reddit需要很长时间来响应。有关如何分解请求-响应周期所用的总时间的详细信息,请参阅此SO帖子。

    $ curl -v -H "User-Agent: Mozilla/5.0" -w "@curl-format.txt" -o /dev/null -s "html" target="_blank">https://www.reddit.com/r/java/top.json?count=1"
    { [2759 bytes data]
    * Connection #0 to host www.reddit.com left intact
    time_namelookup:     0.527
    time_connect:        0.577
    time_appconnect:     0.758
    time_pretransfer:    0.758
    time_redirect:       0.000
    time_starttransfer: 11.189
                      ----------
    time_total:         11.218
    

    根据Netflix Wiki,默认的读取和连接超时是3000毫秒,所以除非更改这些,否则您将一直超时。

    “说话便宜,把密码给我看看。”(Torvalds,Linus(2000-08-25))。

    我使用优秀的Spring Initializr站点生成了一个Gradle应用程序。下面是build.gradle文件中的一个片段。

    dependencies {
        compile('org.springframework.cloud:spring-cloud-starter-feign')
        compile('org.springframework.boot:spring-boot-starter-web')
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
    
    dependencyManagement {
        imports {
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR3"
        }
    }
    

    假装客户:

    @FeignClient(name = "reddit")
    public interface RedditClient {
        @RequestMapping(method = GET, value = "/r/java/top.json?count=1",
                headers = {USER_AGENT + "=Mozilla/5.0", ACCEPT + "=" + APPLICATION_JSON_VALUE})
        public String posts();
    }
    
    @SpringBootApplication
    @EnableFeignClients
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @RestController
        static class DemoController {
            @Autowired
            private RedditClient redditClient;
    
            @GetMapping("/posts")
            public String posts() {
                return redditClient.posts();
            }
        }
    }
    
    reddit:
      ribbon:
        listOfServers: www.reddit.com:443
        ConnectTimeout: 20000
        ReadTimeout: 20000
        IsSecure: true
    hystrix.command.default.execution:
      timeout.enabled: true
      isolation.thread.timeoutInMilliseconds: 50000
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class DemoApplicationTest {
        @Autowired
        private TestRestTemplate restTemplate;
    
        @Test
        public void testGetPosts() {
            ResponseEntity<String> responseEntity = restTemplate.getForEntity("/posts", String.class);
    
            HttpStatus statusCode = responseEntity.getStatusCode();
            assertThat(String.format("Actual status code: %d, reason: %s.",
                    statusCode.value(), statusCode.getReasonPhrase()),
                    statusCode.is2xxSuccessful(), equalTo(true));
        }
    }
    

  •  类似资料:
    • 在我的项目中,diffrent服务被部署为微服务,授权和身份验证在一个公共jar文件中处理,该文件作为依赖项添加到每个微服务项目中。 微服务之间的通信通过虚拟客户端完成 下面给出了这种服务的分级文件 在一个场景中,我强制使用OAuth库中的feign client来调用我的授权微服务,下面给出了jar的依赖文件 但是,当我用我的服务部署新的jar文件时,在我的jar文件中实现的假客户机不起作用,调

    • 我对假装很陌生。今天就发现吧……当我读到Spring Cloud Feign时,我的第一个问题是:“您如何包装您的Fiign客户机?” 我举个例子。假设我们有2个微服务M1和M2。M2使用来自M1的endpoint。 null 也许我完全错了,请指正。 多谢!拜拜

    • 问题内容: 我试图在JAVA中找到一个简单的(ha)带有工作服务的SOAP示例,但我似乎找不到的任何例子。 我已经试过这一个,从这个例子,但它只是不工作,它要求我把一个斜杠的,但它在那里并没有什么发生。 那么,有谁知道任何SOAP示例链接,我可以下载/请求并使用它吗? 谢谢你的帮助。 问题答案: 要用Java实现简单的SOAP客户端,可以使用SAAJ框架(JSE 1.6及更高版本附带): 带有Ja

    • 我试图在JAVA中找到一个简单的(ha)SOAP示例,其中包含一个工作服务,但我发现的任何示例都不工作。 我已经试过这个例子中的这个,但是它不起作用,它要求我输入一个正斜杠,但是它已经输入了,没有任何反应。 有人知道我可以下载/请求和修改的SOAP示例链接吗? 谢谢你的帮助。

    • 使用Spring云合同验证生产者和消费者之间的合同。在我的消费者控制器中,我正在使用Feign client调用另一个微服务方法来获取一些数据。但是现在在SpringCloud contract中,为这个微服务进行存根调用是不可能的。 使用Spring Cloud与Netflix OSS。

    • Erlang RabbitMQ客户端无法工作...(http://www.RabbitMQ.com/erlang-client-user-guide.html) 以下是我采取的步骤。 已创建模块amqp_example.erl 已创建deps文件夹 将rabbit-common和amqp_client放在deps文件夹中 使用erl_libs=deps erlc-o ebin amqp_examp