原文地址:https://www.jianshu.com/p/3892df1eea7e
Eureka是netflix的一个子模块,以实现云端中间层服务注册和服务发现。功能类似于dubbo的注册中心,比如zookeeper。
eureka.png
1.8
Greenwich.SR3
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
server:
port: 8761
eureka:
client:
registerWithEureka: false #不进行自我注册
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
@SpringBootApplication
@EnableEurekaServer
public class MallEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(MallEurekaApplication.class, args);
}
}
1.8
Greenwich.SR3
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
server:
port: 8080
spring:
application:
name: mall-provider
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: mall-provider
prefer-ip-address: true
@SpringBootApplication
@EnableDiscoveryClient
public class MallProviderApplication {
public static void main(String[] args) {
SpringApplication.run(MallProviderApplication.class, args);
}
}
@RestController
@RequestMapping("/demo")
public class ProviderController {
@RequestMapping("/getHello")
@ResponseBody
public String getHello() {
return "hello-provider-api!!!";
}
}
1.8
Greenwich.SR3
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.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
server:
port: 8081
spring:
application:
name: mall-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: mall-consumer
prefer-ip-address: true
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class MallConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(MallConsumerApplication.class, args);
}
}
@FeignClient(value = "MALL-PROVIDER") //引用服务提供者的服务名称
public interface ProviderApi {
@RequestMapping("/demo/getHello")
String getHello();
}
@Autowired
private ProviderApi providerApi;
// 通过fegin调用服务
@RequestMapping("/getHello3")
@ResponseBody
public String getHello3() {
return providerApi.getHello();
}