springboot 통합 nacos 설정 방법

3884 단어 springbootnacos
본 고 는 springboot 통합 nacos 의 설정 방법 을 소개 하 였 으 며,구체 적 으로 다음 과 같 습 니 다.
nacos 창고:https://github.com/alibaba/nacos
nacos 소개 문서:https://nacos.io/zh-cn/docs/architecture.html
nacos 사용 예:https://github.com/nacos-group/nacos-examples
springboot 설정-통합 nacos,예 코드 다운로드
springboot-nacos-consumer
springboot-nacos
1.설치 정보
nacos-mysql.sql 가 져 오기
나 코스 콘 솔 시작

2.게시 및 가 져 오기 설정
1)발표 2 가지 방식 설정:openapi 호출;콘 솔 조작

2)새 네 임 스페이스

3)서로 다른 namespace,서로 다른 dataId,서로 다른 groupId 설정 가 져 오기
@NacosPropertySource 에 dataId,groupId 지정

//   dataId   example     ,       
//@NacosPropertySource(dataId = "example",autoRefreshed = true)
//    groupId   
//@NacosPropertySource(dataId = "example", groupId ="test",autoRefreshed = true)
//    dataId   
@NacosPropertySource(dataId = "example2",autoRefreshed = true)
프로필 에 namespace 를 지정 한 ID

#      ID,    namespace   
#nacos.config.namespace=a8128f5b-7f87-42c5-9ffd-c4f17da7ee34
테스트:

3.서비스 등록 및 소비 서비스
1)서비스 등록
프로필 지정:

server.port= 8080
#      
server.context-path=/springboot-nacos
#Nacos   
spring.application.name=UserService
#nacos      
nacos.config.server-addr=127.0.0.1:8848
#discovery  
nacos.discovery.server-addr=127.0.0.1:8848
#      ID,    namespace   
#nacos.config.namespace=a8128f5b-7f87-42c5-9ffd-c4f17da7ee34
서비스 시작 시 등록:

@SpringBootApplication
public class SpringbootNacosApplication {
  @NacosInjected
  private NamingService namingService;
  @Value("${server.port}")
  private int serverPort;

  @Value("${spring.application.name}")
  private String applicationName;
  //curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=xxx&ip=127.0.0.1&port=8080'
  @PostConstruct
  public void registerInstance() throws NacosException {
    if(CollectionUtils.isEmpty(namingService.getAllInstances(applicationName))){
      namingService.registerInstance(applicationName,"127.0.0.1",serverPort);
    }
  }
  public static void main(String[] args) {
    SpringApplication.run(SpringbootNacosApplication.class, args);
  }

}

2)서비스 소비
Naming Service 를 통 해 선택 서비스 발견

 Instance instance = namingService.selectOneHealthyInstance("UserService");
RestTemplate 호출 을 통 해 결 과 를 가 져 옵 니 다.

springboot 통합 nacos 의 설정 방법 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 springboot 통합 nacos 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기