使用RestTemplate和Feign实现声明式REST调用+Ribbon负载均衡

本节讲的是消费服务

 

编写Eureka Service

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


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server

启动类添加@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,添加如下依赖


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client

启动类添加@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-provider命名为user-provider2 端口号改为8081

服务消费者

user-consumer

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


   org.springframework.cloud
   spring-cloud-starter-netflix-eureka-client



   org.springframework.cloud
   spring-cloud-starter-feign
   1.4.0.RELEASE

 

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

 

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class ProductServiceApplication {

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

    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }
}
@LoadBalanced 它会将请求地址http://user-provider/。user-provider是用户微服务的主机名,自动映射成微服务的网络地址

配置文件

 

server:
  port: 8082
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 HelloController {

    @Autowired
    UserFeignClient userFeignClient;

    @Autowired
    RestTemplate restTemplate;

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

    @GetMapping(value = "/hello/rest/{name}")
    public String helloRest(@PathVariable("name") String name) {
        return restTemplate.getForEntity("http://user-provider/hello/lihaoyang", String.class).getBody();
    }

}

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

 

 

 

你可能感兴趣的:(spring,cloud)