spring cloud(二)-服务消费(Feign)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lh87270202/article/details/83990956

前言

eureka-server作为服务注册中心、eureka-client作为服务提供者

1、负载均衡客户端

1.1、服务消费-LoadBalancerClient

(1)创建一个spring boot消费工程eureka-customer,pom.xml中加入依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> 
    </parent>
 <dependencies>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
 </dependencies>
 <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>Finchley.RELEASE</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>

(2)application.properties配置
指定eureka注册中心的地址

spring.application.name=eureka-consumer
server.port=21011
eureka.client.serviceUrl.defaultZone=http://localhost:80791/eureka/

(3)启动类
初始化RestTemplate,用来发起rest请求,@EnableDiscoveryClient注解用来将当前应用加入到服务治理体系中。

@EnableDiscoveryClient
@SpringBootApplication
public class Application{
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	public static void main(String[] args) {
		SpringApplication.run(Application.class).web(true).run(args);
	}
}

(4)创建一个接口用来消费eureka-client提供的接口

@RestController
public class TestController{
	@Autowired
	LoadBalancerClient loadBalancerClient;
	@Autowired
	RestTemplate restTemplate;
	@GetMapping("/consumer")
	public String test() {
		ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client");
		String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/test";
		System.out.println(url);
		return restTemplate.getForObject(url, String.class);
	}
}
1.2、服务消费-Feign

(1)添加依赖

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

(2)启动类
@EnableFeignClients注解开启扫描Spring Cloud Feign客户端的功能:
注意:EnableFeignClients默认扫描的范围是启动类及启动以下的包,所以你的feignclient定义不在同级及以下,需要指定basePackages属性

//启动类和feign不在同包下,指定basePackages属性
@EnableFeignClients("com.test.clockbone")
@EnableDiscoveryClient
@SpringBootApplication
public class Application{
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	public static void main(String[] args) {
		SpringApplication.run(Application.class).web(true).run(args);
	}
}

(3)创建一个Feign的客户端接口定义
Feign的客户端接口,用来绑定服务提供的接口,使用@FeignClient注解来指定这个接口所要调用的服务名称

//eureka-client, eureka服务提供者的名称
@FeignClient("eureka-client")
public interface  TestClient {
    @GetMapping("/test")
    String consumer();
}

(4)controller调用

@RestController
public class TestController {
    @Autowired
    TestClient client;
    @GetMapping("/consumer")
    public String test() {
        return client.consumer();
    }
}

猜你喜欢

转载自blog.csdn.net/lh87270202/article/details/83990956