1. 程式人生 > >spring cloud(二)-服務消費

spring cloud(二)-服務消費

前言

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客戶端的功能:

@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();
    }
}