서비스 거버넌스는 마이크로 서비스 아키텍처에서 가장 핵심적이고 기본적인 모듈이라고 할 수 있습니다. 주로 다양한 마이크로 서비스 인스턴스의 자동화 된 등록 및 발견을 실현하는 데 사용됩니다.
Spring Cloud Eureka는 Spring Cloud Netflix MicroServices Suite의 일부이며, 주로 마이크로 서비스 아키텍처에서 서비스 거버넌스 기능을 완료하는 데 책임이 있습니다.
이 기사는 간단한 작은 예제를 사용하여 Eureka를 통해 서비스를 관리하는 방법을 공유합니다.
============== 나는 화려한 분할 선입니다 ==============================
1. 서비스 등록 센터를 구축하십시오
먼저 전체 디렉토리 구조를 나열하십시오.
건설 과정은 다음과 같습니다.
1. Maven 프로젝트 만들기 : Eureka (특정 구현 생략)
2. POM 파일을 수정하고 종속성을 소개합니다
<project xmlns = "http://maven.apache.org/pom/4.0.0"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xsi : schemalocation = "http://maven.apache.org/pom/0.0.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.sam </groupid> <artifactid> eureka </artifactid> 0.0.1-snapshot> <groupspr) <Artifactid> Spring-Boot-Starter-Parent </artifactid> <5.1.release </version> </parent> <properties> <javaversion> 1.8 </javaversion> </properties> <!-버전 관리에 종속성 관리를 사용하십시오-> <pecientmanagement> <pependency> <GroupSmud> ORG.SPROMID. <Artifactid> Spring-Cloud-Dependencies </artifactid> version> camden.sr6 </version> <type> pom </type> <type> import </scope> </dependency> </dependencymanagement> <pectionalsies> <! <spection </incelocedency> <group <groupd> org.sprameworg.scrongreg. <Artifactid> Spring-Cloud-Starter-Eureka-Server </artifactid> </fectionency> </fectomency> </fectencies> </project>
3. 시작 클래스를 만듭니다
/** * * @enableEureKaserver * 프로젝트가 Eureka */@enableEureKaserver@springBootApplicationPublic 클래스 Eurekaapp {public static void main (String [] args)을 지정하는 데 사용되는 서비스 등록 센터 {springApplication.run (eurekaapp.class); }} 4. application.properties 파일을 구성합니다
#Tomcat 서비스 포트 번호 서버. 포트 = 1111#서비스 이름을 설정합니다. Spring.name = eureka-serviceeureka.instance.hostname = localhost#Register Center는 eureka.client.register-With-Eureka = False#레지스터 센터를 찾을 필요가 없습니다. Leureka.client.serviceurl.defaultzone = http : // $ {eureka.instance.hostname} : $ {server.port}/eureka 5. 서비스를 시작하고 액세스하면 다음과 같은 그림이 보입니다.
2. 서비스 제공 업체를 등록하십시오
먼저 전체 디렉토리 구조를 나열하십시오.
건설 과정은 다음과 같습니다.
1. Maven 프로젝트 만들기 : Hello-Service (특정 구현 생략)
2. POM 파일을 수정하고 종속성을 소개합니다
<project xmlns = "http://maven.apache.org/pom/4.0.0"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xsi : schemalocation = "http://maven.apache.org/pom/0.0.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.sam </groupid> <artifactid> hello-service </artifactid> <sult> 0.0.1-snapshot> <부모> <groupid> org.springframework.boot </groupid> <artifactid> 스프링-부트 스타터-팔렌트 </artifactid> <bersion> 1.5.1. release </version> </parent> <properties> <javaversion> 1.8 </javaversion> </properties> <peciplencymanagement> <pelovemencies> <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-dependencies </artifactid> <bersion> camden.sr6 </version> <type> pom </type> <scope> 가져 오기 </scope> </dependency> </speenceManagement> <! <!-eureka clienta eureka clienta <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-eureka </artifactid> </fectionement> </fectionency> </fectionencies> </project>
3. 시작 클래스를 만듭니다
/***** @EnableDiscoveryClient* 서비스가 Eureka Server를 사용하여 서비스 등록 및 검색을 구현하기 위해*/@enablediscoveryclient@springbootApplicationPublic Class HelloApp {public static void main (String [] args) {SpringApplication.run (helloApp.class, args); }}4. 컨트롤러를 만듭니다
@RestControllerPublic 클래스 HelloController {Logger Logger = LoggerFactory.getLogger (HelloController.class); @autowired DiscoveryClient DiscoveryClient; @requestmapping ( "/hello") public String hello () {serviceInstance instance = discoveryclient.getLocalServiceInstance (); // 인쇄 서비스 ID logger.info ( "*********" + instance.getServiceId ()); "안녕하세요, 이것은 hello-service입니다"; }} 5. application.properties 파일을 구성합니다
Server.port = 9090#서비스 이름 Spring.application.name = Hello-Service#서비스 등록 센터의 URL을 설정합니다. 이 서비스는 서비스 등록 센터 Eureka.client.serviceurl.defaultzone = http : // localhost : 1111/eureka에 등록해야합니다.
6. 시작 및 테스트
1.) 시작 후 Hello-Service 콘솔에는이 단어가 있습니다 (XXX는 PC 이름을 나타냅니다).
다음과 같이 코드를 복사하십시오.
다음 단어는 Eureka Console에 인쇄됩니다 (XXX는 PC 이름을 나타냅니다).
다음과 같이 코드를 복사하십시오.
2.) LocalHost : 1111을 다시 방문하면 등록 센터에 등록 된 서비스가 있음을 알게됩니다.
3. 서비스 발견 및 소비
전체 디렉토리 구조는 다음과 같습니다.
건설 과정 :
1. Maven 프로젝트 만들기 (특정 구현 생략)
2. POM 파일을 수정하고 종속성을 소개합니다
<project xmlns = "http://maven.apache.org/pom/4.0.0"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xsi : schemalocation = "http://maven.apache.org/pom/0.0.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.sam </groupid> <artifactid> hello-consumer </artifactid> <sivers> 0.0.1-snapshot> <pares> <groupid> org.springframework.boot </groupid> <artifactid> 스프링-부트 스타터-팔렌트 </artifactid> <bersion> 1.5.1. release </version> </parent> <properties> <javaversion> 1.8 </javaversion> </properties> <peciplencymanagement> <pelovemencies> <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-dependencies </artifactid> <bersion> camden.sr6 </version> <type> pom </type> <scope> 가져 오기 </scope> </dependency> </speencemanagement> <<sonceptency 관리> <!-eureka client klublencies re->의 <groupid> org.springframework.cloud </groupid> <artifactid> Spring-Cloud-Starter-Eureka </artifactid> </fectionency> <!-리본 의존성을 소개하는 데 사용됩니다. 우리는 여기서 그것을 사용하고 있으므로 다른 곳에서 그것을 소개하지 않을 것입니다-> <pectionement> <groupid> org.springframework.cloud </groupid> <artifactid> Spring-Cloud-Starter-Ribbon </artifactid> </fectionement> </fectionals> </project>
Hello Service Service 제공 업체보다 리본 의존성이 더 많습니다.
3. 시작 클래스를 만듭니다
@enablediscoveryclient@springbootapplicationpublic class consumerapp {//@bean은 메소드에 적용되어 메소드 리턴 값을 bean @loadbalanced //@loadbalanced로 설정하는 데 사용됩니다. } public static void main (String [] args) {springApplication.run (consumerapp.class, args); }}@enablediscoveryclient는 서비스가 Eureka 서버를 사용하여 서비스 등록 및 발견을 실현할 수 있도록 여기에 사용해야합니다.
4. 컨트롤러를 만듭니다
@RestControllerPublic Class ConsiderController {// 여기에 주입 된 resttemplate는 com.sam.consumerapp @autowired resttemplate resttemplate에서 @bean을 통해 구성된 인스턴스입니다. @requestmapping ( "/hello-consumer") public string helloconsumer () {// Hello-Service Service를 호출하면 서비스 이름이 특정 IP+Port restTemplate.getForObject가 아닌 여기에서 사용됩니다. "Hello Consumer Finish !!!"를 반환합니다. }}5. application.properties 파일을 구성합니다
Server.port = 9999Spring.application.name = hello-consumereureka.client.serviceurl.defaultzone = http : // localhost : 1111/eureka#여기 구성 프로젝트는 서비스 제공 업체 Hello Service와 동일합니다.
6. 시작, 테스트 1) 유레카 시작. 균형에 대한 책임이 있음을 보여주기 위해 Hello Service는 두 가지 서비스를 시작합니다. 두 서비스를 시작하는 특정 단계는 다음과 같습니다.
위는 포트 번호 9090 인 hello-service1의 시작 단계입니다. 포트 번호 9091과 함께 동일한 방법이 Hello-Service2로 설정됩니다 (특정 구현은 생략 됨).
2.) Hello-Consumer를 시작하십시오
3.) http : // localhost : 1111/다시 방문하면 2 개의 Hello Service Services (포트 번호는 9090 및 9091)와 1 개의 Hello-Consume Service가 있음을 알 수 있습니다.
4.) http : // localhost : 9999/hello-consumer를 여러 번 방문한 후, 당신은 hello-service1과 hello-service2가 차례로 호출 될 것이라는 것을 알게 될 것입니다 (균형에 대한 책임은 달성되었습니다). 둘의 콘솔을 통해 컨텐츠를 확인할 수 있습니다 (우리는 A a LoggerLogger.info ( "*********" + instervice.ggervice.ggeService.ggeService.ggeService.) 예, 이것은 인쇄입니다)
4. 요약
위의 예는 기본 서비스 거버넌스를 실현합니다.
추신 : 여기에서 내가 사용하는 IDE는 스프링 툴 스위트 (Spring Tool Suite)이며, 스프링 일식의 맞춤형 버전인데, 이는 스프링을 개발하기에 편리합니다. 관심있는 친구들은 바이두에서 그것에 대해 배울 수 있습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.