使用Feign实现声明式REST调用

本节讲的是消费服务

编写Eureka Service

新建一个spring boot Maven项目,添加如下依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

启动类添加@EnableEurekaServer注解,声明是一个Eureka Server。

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

配置文件

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

将服务注册到Eureka Service上

user-provider

新建一个spring boot Maven项目命名user-provider,添加如下依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

启动类添加@EnableDiscoveryClient注解,也可以使用@EnableEurekaClient,声明是一个Eureka Client。

@EnableDiscoveryClient
@SpringBootApplication
public class ProviderUserApplication {

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

配置文件

server:
  port: 8080
spring:
  application:
    name: user-provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

spring.application.name 用于指定注册到Eureka Service上的应用名称。

创建一个 RestController 

@RestController
public class HelloController {
    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name) {
        return "I am user-provider say hello to you "+name;
    }

}
user-consumer

新建一个spring boot Maven项目命名user-consumer,添加如下依赖

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-feign</artifactId>
   <version>1.4.0.RELEASE</version>
</dependency>

启动类添加@EnableDiscoveryClient和@EnableFeignClients注解

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class UserConsumerApplication {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

配置文件

server:
  port: 8081
spring:
  application:
    name: user-consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

创建一个Feign接口,并添加@FeignClient注解

@FeignClient(name = "user-provider")
public interface UserFeignClient {
    @GetMapping(value = "/hello/{name}")
    public String hello(@PathVariable("name") String name);
}

创建一个 RestController 

@RestController
public class CallHelloController {

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping(value = "/hello/{name}")
    public String hello(@PathVariable("name") String name) {
        return userFeignClient.hello(name);
    }

}

   运行三个项目访问 http://localhost:8081/hello/lihaoyang 会得到 I am user-provider say hello to youlihaoyang




猜你喜欢

转载自blog.csdn.net/u011164906/article/details/79830683