pom
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
1.8
Greenwich.SR2
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
application.yml
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
client:
#是否将自己注册到eureka
register-with-eureka: false
#是否从eureka获取注册信息
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址
defaultZone: http://127.0.0.1:${server.port}/eureka
启动项
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//千万别导错包
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
pom
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
1.8
Greenwich.SR2
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
application.yml
server:
port: 10100
spring:
application:
name: base
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka
启动项
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class BaseApplication {
public static void main(String[] arg){
SpringApplication.run(BaseApplication.class,arg);
}
}
模拟服务
@RestController
public class IndexController {
@RequestMapping("/hello")
public String index(@RequestParam String name){
String msg = "Hello " + name + ", this time is " + System.currentTimeMillis();
return msg;
}
}
pom
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
1.8
Greenwich.SR2
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
application.yml
server:
port: 10101
spring:
application:
name: customer
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka
feign:
hystrix:
enabled: true
启动项
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class CustomerApplication {
public static void main(String[] arg){
SpringApplication.run(CustomerApplication.class,arg);
}
}
feign 接口
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "base")
public interface Index {
@RequestMapping(value = "/hello")
public String hello(@RequestParam("name") String name);
}
value : 要调用的spring.application.name
@requestMapping 要调用的服务的地址,必须为完整路径,即使是GetMapping ,也必 须写成RequestMapping (Method=Get) 格式
调用
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Date: 2019/7/29
* Author: POPPET
* Description:
*/
@RestController
public class IndexController {
@Autowired
private Index index;
@RequestMapping("hello/{name}")
public String index(@PathVariable("name") String name){
return index.hello(name);
}
}
至此 服务调用结束
同时,eureka-server 注册中心,两个服务都注册了过去
如有疑问,欢迎指正。