머리말
@DiscoveryClient 주석을 사용할 때 다음과 같은 질문이 있습니다. 왜 서비스 등록 작업을 수행합니까? 서비스 발견으로 사용해서는 안됩니까? 소스 코드를 깊이 탐색합시다.
1. SpringFramework의 수명주기 인터페이스
이 문제를 이해하려면이 중요한 인터페이스를 이해해야합니다.
/ * * 저작권 2002-2015 원래 저자 또는 저자. * * Apache 라이센스에 따라 라이센스가 부여 된 버전 2.0 ( "라이센스"); * 라이센스를 준수하는 것 외에는이 파일을 사용할 수 없습니다. * 귀하는 * * http://www.apache.org/license/license/license-2.0 *에서 라이센스 사본을 얻을 수 있습니다. 적용 가능한 법률에 의해 요구되거나 서면으로 동의하지 않는 한, 라이센스에 따라 배포 된 소프트웨어 *는 "기준"에 배포됩니다. * 라이센스에 따른 특정 언어 통치 권한 및 * 제한 사항에 대한 라이센스를 참조하십시오. */패키지 org.springframework.context;/*** 시작/정지 수명주기 제어를위한 일반적인 인터페이스 정의 방법. *이를위한 일반적인 사용 사례는 비동기 처리를 제어하는 것입니다. * <b> 참고 :이 인터페이스는 특정 자동 시작 의미를 의미하지 않습니다. * 해당 목적으로 {@link smartLifeCycle} 구현을 고려하십시오. </b> * * * <p>는 두 구성 요소 (일반적으로 * 스프링 컨텍스트에 정의 된 스프링 빈)와 컨테이너 (일반적으로 스프링 {@link applicationcontext} * 자체)에 의해 구현 될 수 있습니다. 컨테이너는 각 컨테이너 내에 적용되는 모든 구성 요소에 시작/정지 신호를 전파합니다. * * <p>는 직접 호출 또는 JMX를 통한 관리 작업에 사용할 수 있습니다. 후자의 경우 {@link org.springframework.jmx.export.mbeanexporter} *는 일반적으로 * {@link org.springframework.jmx.export.assembler.interfacebasedmbeanInfoassembler}로 정의됩니다. * * <p> 수명주기 인터페이스는 <b> 최상위 싱글 톤 * 콩 </b>에서만 지원됩니다. 다른 구성 요소의 경우, 수명주기 인터페이스는 검출되지 않은 상태로 유지되므로 무시됩니다. 또한 확장 {@link smartLifeCycle} 인터페이스 *는 응용 프로그램 컨텍스트의 시작 및 종료 단계와 통합을 제공합니다. * * * @Author Juergen Hoeller * @since 2.0 * @see smartLifeCycle * @see configurableApplicationContext * @see org.sprameframework.jms.listener.abstractmessagelistenercontainer * @see org.springframework.schedlecy.schedulercodorybean */public interfore. { /***이 구성 요소를 시작하십시오. 구성 요소가 이미 실행중인 경우 * <p>는 예외를 던지지 않아야합니다. * <p> 컨테이너의 경우 적용되는 모든 * 구성 요소로 시작 신호를 전파합니다. * @see smartLifeCycle#isaUtostArtup () */ void start (); /** *이 구성 요소, 일반적으로 동기식 으로이 구성 요소를 중지하여 구성 요소 가이 방법을 반환하면 완전히 중지됩니다. 비동기 정지 동작이 필요한 경우 {@link smartlifecycle} * 및 {@code stop (runnable)} 변형 구현을 고려하십시오. * <p>이 정지 알림은 파괴 전에 오는 것이 보장되지 않습니다. 그러나 * 컨텍스트의 생애 동안 또는 낙태 된 새로 고침 시도에서 뜨거운 새로 고침에서는 파괴 방법 만 호출됩니다. 구성 요소가 아직 시작되지 않은 경우 * <p>는 예외를 던지지 않아야합니다. * <p> 컨테이너의 경우, 적용되는 모든 구성 요소 *로 정지 신호를 전파합니다. * @Secee SmartLifeCycle#stop (runnable) * @see org.springframework.beans.bean.disposablebean#destroy () */ void stop (); /***이 구성 요소가 현재 실행 중인지 확인하십시오. * <p> 컨테이너의 경우, 적용되는 모든 구성 요소가 현재 실행중인 경우에만 {@code true}를 반환합니다. * @return 구성 요소가 현재 실행 중인지 */ boolean isrunning ();}이 인터페이스는 시작/정지 수명주기 제어 방법을 정의합니다. 스프링 IOC 컨테이너가 시작되거나 정지되면 시작 또는 정지 신호가 각 구성 요소로 전송되므로 해당 방법으로 원하는 작업을 수행 할 수 있습니다. 클래스 다이어그램을 통해 일반적으로 classPathXmlApplicationContext 클래스를 사용하는 것을 찾을 수 있습니다.
사례를 간단히 보여주고 mylifecycle 수업을 만들어 봅시다.
package org.hzgj.spring.study.context; import org.springframework.context.smartlifecycle; public class mylifecycle 구현 smartlifecycle {@override public void start () {system.out.println ( "mylifecycle start ...."); } @override public void stop () {system.out.println ( "myLifeCycle stop ....."); } @override public boolean isrunning () {return false; } @override public boolean isautostartup () {return true; } @override public void stop (runnable 콜백) {} @override public int getPhase () {System.out.println ( "phase"); 반환 10; }}여기서 우리는 smartlifecycle을 상속합니다. 인터페이스는 수명주기를 상속합니다. isrunning 방법은 현재 구성 요소가 실행중인 상태인지 여부를 감지하는 데 사용됩니다. 반환 값 isrunning이 False 일 때만 실행할 수 있습니다.
MyLifeCycle을 Spring 구성 파일로 구성하고 ClassPathXmlApplicationContext를 통해 실행하여 다음 결과를 얻습니다.
또한 여기서 GetPhase 방법은 스테이지 값을 정의하는 것입니다 (우선 순위로 이해 될 수 있습니다. 값이 작을수록 해당 수명주기가 먼저 실행됩니다).
2. DiscoveryClient 소스 코드 연구
@enablediscoveyclient
/ * * 저작권 2013-2015 원래 저자 또는 저자. * * Apache 라이센스에 따라 라이센스가 부여 된 버전 2.0 ( "라이센스"); * 라이센스를 준수하는 것 외에는이 파일을 사용할 수 없습니다. * 귀하는 * * http://www.apache.org/license/license/license-2.0 *에서 라이센스 사본을 얻을 수 있습니다. 적용 가능한 법률에 의해 요구되거나 서면으로 동의하지 않는 한, 라이센스에 따라 배포 된 소프트웨어 *는 "기준"에 배포됩니다. * 특정 언어 거버넌스 권한 및 라이센스에 따른 제한에 대한 라이센스를 참조하십시오. */패키지 org.springframework.cloud.client.discovery; import java.lang.annotation.documented; import java.lang.elementtype; import java.lang.annotation.inherited; import java.lang.annotation.restention; import java.lang.annotation. retonationpolicy; java.lang.annotation.target; import org.springframework.context.annotation.import;/*** discoveryclient 구현을 가능하게하는 주석. * @Author Spencer gibb * /@target (elementType.type) @retention (retentionpolicy.runtime)@documented@inherited@import (enablediscoveryclientimportselector.class) public @interface enablediscoveryclient { /** * Serviceregistry는 로컬 서버에 자동으로 등록합니다. */ boolean autoregister () 기본값;} @Import(EnableDiscoveryClientImportSelector.class) 이 클래스를 참조 할 수 있습니다.
/ * * 저작권 2013-2015 원래 저자 또는 저자. * * Apache 라이센스에 따라 라이센스가 부여 된 버전 2.0 ( "라이센스"); * 라이센스를 준수하는 것 외에는이 파일을 사용할 수 없습니다. * 귀하는 * * http://www.apache.org/license/license/license-2.0 *에서 라이센스 사본을 얻을 수 있습니다. 적용 가능한 법률에 의해 요구되거나 서면으로 동의하지 않는 한, 라이센스에 따라 배포 된 소프트웨어 *는 "기준"에 배포됩니다. * 라이센스에 따른 특정 언어 통치 권한 및 * 제한 사항에 대한 라이센스를 참조하십시오. */패키지 org.springframework.cloud.client.discovery; import org.springframework.boot.bind.relaxedPropertyresolver; import org.springframework.cloud.commons.util.springfactoryimportselector; import org.springframework.core.core.core.core.cored; org.springframework.core.annotation.annotationattributes; import org.springframework.core.annotation.order; import org.spramframework.core.env.configurableenvironment; import org.springframework.core.env.envonderment; import org.springframework.core.env.mappropertysource; import org.springframework.core.type.annotationmetadata; import java.util.arraylist; import java.util.arrays; import java.util.linkedhashmap import java.util.list;/** @ @ @ @@auther java.util.list; */ @order (ordered.lowest_precedence -100) public class enableableDiscoveryClientImportSelector 확장 springFactoryImportSelector <enabledIscoveryClient> {@override public string [] selectimports (AnnotationMetAdata metadata) {String [] imports = super.selectimports (metadata); AnnotationAttributes attributes = AnnotationAttributes.frommap (metadata.getAnnotationAttributes (getAnnotationClass (). getName (), true); 부울 autoregister = attributes.getBoolean ( "autoregister"); if (autoregister) {list <string> importSlist = new ArrayList <> (Arrays.Aslist (imports)); importSlist.add ( "org.springframework.cloud.client.serviceRegistry.AUTOSERVICEREGESTRATIONCONFIGURATION"); imports = importSlist.toArray (새 문자열 [0]); } else {환경 env = getEnvironment (); if (configurableEnvironment.class.isinstance (ENV)) {configurableenvironment configenv = (configurableEnvironment) ENV; LinkedHashMap <문자열, 개체>지도 = 새로운 LinkedHashMap <> (); map.put ( "spring.cloud.service-registry.auto-registration.enabled", false); MapPropertySource PropertySource = New MapPropertySource ( "SpringCloudDiscoveryClient", MAP); configenv.getPropertySources (). AddLast (PropertySource); }} 반환 가져 오기; } @override protected boolean isenabled () {return new allingpropertyresolver (getenvironment ()). getProperty ( "spring.cloud.discovery.enabled", boolean.class, boolean.true); } @override protected boolean hasdefaultFactory () {return true; }} 이 클래스를 다시 쓰는 방법은 Interface importSelector에서 나옵니다. if(autoRegister) : org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration 의 코드에 따라 클래스를 추적 할 수 있습니다. 구조 다이어그램을 살펴 보겠습니다.
이 클래스는 수명주기 인터페이스를 구현한다는 것을 알 수 있으므로 부모 클래스 AbstractDiscoveryLifeCycle에있는 시작 방법을 살펴 보겠습니다.
/ * * 저작권 2013-2015 원래 저자 또는 저자. * * Apache 라이센스에 따라 라이센스가 부여 된 버전 2.0 ( "라이센스"); * 라이센스를 준수하는 것 외에는이 파일을 사용할 수 없습니다. * 귀하는 * * http://www.apache.org/license/license/license-2.0 *에서 라이센스 사본을 얻을 수 있습니다. 적용 가능한 법률에 의해 요구되거나 서면으로 동의하지 않는 한, 라이센스에 따라 배포 된 소프트웨어 *는 "기준"에 배포됩니다. * 라이센스에 따른 특정 언어 통치 권한 및 * 제한 사항에 대한 라이센스를 참조하십시오. */패키지 org.springframework.cloud.client.discovery; import java.util.concurrent.atomic.atomicboolean; import java.util.concurrent.atomic.atomicinteger; import javax.annotation.predestroy; import org.apache.commons.loggging .logging; org.apache.commons.logging.logfactory; import org.springframework.beans.beansexception; import org.spramepramework.boot.context.embedded.embeddedservletcontainerinitializedevent; import org.springframework.cloud.discovery.event.event org.springframework.cloud.client.serviceregistry.serviceregistry; import org.springframework.context.applicationcontext; import org.springframework.context.applicationcontextaware; import org.springframework.context.applicationListenser; org.springframework.core.env.environment;/*** 다양한 발견 클라이언트 구현에 유용하고 공통적 일 수있는 수명주기 방법. * * @deprecated use {@link org.springframework.cloud.client.serviceregistry.abstractautoServiceRegration} 대신. 이 수업은 다음 릴리스 열차에서 제거됩니다. * * @Author Spencer Gibb */ @wamberatedPublic Abstract Class AbstractDiscoveryLifeCycle은 DiscoveryLifeCycle, ApplicationListener <embeddedServletContainerInitializeEvent> {private static final logger = logfactory.getLog (acpractDiscOveryLifeCycle.getLog); 개인 부울 autostartup = true; 개인 AtomicBoolean Running = New Atomicboolean (False); 개인 int order = 0; Private ApplicationContext 컨텍스트; 개인 환경 환경; 개인 atomicinteger port = 새로운 atomicinteger (0); 보호 된 ApplicationContext getContext () {return Context; } @override public void setApplicationContext (ApplicationContext ApplicationContext)는 beansexception {this.context = ApplicationContext; this.environment = this.context.getenvironment (); } @Deprecated Protected Environment getEnvironment () {반환 환경; } @deprecated Protected atomicinteger getport () {return port; } @override public boolean isautostartup () {return this.AutosTartup; } @override public void stop (runnable 콜백) {try {stop (); } catch (예외 e) {logger.error ( "발견 수명을 중지하려고 시도한 문제", e); } callback.run (); } @override public void start () {if (! isenabled ()) {if (logger.isdebugenabled ()) {logger.debug ( "Discovery Lifecycle Disabled. 시작하지 않음"); } 반품; } // nonsecureport가 0이고 this.port! = 0 if (this.port.get ()! = 0 && getConfiguredport () == 0) {setConfiguredPort (this.port.get ()); } // nonsecureport가 0보다 큰 경우에만 초기화하고 // if (! this.running.get () && getConfiguredport ()> 0) {register (); if (thiteRegisterManagement ()) {registerManagement (); } this.context.publishevent (new instancerEgisteredevent <> (this, getConfiguration ()); this.running.compareAndset (false, true); }} @DepRecated Protected Abstract int getConfiguredport (); @Deprecated Protected Abstract void setConfiguredport (int port); / ** * @return 관리 서비스가 {@link serviceregistry}에 등록되어야하는 경우 {@link serviceregistry} */ protected boolean rookistermanagement () {return getmanagementport ()! = null && managementserverportutils.isdifferent (this.context); } / ** * @return 등록을 구성하는 데 사용되는 객체 * / @deprecated protected 객체 getConfiguration (); / ** * DiscoveryClient */ Protected Abstract void register ()로 로컬 서비스를 등록하십시오. / ** * DiscoveryClient */ Protected Void RegisterManagement () {}/ ** *로 지역 관리 서비스를 등록하십시오. / ** * DiscoveryClient */ Protected Void DereGisterManagement () {}/ ** * @return true로 로컬 관리 서비스를 등록 해제합니다. / ** * @@Return 관리 서비스의 ServiceD */ @DepRecated Protected String getManagementServiceId () {// todo : configurable management 접미사 retrys.context.getId () + ": management"; } / ** * @return 관리 서비스의 서비스 이름 * / @deprecated protected string getManagementServicEname () {// todo : 구성 가능한 관리 접미사 return getAppName () + ": 관리"; } / ** * @ @return 관리 서버 포트 * / @deprecated 보호 정수 getmanagementport () {return managementserverportUtils.getport (this.context); } / ** * @ @앱 이름, 현재 spring.application.name 속성 * / @deprecated protected string getAppName () {return this.environment.getProperty ( "spring.application.name", "application"); } @override public void stop () {if (this.running.compareAndset (true, false) && isenabled ()) {deregister (); if (rithregistermanagement ()) {deregistermanagement (); }}} @predestroy public void destroy () {stop (); } @override public boolean isrunning () {return this.running.get (); } 보호 된 AtomicBoolean getRunning () {return running; } @override public int getOrder () {return this.order; } @override public int getPhase () {return 0; } @override @deprecated public void onapplicationEvent (embeddedServletContainerInitializeEvent 이벤트) {// todo : ssl을 고려하여 // 관리 포트를 포트로 등록하지 마십시오. event.getEmbeddedServletContainer (). getPort ()); this.start (); }}}시작 방법에는이 코드의 부분이 있습니다.
if (! this.running.get () && getConfiguredport ()> 0) {register (); if (thiteRegisterManagement ()) {registerManagement (); } this.context.publishevent (new instancerEgisteredevent <> (this, getConfiguration ()); this.running.compareAndset (false, true); } register() 메소드는이 클래스에서 추상적 인 메소드입니다. 따라서 AbstractOutoServiceRegration 클래스의 코드를 되돌아 보겠습니다. 여기에는 주요 부분 만 게시하겠습니다.
// ..... 보호 된 추상 AUTOSERVICEREGRATION (ServiceRegistry <R> ServiceRegistry, AutoServiceRegationProperties 속성) {this.serviceregistry = ServiceRegistry; this.properties = 속성; } //....../** * {@link serviceregistry} */@override protected void register () {this.serviceregistry.register (getRegistration ())에 로컬 서비스를 등록합니다. }SpringCloud가 서비스 등록을 위해 제공하는 인터페이스 인 ServiceRegistry 유형이 생성자에 전달된다는 것을 알 수 있습니다. 여기에서 eureKaserviceRegistry는이 인터페이스를 구현합니다.
/ * * 저작권 2013-2016 원래 저자 또는 저자. * * Apache 라이센스에 따라 라이센스가 부여 된 버전 2.0 ( "라이센스"); * 라이센스를 준수하는 것 외에는이 파일을 사용할 수 없습니다. * 귀하는 * * http://www.apache.org/license/license/license-2.0 *에서 라이센스 사본을 얻을 수 있습니다. 적용 가능한 법률에 의해 요구되거나 서면으로 동의하지 않는 한, 라이센스에 따라 배포 된 소프트웨어 *는 "기준"에 배포됩니다. * 라이센스에 따른 특정 언어 통치 권한 및 * 제한 사항에 대한 라이센스를 참조하십시오. * */패키지 org.springframework.cloud.netflix.eureka.serviceregistry; import java.util.hashmap; import org.apache.commons.logging.log; import org.apache.commons.logging.log org.springframework.cloud.servicegretery.servicegrety.commons com.netflix.appinfo.instanceinfo;/** * @Author Spencer Gibb */public class eureKaserviceRegistry 구현 <유레 케어 로그 (eureKaregistration) {private static final log = logactory.getLog (eurekaserviceregistry.class); @override public void register (eurekaregistration reg) {maynitializeclient (reg); if (log.isinfoenabled ()) {log.info ( "응용 프로그램 등록" + reg.getInstanceConfig (). getAppName () + "with eureka" + reg.getInstanceConfig (). getInitialStatus ()); } reg.getApplicationInfomanager () .setInstanCestatus (reg.getInstanceConfig (). getInitialStatus ()); if (reg.gethealthcheckhandler ()! = null) {reg.geteureKaclient (). RegisterHealthCheck (reg.gethealthcheckhandler ()); }} private void mayginitializeclient (eureKareGistration reg) {// 가능한 스코프 프록시의 초기화 reg.getApplicationInfomanager (). getInfo (); reg.geteureKaclient (). getApplications (); } @override public void deregister (eureKareGistration reg) {if (reg.getApplicationInfomanager (). getInfo ()! = null) {if (log.isinfoEnabled ()) {log.info ( "응용 프로그램이 reging" + reg.getInstanceConfig (). } reg.getApplicationInfomanager (). setInstancestatus (instanceInfo.instancestatus.down); // Eureka 클라이언트의 종료가 eureKareGistration.close () // 자동 등록은 적절하게 배포 될 Bean을 생성합니다. // 수동 등록은 Close ()}} @override public void setstatus (eureKaregistration Registration, String Stature) {registration.ggetInficationInfication (getInfitration) // TODO : 삭제를 올바르게 처리하는 방법은 무엇입니까? if ( "cancel_override".EqualSignoreCase (status)) {registration.geteureKaclient (). CancelOverRidestatus (info); 반품; } // todo : 발견 시스템에서 상태 유형을 다루는 방법? instanceInfo.instancestatus newstatus = instancesinfo.instancestatus.toenum (상태); registration.geteureKaclient (). SetStatus (Newstatus, Info); } @override public Object getStatus (eureKareGistration registration) {hashmap <String, Object> status = new Hashmap <> (); instanceInfo info = registration.getApplicationInfomanager (). getInfo (); status.put ( "status", info.getStatus (). toString ()); status.put ( "reveriddenStatus", info.getOverRiddenStatus (). toString ()); 반환 상태; } public void close () {}}따라서 다음 사항을 요약 할 수 있습니다.
1. @DiscoveryClient를 사용하여 서비스를 등록하면 수명주기 메커니즘을 사용하며 컨테이너가 시작되면 Serviceergistry의 register() 메소드가 실행됩니다.
2. @DiscoveryClient를 사용하는 것은 @enableEureKaclient 및 @enableEureKaserver보다 유연합니다. 서비스 등록의 구현을 차단하고 등록 센터를 사용자 정의 할 수도 있기 때문입니다.
3. 이것은 또한 서비스 검색을위한 DiscoveryClient 인터페이스의 구현을 자동으로 검색합니다.
3. DiscoveryClient 실제 Redis 등록 센터
아래에서는 Redis를 기반으로 DiscoveryClient를 이해하기위한 등록 센터로서 요구 사항을 구현합니다. 그건 그렇고, SpringCloud의 중요한 인터페이스 : Serviceregistry, ServiceStance를 이해해 봅시다. 그 전에는 Redis에 대한 지원을 추가 할 것입니다.
컴파일 그룹 : 'org.springframework.boot', 이름 : 'Spring-Boot-Starter-Data-Redis'
1. 등록 인터페이스를 구현하십시오
package com.hzgj.lyrk.memb; import org.springframework.beans.beans.annotation.value; import org.springframework.cloud.client.serviceregistry.registration; import org.springframework.stereotyp.component; import java.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net.net java.net.uri; import java.util.enumeration; import java.util.map; @componentpublic 클래스 retisregistration registration {@Value ( "$ {server.port}") 개인 정수 포트; @Value ( "$ {spring.application.name}") private String applicationname; 개인 문자열 호스트; public void sethost (문자열 호스트) {this.host = host; } public void setport (정수 포트) {this.port = 포트; } public void setApplicationName (String applicationName) {this.applicationName = applicationName; } @override public String getServiceId () {return applicationName + ":" + gethost () + ":" + getport (); } @override public String gethost () {try {if (host == null) return getLocalHostLanAddress (). gethostAddress (); 그렇지 않으면 호스트를 반환합니다. } catch (예외 e) {e.printstacktrace (); } return null; } @override public int getport () {return port; } @override public boolean issecure () {return false; } @override public uri geturi () {return null; } @override public map <string, String> getMetadata () {return null; } public String getServicEname () {return this.applicationName; } public inetAddress getLocalHostLanAddress ()는 예외를 겪고 {inetAddress quandAdAddress = null; // (enumeration ifaces = workinterface.getnetworkinterfaces (); ifaces.hasmoreElements ();) {workinterface = (NetworkInterface) ifaces.nextElement (); // (Enumeration inetAddrs = iface.getInetAddresses (); inetAddrs.hasMoreElements ();) {inetAddress inetAddr = (inetAddress) inetAddrs.nextElement (); if (! inetaddr.isloopbackaddress ()) {// 루프백 유형 주소 제외 if (inetaddr.issitelocaladdress ()) {// 사이트 local 주소 인 경우 inetaddr return inetaddr입니다. } else if (candidAteAddress == null) {// 사이트-로컬 유형의 주소는 찾을 수 없었습니다. 먼저 후보자 주소를 기록하십시오. }}}}} if (candidAteadDress! = null) {return candidAteAddress; } // 루프백이 아닌 주소가없는 경우 가장 선택된 솔루션 만 사용할 수 있습니다. inetAddress jdksuppledAddress = inetAddress.getLocalHost (); jdksuppliedAddress를 반환합니다. } catch (예외 e) {e.printstacktrace (); } return null; }}이 인터페이스는 ServiceIntance를 상속 하므로이 인터페이스의 주요 기능은 ServiceID, 포트 번호 등과 같은 서비스 인스턴스의 사양을 정의하는 것입니다.
2. ServiceRegistry의 인터페이스를 구현하십시오
package com.hzgj.lyrk.member; import org.springframework.beans.beans.bean.annotation.autowired; import org.springframework.cloud.client.serviceregistry.serviceregistry; import org.spramework.data.redis.core.stringgertemplate; ServiceRegistry <RedisRgistration> {@autowired private StringRedistemplate redistemplate; @override public void register (redisregistration registration) {string serviceid = registration.getServiceId (); redistemplate.opsforList (). LeftPush (ServiceId, renistration.gethost () + ":" + rgistration.getport ()); } @override public void deregister (redisregistration registration) {redistemplate.opsforList (). remain (registration.getServiceId (), 1, registration.gethost () + ":" + registration.getport ()); } @override public void close () {//redistemplate.d system.out.println ( "closed ..."); } @override public void setstatus (rebleisregistration registration, 문자열 상태) {} @override public <t> t getStatus (retisregistration registration) {return null; }}이 인터페이스의 주요 기능은 등록, 서비스 취소, 서비스 상태 설정 및 서비스 상태를 정의하는 것입니다.
3. 추상적 인 ATOUSVICEREGRATION 추상 클래스를 상속합니다
package com.hzgj.lyrk.memb; import org.springframework.beans.beans.bean.annotation.autowired; import org.springframework.cloud.client.serviceregistry.abstractoutoserviceregistration; import org.springframework.cloud.cloud .abserviceregistration.absortopervicereverorverorvicereverorviceRerviceRerviceRerviceRerviceRerviceRerviceRegration; org.springframework.cloud.client.serviceregistry.serviceregistry; 공개 클래스 RedisAutoServiceRegistration은 추상적 인 AbstractoServiceRegatration <RedisRegistration> {@autowired Private registration Registration; 보호 된 RedisAutoserviceRegration (ServiceRegistry <RedisRegistration> Serviceregistry, AutoServiceRegeStrationProperties 속성) {Super (Serviceregistry, Properties); // serviceregistry.register (getRegistration ()); } @override protected int getConfiguredport () {return redisRgistration.getPort (); } @override protected void setconfiguredport (int port) {} @override protected object getConfiguration () {return null; } @override protected boolean isenabled () {return true; } @override protected redisgistration getRegistration () {return redisregistration; } @override protected revistration getManagementRegistration () {return null; }}4. DiscoveryClient RedisDiscoveryClient의 구현 클래스를 정의하십시오
package com.hzgj.lyrk.memb; import org.apache.commons.lang.stringUtils; import org.springframework.beans.beans.annotation.autowired; import org.springframework.cloud.client.serviceinstance; import org.springframework.cloud.client.discovery.discoverofofofovery org.springframework.data.redis.core.stringredistemplate; import java.util.arraylist; import java.util.list; import java.util.function.function; import java.util.stream.collectors; 공개 클래스 RedisDiscoveryClient reclient {aUaUried private strestemplate plentestemplate; @override public string description () {return "Redis 등록 센터의 서비스 발견"; } @override Public ServiceInstance getLocalServiceInstance () {return null; } @override public list <werviceInstance> getInstances (String ServiceId) {return redistemplate.opsforList (). 범위 (ServiceID, 0, -1). parallelStream (). map ((function <string, serviceInstance>) s-> {redisregistration redisRgistration = new retisRgistration (); rebleisRgistration.setApplicationName (serviceId); String hostName = StringUtils.split (s, ":": String port = stringUtils.split (s, "); redisregistration.sethost (hostname). } @override public list <string> getServices () {list <string> list = new ArrayList <> (); list.addall (redistemplate.keys ( "*")); 반환 목록; }}이 수업은 주로 Redis 등록 센터의 서비스 검색을 목표로합니다.
5. 자동 조립 클래스를 정의하여 해당 콩을 생성합니다
package com.hzgj.lyrk.member; import org.springframework.autoconfigure.condition.conditionalonProperty; import org.springframework.context.properties.enableconfigurationProperties; import org.springframework.cloud.client.serviceregistry.autoServiceRegeStrationProperties; import org.springframework.context.annotation.bean; import org.sprameframework.context.annotation.configuration; import org.springframework.context.annotation.primary;@configuration@enableconfigurationProperties (readisconfig.class) @conditionalonProperty (value = "spring.redis.registry.enabled", matchifmissing = true), 공공 클래스 redisregistrice -configuration {@beconfiguration redisserviceregistry (readisconfig readisconfig) {system.out.println (readisconfig.gethost ()); 새로운 RedisserViceRegistry ()를 반환합니다. } @bean redisautoServiceRegration redisAutoServicEREGESTRATION (REDISSERVICEREGISTRY READISSERVICEREGISTRY) {RETURN NEW REDISOUTOSERGISTRY (rebdisserviceRegistry, New AutoserviceRegationStrationProperties ()); } @bean @primary redisdiscoveryclient redisdiscoveryclient () {return new redisdiscoveryclient (); }}6. 시작 클래스를 정의하십시오
package com.hzgj.lyrk.member; import org.springframework.springApplication; import org.springframework.boot.autoconfigure.springbootApplication; import org.spramframework.cloud.discovery.discoveryclient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration;import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;import org.springframework.context.configurablepplicationcontext;@enablediscoveryclient@springbootApplication (exclude = {simpledediscoveryclientAutoconfiguration.class, compositediscoveryClientStateConfiguration.class}) public memberpolication void (string) [ARGS). ApplicationContext = SpringApplication.Run (MemberApplication.Class, Args); DiscoveryClient DiscoveryClient = ApplicationContext.getBean (DiscoveryClient.class); DiscoveryClient.getServices (). foreach (action-> {system.out.println (action);}); }}여기서는 DiscoveryClient의 기본 어셈블리가 SpringBootApplication 주석에 제외됩니다.
성공적으로 시작하면 콘솔에 해당 서비스 이름과 주소를 출력했음을 알 수 있습니다.
우리는 다시 한 번 Gradle 포장 및 실행을 통해 JAR 파일을 생성합니다.
java -jar 멤버-서버 -0.0.1-snapshot.jar ---server.port = 8800
우리는 Redis에서 캐시 된 서비스 등록 값을 볼 수 있습니다.
요약
위는이 기사의 전체 내용입니다. 이 기사의 내용에 모든 사람의 연구 나 작업에 대한 특정 참조 가치가 있기를 바랍니다. 궁금한 점이 있으면 의사 소통을 위해 메시지를 남길 수 있습니다. Wulin.com을 지원 해주셔서 감사합니다.