spring feign demo

版本:

 spring boot 2.0.4

spring cloud Finchley.RELEASE

前提: 

    掌握eureka

工程结构:

   feign-parent

   ----eureka-server(服务注册中心)

   -----eureka-client (服务提供者)

  ------feign-client(服务消费者)

步骤如下:

 1.创建eureka-server

package com.sinotrans.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

配置文件:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

2.创建eureka-client

package com.sinotrans.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

创建一个简单的say hi 方法:

package com.sinotrans.eurekaclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HiController {
    @Value("${server.port}")
    String port;

    @GetMapping("/hi")
    public String Home(@RequestParam String name){
        return "hi "+name+",i am from port:"+port;
    }
}

3.创建feign-clietn

feign工程主要为了调用上面的say hi方法:

package com.sinotrans.eurekafeignclient.controller;


import com.sinotrans.eurekafeignclient.service.HiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * Created by JAMES.
 * 2018/6/26 9:33
 **/
@RestController
public class HiController {
    @Autowired
    HiService hiService;

    @GetMapping("/hi")
    public String sayHi(@RequestParam(defaultValue = "cralor",required = false)String name){
        return hiService.sayHi(name);
    }
}
package com.sinotrans.eurekafeignclient.feign;


import com.sinotrans.eurekafeignclient.config.FeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by cc.
 * 2018/8/26 9:11
 **/
@Component
@FeignClient(value = "eureka-client",configuration = FeignConfig.class)
public interface EurekaClientFeign {
    @GetMapping(value = "/test")
    String testFromClientEureka();

    @GetMapping(value = "/hi")
    String sayHiFromClientEureka(@RequestParam(value = "name")String name);
}

完整的工程代码下载:

https://download.csdn.net/download/fz2543122681/10625671

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这里有一个简单的Spring Cloud Feign集成Protobuf的示例: 1. 首先,您需要在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>2.2.5.RELEASE</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty-shaded</artifactId> <version>1.37.0</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> <version>1.37.0</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> <version>1.37.0</version> </dependency> ``` 2. 创建Protobuf定义文件 在src/main/proto目录下创建一个名为`hello.proto`的文件,并添加以下内容: ```protobuf syntax = "proto3"; package io.github.xyz.spring.cloud.feign.protobuf.example.api; message HelloRequest { string name = 1; } message HelloResponse { string message = 1; } service HelloService { rpc hello(HelloRequest) returns (HelloResponse); } ``` 3. 生成Java代码 在pom.xml文件中添加以下插件,用于生成Java代码: ```xml <build> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.5.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.12.4:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.37.0:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 然后运行`mvn protobuf:compile`和`mvn protobuf:compile-custom`,将生成的Java代码放在`target/generated-sources/protobuf`目录下。 4. 创建Feign客户端 创建一个Feign客户端,用于调用HelloService服务。在使用Protobuf时,需要使用`@RequestLine`注解,以指定使用哪个HTTP方法,并使用`@Headers`注解,以指定使用哪种消息类型。 ```java @FeignClient(name = "hello-service", configuration = HelloClientConfiguration.class) public interface HelloClient { @RequestLine("POST /hello") @Headers({"Content-Type: application/x-protobuf", "Accept: application/x-protobuf"}) HelloResponse hello(HelloRequest request); } ``` 5. 创建Feign配置 创建一个Feign配置类,用于配置Feign客户端。在使用Protobuf时,需要将`Encoder`和`Decoder`设置为`ProtobufEncoder`和`ProtobufDecoder`,并将`Content-Type`和`Accept`设置为`application/x-protobuf`。 ```java @Configuration public class HelloClientConfiguration { @Bean public Encoder protobufEncoder() { return new ProtobufEncoder(); } @Bean public Decoder protobufDecoder() { return new ProtobufDecoder(); } @Bean public Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } @Bean public Request.Options options() { return new Request.Options(5000, 10000); } } ``` 6. 创建服务端 创建一个服务端,用于提供HelloService服务。在使用Protobuf时,需要使用`@RequestBody`和`@ResponseBody`注解,以指定使用哪种消息类型。 ```java @RestController public class HelloController { @PostMapping("/hello") public HelloResponse hello(@RequestBody HelloRequest request) { return HelloResponse.newBuilder() .setMessage("Hello, " + request.getName() + "!") .build(); } } ``` 7. 配置Swagger 使用Swagger来测试Feign客户端。在Spring Boot应用中,可以使用Springfox Swagger来配置Swagger。 ```java @Configuration @EnableSwagger2 public class SwaggerConfiguration { @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } } ``` 8. 启动应用 现在,您可以启动应用并访问http://localhost:8080/swagger-ui.html来测试Feign客户端了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值