springcloud-feign
2020-12-13 05:02
标签:system width 专用 contex deb depend 依赖 ota value 1 feign的本质是还是使用HTTP协议调用 ==================>feign调用思路 ===================》注解含义 我们来写一个实例测试一下: 1 启动Eureka Server 1.1 配置文件application.properties 1.2 编写启动类 1.3 配置文件如下 2 编写服务提供者 2.1 编写提供者 2.2编写启动类 2.3 配置文件 application.properties 2.4编写启动类 2.5 xml文件 3 编写fegin客户端 3.1 启动类 3.2 配置类 3.3 Controller 3.4 fegin接口 3.5 配置文件application.properties 3.6 pom.xml依赖 4 测试 我们在浏览器输入http://localhost:8082/substitution/callHello1
springcloud-feign 标签:system width 专用 contex deb depend 依赖 ota value 原文地址:https://www.cnblogs.com/wangpipi/p/11128378.html
1 必须开启Eureka (调用者和被调用者都需要注册自身到注册中心中)
2 假如A是服务提供者B是接口调用者。 B调用A服务
3 我们不需要在A服务的启动类上加上 @EnableFeignClients 因为B是feigen的客户端
4 feign接口是写在B服务上的,然后@FeignClient(value = "FSH-HOUSE" , path = "/house")。中value的值就是A 服务注册在eureka中的服务名字
我们想要调用哪个服务,就写哪个服务的名字
5 无论A服务还是B服务,都必须注册在eurka中
6 B的启动类加上@EnableFeignClients 来声明B服务是一个feigen客户端的程序
7 fegin的调用,应该对被调用者完全透明,B调用A 那么B需要开启fegin,但A仍然只是注册到eurka中的A
而且A的服务启动类应该不需要加上@EnableFeignClients,也就是说A并不知道B是如何调用的,restTemplate或者feign,A只需要注册到Eureka中就行了
@EnableEureka 开启Eureka Server 作为注册中心的服务,需要在启动类上加上这个注解
@EnableDiscoveryClient 表示这个服务是一个Eureka客户端,可以提供服务
@EnableEurekaClient 和@EnableDiscoveryClient作用一样,只不过是eureka专用注解
@EnableFeignClients 标注此服务为feigen的客户端spring.application.name=fangjia-eureka
server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=true
#安全
security.basic.enabled=true
security.user.name=wyp
security.user.password=123456
package wyp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurkaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurkaServerApplication.class,args);
}
}
package wyp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/house")
public class HelloEureka {
@GetMapping("/hello")
public String hello(){
System.out.println("this is 8081");
return "this is 8080 : hello";
}
}
package wyp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
//@EnableFeignClients
public class FshHouseServicApplication {
public static void main(String[] args) {
SpringApplication.run(FshHouseServicApplication.class,args);
}
}
spring.application.name=fsh-house
server.port=8081
eureka.client.serviceUrl.defaultZone=http://wyp:123456@localhost:8761/eureka/
package wyp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
//@EnableFeignClients
public class FshHouseServicApplication {
public static void main(String[] args) {
SpringApplication.run(FshHouseServicApplication.class,args);
}
}
package wyp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FshSubstitutionServiceApplication {
public static void main(String[] args) {
SpringApplication.run(FshSubstitutionServiceApplication.class,args);
}
}
package wyp.configuration;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class BeanConfiguration {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
package wyp.configuration;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfiguration {
@Bean
Logger.Level feginLoggerLevl(){
return Logger.Level.FULL;
}
}
package wyp.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import wyp.feginInterface.HouseRemoteClient;
@RestController
@RequestMapping("substitution")
public class Xcontroller {
@Autowired
private RestTemplate restTemplate;
@Autowired
private HouseRemoteClient houseRemoteClient;
@GetMapping("/callHello")
public String getHello(){
for (int i=0 ;i){
String forObject = restTemplate.getForObject("http://FSH-HOUSE/house/hello", String.class);
}
return "dada";
}
/**
* 通过fegin方式
* @return
*/
@GetMapping("/callHello1")
public String getHello1(){
String hello = houseRemoteClient.hello();
System.out.println(hello);
return hello;
}
}
package wyp.feginInterface;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import wyp.configuration.FeignConfiguration;
@FeignClient(value = "FSH-HOUSE" , path = "/house",configuration = FeignConfiguration.class)
public interface HouseRemoteClient {
@GetMapping("hello")
String hello();
}
spring.application.name=fsh-substitution
server.port=8082
eureka.client.serviceUrl.defaultZone=http://wyp:123456@localhost:8761/eureka/
#fegin日志配置
logging.level.wyp.feginInterface.HouseRemoteClient=DEBUG