Spring Cloud 가 Eureka 응용 을 구축 하 는 방법
Eureka 는 REST 기반 서 비 스 를 제공 하 며 클 러 스 터 에 서 는 주로 서비스 관리 에 사용 된다.Eureka 는 자바 언어 를 바탕 으로 하 는 클 라 이언 트 구성 요 소 를 제공 하고 클 라 이언 트 구성 요 소 는 부하 균형 기능 을 실현 하여 업무 구성 요소 의 클 라 이언 트 배치 에 조건 을 마련 했다.이 프레임 워 크 를 사용 하면 비 즈 니스 구성 요 소 를 Eureka 용기 에 등록 할 수 있 습 니 다.이 비 즈 니스 구성 요 소 는 클 러 스 터 배 치 를 할 수 있 습 니 다.Eureka 는 주로 이러한 서비스의 목록 을 유지 하고 상 태 를 자동 으로 검사 합 니 다.
프로그램 구성
유레카 서버 만 들 기
maven 의존
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
spring boot 시작 포트 를 application.yml 에서 변경 합 니 다.
server:
port: 8761Eureka 서비스 설명 열기@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EKServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EKServerApplication.class).run(args);
}
}
springboot 시작
[Thread-11] o.s.c.n.e.server.EurekaServerBootstrap: Initialized server context
[main] s.b.c.e.t.TomcatEmbeddedServletContainer: Tomcat started on port(s): 8761 (http)
[main] .s.c.n.e.s.EurekaAutoServiceRegistration: Updating port to 8761
[main] c.b.firstEkServer.EKServerApplication: Started EKServerApplication in 8.594 seconds (JVM running for 9.523)시작 하 는 동안 서버 에 연결 할 수 없 는 이상 이 생 길 수 있 습 니 다.이 는 Eureka 가 시작 할 때 자신 을 클 라 이언 트 로 생각 하고 서버 에 가서 등록 정 보 를 캡 처 하기 때 문 입 니 다.
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
다음 설정 을 추가 하면 시작 할 때 이 이상 이 발생 하지 않 습 니 다.
eureka:
client:
registerWithEureka: false
fetchRegistry: falseregister With Eureka 는 자신의 정 보 를 Eureka 서버 에 등록 할 지 여 부 를 설명 합 니 다.기본 값 은 true 입 니 다.fetch Registry 는 Eureka 서버 에서 등록 정 보 를 캡 처 할 지 여 부 를 설명 합 니 다.기본 값 은 true 입 니 다.
브 라 우 저 에 접근http://localhost:8761 Eureka 콘 솔 보기 그림 설명 입력
서비스 공급 자 만 들 기
의지 하 다
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>application.yml 에 포트,Eureka 인 스 턴 스 이름과 Eureka 서비스 주 소 를 설정 합 니 다.
server:
port: 8080
spring:
application:
name: ek-provider
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/REST 서비스 만 들 기
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(HttpServletRequest request) {
return "hello:" + request.getRequestURL();
}
}
Eureka 클 라 이언 트 주석 열기@EnableEurekaServer
@EnableEurekaClient
@SpringBootApplication
public class EkProviderApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EkProviderApplication.class).run(args);
}
}
시작 하면 Eureka 콘 솔 에서 서비스 제공 자가 Eureka 에 등록 되 어 있 는 것 을 볼 수 있 습 니 다.
서비스 호출 자 생 성
의지 하 다
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
application.yml 에 포트,Eureka 인 스 턴 스 이름과 Eureka 서비스 주 소 를 설정 합 니 다.
server:
port: 9000
spring:
application:
name: ek-invoke
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
REST 서비스 호출 서비스 제공 자의"/hello"를 작성 합 니 다.
@RestController
@Configuration
public class InvokeController {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@RequestMapping("/invoke")
public String invoke() {
RestTemplate restTemplate = getRestTemplate();
return restTemplate.getForObject("http://ek-provider/hello", String.class);
}
}
전통 적 인 모드 에서 저 희 는 보통 Apache 의 Httpclient 로 REST 서 비 스 를 호출 합 니 다.여기 서 저 희 는 Spring 을 사용 하여 REST 서 비 스 를 호출 하 는 구성 요소 RestTemplate 를 제공 합 니 다.RestTemplate 자체 가 분포 식 서 비 스 를 호출 하 는 능력 을 갖 추고 있 지 는 않 지만 RestTemplate 의 bean 이@LoadBalanced 주석 에 의 해 수 정 된 후에 이 RestTemplate 인 스 턴 스 는 분포 식 서 비 스 를 방문 하 는 능력 을 가지 게 되 었 습 니 다.이것 은 Spring 이 제공 하 는 각종 차단기 덕분 입 니 다.Eureka 클 라 이언 트 주석 열기@EnableEurekaServer
@EnableEurekaClient
@SpringBootApplication
public class EkInvokeApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EkInvokeApplication.class).run(args);
}
}
시작 하면 Eureka 콘 솔 에서 서비스 호출 자가 Eureka 에 등록 되 어 있 는 것 을 볼 수 있 습 니 다.이후 브 라 우 저 액세스 서비스 호출 자의"invoke"인 터 페 이 스 는 다음 과 같 습 니 다.
총결산
Eureka 서버 는 심장 박동 링크 를 통 해 최신 등록 정 보 를 유지 하 는데 이 등록 정 보 는 모두 메모리 에 저 장 됩 니 다.
Eureka 서비스 제공 자 는 주로 다음 과 같이 진행 합 니 다.
Eureka 서버 에 서비스 등록
Eureka 서버 에 서비스 등록
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[MeU] Hashtag 기능 개발➡️ 기존 Tag 테이블에 존재하지 않는 해시태그라면 Tag , tagPostMapping 테이블에 모두 추가 ➡️ 기존에 존재하는 해시태그라면, tagPostMapping 테이블에만 추가 이후에 개발할 태그 기반 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.