PS: 本篇博客只讲具体使用,原理源码后续博客再讲。
博客源码地址在 这里。
如图,euraka-feign
是euraka
服务注册中心,eureka-feign-server
是feign服务端,eureka-feign-client
是feign客户端。
pom.xml
中添加依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
添加注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignApplication.class, args);
}
}
application.properties
中添加配置
user.application.shout-name=eureka
server.port=10000
eureka.instance.hostname=127.0.0.1
#是否把自己注册到Eureka
eureka.client.register-with-eureka=false
#是否从Eureka获取注册信息(单Eureka为false)
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
spring.application.name=cloud-${user.application.shout-name}
pom.xml
中添加依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
添加注解
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaFeignServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignServerApplication.class, args);
}
}
application.properties
中添加配置
spring.application.name=eureka-feign-server
server.port=10001
eureka.client.service-url.defaultZone=http://127.0.0.1:10000/eureka
eureka.instance.prefer-ip-address=true
pom.xml
中添加依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
添加注解
@EnableFeignClients(basePackages = "com.lxh.eurekafeign.common.api")
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaFeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignClientApplication.class, args);
}
}
application.properties
中添加配置
spring.application.name=eureka-feign-client
server.port=10002
eureka.client.service-url.defaultZone=http://127.0.0.1:10000/eureka
eureka.instance.prefer-ip-address=true
测试接口类
@Slf4j
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/test")
public String test() {
return testService.getName();
}
}