springcloud之eureka

springcloud之eureka

*Client-side service discovery* allows services to find and communicate with each other without hard-coding hostname and port.

  • client register in eureka server
  • send a heartbeat signal to the registry to inform the presence or liveness

Components: a service registry (Eureka Server), a REST service which registers itself at the registry (Eureka Client) and a web application, which is consuming the REST service as a registry-aware client (Spring Cloud Netflix Feign Client).

Eureka HA

  • server cluster

    Eureka can be deployed as a cluster of servers. In case, one of these Eureka servers crash, clients can still connect to the remaining Eureka servers and discover other services.

  • client cache

    Clients retrieve and cache registry information from Eureka server. In case all Eureka servers crash, clients still posses the last healthy snapshot of the registry. This is the default behavior of Eureka clients

Screen Shot 2020-12-29 at 7.35.07 PM

cluster

first server



server:
port: 1111
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2:1112/eureka/

Second server

server:
port: 1112
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1:1111/eureka/

When Eureka server instances will boot up they will look for each other. All microservices will register with them automatically, so if one goes down the other server instance will be always there. On both Eureka instances you will be able to see all the registered microservices. Like this you can scale-up and have multiple server instances in a production environment


code

Eureka server

  1. springboot 工程添加spring-cloud-starter-netflix-eureka-server
  2. @EnableEurekaServer
  3. 配置
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
server:
port: 8761
eureka:
client:
registerWithEureka: false //Not to register with ‘itself'
fetchRegistry: false


这样就在8761端口setup了一个eureka server

Eureka client

  1. 添加依赖spring-cloud-starter-netflix-eureka-client
  2. @EnableDiscoveryClient or @EnableEurekaClient
  3. 配置
spring:
application:
name: spring-cloud-eureka-client
server:
port: 0
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
instance:
preferIpAddress: true

FeignClient

discovery-aware Spring RestTemplate using interfaces to communicate with endpoints. This interfaces will be automatically implemented at runtime and instead of service-urls, it is using service-names.

Withou Feign

不使用feign的话,需要注入EurekaClient ,获取服务信息

@Autowired
private EurekaClient eurekaClient;

public void doRequest() {
Application application =
eurekaClient.getApplication("spring-cloud-eureka-client");
InstanceInfo instanceInfo = application.getInstances().get(0);
String hostname = instanceInfo.getHostName();
int port = instanceInfo.getPort();
// ...
}

With Feign

@FeignClient(“service-name”)

@FeignClient("spring-cloud-eureka-client")
public interface GreetingClient {
@RequestMapping("/greeting")
String greeting();
}

@SpringBootApplication
@EnableFeignClients
@Controller
public class FeignClientApplication {
@Autowired
private GreetingClient greetingClient;

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

@RequestMapping("/get-greeting")
public String greeting(Model model) {
model.addAttribute("greeting", greetingClient.greeting());
return "greeting-view";
}
}

https://www.baeldung.com/spring-cloud-netflix-eureka