SpringBoot+Spring Cloud Consul 서비스 등록 및 발견 상세 분석
 
 뭐 공부 해요?
Consul 은 HashiCorp 사가 내 놓 은 오픈 소스 도구 로 분포 식 시스템 의 서비스 발견 과 설정 을 실현 하 는 데 사용 된다.다른 분포 식 서비스 등록 과 발견 방안 과 함께 Consul 의 방안 은 더욱'원 스 톱'으로 서비스 등록 과 발견 프레임 워 크,분포 일치 성 협의 실현,건강 검사,Key/Value 저장,다 중 데이터 센터 방안 을 내 장 했 고 다른 도구(예 를 들 어 ZooKeeper 등)에 의존 하지 않 아 도 된다.사용 하기 도 간단 하 다.Consul 은 Go 언어 로 작성 되 었 기 때문에 천연 이식 성(Linux,windows,Mac OS X 지원)이 있 습 니 다.설치 패 키 지 는 실행 가능 한 파일 하나만 포함 되 어 있어 배치 가 편리 하고 Docker 등 경량급 용기 와 빈 틈 없 이 협조 할 수 있 습 니 다.
설치 컨설팅
홈 페이지(consul.io)최신 버 전 1.8.0 은 MacOS,Windows,Linux 를 제공 합 니 다.어떻게 설치 해 야 할 지 모 르 면 공식 적 으로 동 영상 도 제공 합 니 다.
 
 저 는 docker 를 사용 하여 설치 하고 설치 과정 을 요약 하면 세 마디 입 니 다.
docker search consul
docker pull consul
docker run --name consul -d -p 8600:8500 consul
문제 가 없 으 면 본 컴퓨터 방문http://localhost:8600은 consul 자체 관리 시스템 을 열 수 있 습 니 다.기본 적 인 상황 에서 서비스 등록 이 없습니다.
 
  
 부모 프로젝트 pom.xml
<dependencyManagement>
 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-dependencies</artifactId>
 <version>2.2.2.RELEASE</version>
 <type>pom</type>
 <scope>import</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-dependencies</artifactId>
 <version>Hoxton.SR1</version>
 <type>pom</type>
 <scope>import</scope>
 </dependency>
 </dependencies>
 </dependencyManagement>서비스 등록 센터 가 있 습 니 다.그러면 저 희 는 두 개의 서비스 제공 자 를 개발 하 겠 습 니 다.여 기 는 두 개의 Module,포트 8001 과 8002 를 새로 만 들 었 습 니 다.두 모듈 코드 가 같 습 니 다.주로 부하 사용 을 보 여주 기 위해 서 입 니 다.
새 모듈,spring-cloud-starter-consul-disconvery 의존 추가
 
 pom.xml
 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-consul-discovery</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 </dependency>
 </dependencies>
server:
 port: 8001
spring:
 application:
 name: consul-student-service
 cloud:
 consul:
 port: 8600
 host: 127.0.0.1
 discovery:
 service-name: ${spring.application.name}여기 서 나 는 직접 테스트 인 터 페 이 스 를 써 서 시동 류 에 넣 었 다.여기 서 나 는 포트 8001 의 코드 만 붙 였 는데 8002 코드 의 구조 가 같 지만 포트 가 다르다.
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulStudentService8001 {
 public static void main(String[] args) {
 SpringApplication.run(ConsulStudentService8001.class,args);
 }
 @GetMapping("/student/version")
 public String version(){
 return "8001,202007222300";
 }
} 
  
  
 소비자 컨설팅
서비스 등록 센터 가 있 고 서비스 제공 자 도 있 습 니 다.우 리 는 서비스 소비 자 를 다시 개발 합 니 다.
새 모듈,spring-cloud-starter-consul-disconvery 의존 도 추가
pom.xml
<dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-consul-discovery</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 </dependency>
 </dependencies>
server:
 port: 8080
spring:
 application:
 name: consul-student-consumer
 cloud:
 consul:
 host: 127.0.0.1
 port: 8600
 discovery:
 service-name: ${spring.application.name}
 #       consul 
 register: falseRestTemplate 설정 클래스 를 개발 하여 REST 인 터 페 이 스 를 호출 할 때 사용 합 니 다.
@Configuration
public class ApplicationContextConfig {
 @Bean
 @LoadBalanced
 public RestTemplate restTemplate(){
 return new RestTemplate();
 }
}
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulStudentConsumer {
 public static void main(String[] args) {
 SpringApplication.run(ConsulStudentConsumer.class,args);
 }
 @Autowired
 RestTemplate restTemplate;
 @GetMapping("/consul/student/version")
 public String version(){
 //           REST  
 return restTemplate.getForObject("http://consul-student-service/student/version",String.class);
 }
}
스프링 부 트+스프링 클 라 우 드 컨 설 턴 트 서비스 등록 및 발견 에 대한 상세 한 설명 은 여기까지 입 니 다.스프링 부 트 스프링 클 라 우 드 컨 설 턴 트 서비스 등록 내용 은 저희 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Java・SpringBoot・Thymeleaf】 에러 메세지를 구현(SpringBoot 어플리케이션 실천편 3)로그인하여 사용자 목록을 표시하는 응용 프로그램을 만들고, Spring에서의 개발에 대해 공부하겠습니다 🌟 마지막 데이터 바인딩에 계속 바인딩 실패 시 오류 메시지를 구현합니다. 마지막 기사🌟 src/main/res...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.