springcloud的远程调用-feign

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

一:cloud的远程调用

 1.restemplate的远程调用

  我们下来看一个我们之前的案例:

	@Autowired
	private RestTemplate restTemplate;

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

	@RequestMapping("/hi")
	public String callHome(){
		System.err.println("啊哈,我要去调用8989的miya接口啦");
		return restTemplate.getForObject("http://localhost:8989/miya", String.class);
	}

  这里是去调用了远程的8989的方法,使用了restemplate注入为bean,然后调用,这种调用局限性太大,在代码里暴露接口的数

剧,并且一旦远程接口ip/端口改变就无法调用。

 2.基于feign的远程调用

   a.添加feign、eureka支持

   b.yml添加feign,打开hystrix断路器

  c.通过server-name调用

server:
  port: 8989

spring:
  application:
    name: service-c3

#feign的配置,连接超时及读取超时配置
feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: basic
        
eureka:
  instance:
    hostname: localhost # 服务消费者应用所在的主机(本地测试的话,就使用localhost即可;当然你也可以在host文件中配置一个虚拟域名;但必须要在本地浏览器中可以访问才行)
  client:
    service-url:
      defaultZone: http://admin:admin@peer2:1002/eureka,http://admin:admin@peer3:1003/eureka,http://admin:admin@peer1:1001/eureka
@SpringBootApplication
@RestController
@EnableFeignClients
@EnableDiscoveryClient
public class client03 {

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

	@Autowired
   private feginService  feginService;

	@RequestMapping("/hi")
	public String home(){
		System.err.println("哦哈哈哈哈,我是8989的端口哦");
		return "port=8989";
	}

	@RequestMapping("/miya")
	public String info(){
		System.err.println("啊哈,我要去调用8988的info端口了");
     return feginService.getHi();
	}

	
}
/**
 * Project Name:zipkin-client3
 * File Name:service.java
 * Package Name:com.forezp.service
 * Date:2019年6月19日下午9:19:46
 * Copyright (c) 2019, [email protected] All Rights Reserved.
 *
*/

package com.forezp.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.forezp.service.impl.serviceimpl;

/**
 * ClassName:service <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2019年6月19日 下午9:19:46 <br/>
 * @author   monxz
 * @version  
 * @since    JDK 1.8
 * @see 	 
 */
@FeignClient(value="service-c2" ,fallback=serviceimpl.class )
public interface feginService {

	@GetMapping("/hi")
  public String getHi();
}

/**
 * Project Name:zipkin-client3
 * File Name:serviceimpl.java
 * Package Name:com.forezp.service.impl
 * Date:2019年6月19日下午9:20:51
 * Copyright (c) 2019, [email protected] All Rights Reserved.
 *
*/

package com.forezp.service.impl;

import org.springframework.stereotype.Component;

import com.forezp.service.feginService;

/**
 * ClassName:serviceimpl <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2019年6月19日 下午9:20:51 <br/>
 * @author   monxz
 * @version  
 * @since    JDK 1.8
 * @see 	 
 */
@Component
public class serviceimpl implements  feginService{

	@Override
	public String getHi() {
		
		
		return "不好意思哈";
	}

}

猜你喜欢

转载自blog.csdn.net/qq_35755863/article/details/92842069