Eureka를 사용하여 서비스 거버넌스를 달성하십시오
기능 : 서비스 거버넌스 실현 (서비스 등록 및 발견)
소개 : Spring Cloud Eureka는 Spring Cloud Netflix 프로젝트의 서비스 거버넌스 모듈입니다. Spring Cloud Netflix 프로젝트는 Spring Cloud의 하위 프로젝트 중 하나입니다. 주요 내용은 Netflix의 일련의 오픈 소스 제품을 포장하는 것입니다. 스프링 부팅 애플리케이션에 자체 구성된 Netflix OSS 통합을 제공합니다. 간단한 주석으로 개발자는 응용 프로그램에서 일반적으로 사용되는 모듈을 빠르게 구성하고 거대한 분산 시스템을 구축 할 수 있습니다. 주요 모듈에는 서비스 검색 (Eureka), 회로 차단기 (Hystrix), 지능형 라우팅 (ZUUL), 클라이언트로드 밸런싱 (리본) 등이 있습니다.
프로젝트 실무 실습 :
서비스 등록 센터 : Eureka-Server
기능 : 서비스 등록 센터는 서비스 등록 기능을 제공합니다
서비스 제공 업체 : Eureka-Client
기능 : 서비스 등록 센터에 서비스를 등록하십시오
서비스 등록 센터 : Eureka-Server
새 SpringBoot 프로젝트 작성 : Eureka-Server 및 Pom.xml 구성은 다음과 같습니다.
<properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.ropporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <fexence> <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka-server </artifactid> </dependency> </dependencies> <pectionalmanagement> <pecientencies> <pectionency> <groupid> org.springfram.cloud </groupid> <artifactid> Spring-cluctidencies <버전> dalston.sr1 </version> <type> pom </type> <copo> import </scope> </fectionency> </fectencies> </fectionencymanagement>
서비스 등록 센터의 기능을 구현하는 것은 매우 간단합니다. 프로젝트의 시작 클래스 EureKaserVerapplication에서 @enableEureKaserver 주석 만 사용하면됩니다.
@enableEureKaserver @springbootApplication public class eureKaserVerApplication {public static void main (string [] args) {new springApplicationBuilder (eureKaserVerApplication.class) .web (true) .run (args); }}기본적으로 Service Registration Center는 클라이언트로 등록하려고 시도하므로 클라이언트 등록 동작을 비활성화하고 다음 정보를 Application.Properties 구성 파일에 추가해야합니다.
spring.application.name = eureka-serverserver.port = 1001eureka.instance.hostname = localhosteureka.client.register-with-eureka = falseureka.client.fetch-registry = false
EureKaserVerApplication을 시작하고 http : // localhost : 9001/를 방문하여 Eureka의 페이지를 참조하십시오. 빨간색 상자의 위치에서 현재 서비스 등록 센터에 등록 된 작업 서비스 인스턴스가 없음을 알 수 있습니다.
서비스 제공 업체 : Eureka-Client
각 인스턴스가 등록되면 등록 센터에 심장 박동을 보내려면 심장 박동이 필요합니다. 클라이언트가 서버에 등록하면 호스트 및 포트, URL, 홈페이지 등과 같은 메타 데이터를 제공합니다. Eureka Server는 각 클라이언트 인스턴스로부터 하트 비트 메시지를받습니다. 하트 비트가 시간이 지나면 인스턴스가 일반적으로 등록 된 서버에서 제거됩니다.
새 SpringBoot 프로젝트를 작성하십시오 : Eureka-Client 및 Pom.xml 구성은 다음과 같습니다.
<properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.ropporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <fexence> <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka </artifactid> </eppendency> <pectionency> <groupid> org.springframewort.boot </groupid> <artifactid> spring-boot-starter-web </eppence> </dependency> </dependency> <pectionencymanagement> <pectionency> <pectinement> <groupId> org.springframework.cloud </groupid> <artifactid> Spring-Cloud-Dependencies </artifactid> <bersion> dalston.sr1 </version> pom </type> <scope> import </scope> </spection> </felepence>
서비스 제공 업체를 구현하는 것도 매우 간단합니다. 프로젝트 스타트 업 클래스 EureKaclientApplication에서 @enableEureKaclient 주석을 사용하십시오.
@enableEureKaclient @SpringBootApplication public class eureKaclientApplication {public static void main (String [] args) {new springApplicationBuilder (eureKaClientApplication.class) .web (true) .run (args); }}Application.Properties에서 다음을 구성하십시오
spring.application.name = eureka-clientserver.port = 9002eureka.client.serviceurl.defaultzone = http : // localhost : 9001/eureka/
Spring.application.name 속성을 통해 호출 할 때 마이크로 서비스 이름을 지정하여 서비스에 액세스 할 수 있습니다.
eureka.client.serviceurl.defaultzone 속성은 서비스 등록 센터의 구성 내용에 해당하며 서비스 등록 센터의 위치를 지정합니다.
Server.port 속성을 사용하여 다른 포트를 설정하십시오.
EureKaclientApplication 클래스를 시작하십시오
새로 고침 http : // localhost : 9001/, 서비스 제공 업체가 서비스 등록 센터에 등록했음을 알 수 있습니다.
새로운 DiscoveryController를 만듭니다
DiscoveryClient.getServices ()를 사용하여 등록 된 서비스 이름을 얻고 @Value를 사용하여 구성 파일의 정보를 IP에 할당하십시오.
@RestControllerPublic Class DiscoveryController {@autowired private DiscoveryClient DiscoveryClient; @Value ( "$ {server.port}") 개인 문자열 IP; @GetMapping ( "/client") public String Client () {String Services = "Services :"+DiscoveryClient.getServices ()+"IP :"+IP; System.out.println (서비스); 반품 서비스; }}방문 : http : // localhost : 9002/client
마지막으로 @enableeurekaclient 및 @enablediscoveryclient의 두 주석을 설명하겠습니다.
우선, 두 주석 모두 서비스 발견의 기능을 실현할 수 있습니다. Spring Cloud (Eureka, Consul, Zookeeper 등)에는 많은 Discovery Service 구현이 있습니다.
@enableeurekaclient는 Spring-Cloud-Netflix를 기반으로합니다. 이 서비스는 Eureka를 등록 센터로 사용하며 사용 시나리오는 비교적 단일입니다.
@EnableDiscoveryClient는 스프링 클라우드-커먼즈를 기반으로합니다. 이 서비스는 다른 등록 센터를 채택합니다.
github : https://github.com/mingyuhub/springcloud
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.