소개
전에 봄 클라우드 구성 기본 이 문서는 봄 클라우드 구성 구성의 실현에 대해 설명 센터를 기반 봄 클라우드 구성 결합 nacos 오늘 이야기를 계속 할 서비스 등록 센터, 멀티 프로젝트, 멀티 구성 파일, 기능의 프로젝트 디렉토리의 묘사에 따라 구성 서비스 센터.
이 글을 읽기 전에, 그것은 nacos 근거가하는 것이 가장 좋습니다, 어떤 nacos하여 사용하는 방법이다, 당신이 내 이전 기사를 참조 할 수 있습니다 봄 클라우드 알리바바의 (a) nacos 서비스 등록 및 검색 사용하는 방법에 공식 웹 사이트 튜토리얼로, 또는 직접 링크를 Nacos 빠른 시작
이 예를 주요 내용
- Nacos이 새로 지어진 알리 - nacos - 설정 - 서버 구성 서비스 센터 프로젝트의 기초 할 서비스 레지스트리, 봄 클라우드 구성 구성 서비스 센터의 활용, 알리 - nacos - 설정 - 클라이언트는 클라이언트 프로젝트를 구성하고, 알리 -nacos - 소비자 꾀병 구성 중심으로부터 구성을로드하도록 조정될
- 자동 식별의 응용 프로그램을 사용하여 / 클라우드 알리바바 / 설정 - REPO / {응용 프로그램} / 디렉토리 조회 : 멀티 프로젝트, 프로젝트 계획에 의해 설정-REPO 디렉토리 프로파일 이름, 구성 센터 searchPaths 지원
- 단일 프로젝트 지원 여러 개의 프로필, 프로필 spring.cloud.config.name 알리 - nacos - 설정 - 클라이언트 프로그램 = $ {spring.application.name}, MyConfig를, 이름의 복수를 지정하여 멀티 프로필
구현 예 공정
새로운 알리 - nacos - 설정 - 서버 프로젝트
이 프로젝트는 구성 서비스 센터에 사용되는 코드의 다음과 같은 주요 섹션을 게시
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-config-server</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 8001
spring:
application:
name: ali-nacos-config-server
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server:
git:
#uri: https://github.com/smltq/spring-boot-demo.git
uri: https://gitee.com/tqlin/spring-boot-demo.git
searchPaths: /cloud-alibaba/config-repo/{application}/
force-pull: true
클래스 AnConfigServerApplication.java 시작
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class AnConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(AnConfigServerApplication.class, args);
}
}
새로운 알리 - nacos - 설정 - 클라이언트 프로젝트
이 프로젝트는 하나 개의 유통 센터 클라이언트 테스트에 사용되며, 다음과 같은 몇 가지 중요한 코드를 게시
pom.xml 파일
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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-netflix-ribbon</artifactId>
</dependency>
</dependencies>
bootstrap.yml
spring:
application:
name: ali-nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
name: ${spring.application.name},myconfig
uri: http://localhost:8001/ # config server 配置服务地址
profile: ${spring.profiles.active}
label: master
profiles:
active: pro # 配置文件版本(该示例分为test,dev,pro)
테스트 클래스 HelloController.java을 읽을 수있는 구성을 쓰기
@RestController
public class HelloController {
@Value("${easy.hello}")
private String hello;
@Value("${easy.myconfig}")
private String myconfig;
@RequestMapping("/hello")
public Map hello() {
Map map = new HashMap<>();
map.put("hello", hello);
map.put("myconfig", myconfig);
return map;
}
}
클래스 AnConfigClientApplication.java 시작
@SpringBootApplication
@EnableDiscoveryClient
public class AnConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(AnConfigClientApplication.class, args);
}
}
조정 알리 - nacos을-소비자 체하다 프로젝트
코드의 다음 조정 게시
pom.xml 파일 증가 스프링 클라우드 스타터 설정에 의존
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</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-config</artifactId>
</dependency>
</dependencies>
YML 프로필 증가 bootstrap.yml, 프로파일 이동 코어 구성
bootstrap.yml을
spring:
application:
name: ali-nacos-consumer-feign
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
name: ${spring.application.name}
uri: http://localhost:8001/ # config server 配置服务地址
profile: ${spring.profiles.active}
label: master
profiles:
active: dev # 配置文件版本(该示例分为test,dev,pro)
읽기 구성을 쓰기 테스트 클래스를 작성 HomeController.java
@RestController
@Slf4j
public class HomeController {
@Autowired
private HelloService helloService;
@Value("${easy.hello}")
private String hello;
@GetMapping(value = "/", produces = "application/json")
public String home() {
log.info("-----------------consumer调用开始-----------------");
String param = "云天";
log.info("消费者传递参数:" + param);
String result = helloService.hello(param);
log.info("收到提供者响应:" + result);
return "feign消费者" + result;
}
@RequestMapping("/hello")
public Map hello() {
Map map = new HashMap<>();
map.put("hello", hello);
return map;
}
}
마지막으로, 프로파일 디렉토리 계획을 넣어
설정 - 환매 특약 구성 목록
알리 - nacos - 설정 - 서버 구성 디렉토리 프로젝트 GIT
알리 - nacos - 소비자 체하다 GIT 프로젝트 구성 디렉토리
사용의 예
기초에, 우리는 두 항목을 구축하고, 원격 구성 읽기를 지원하는 테스트를 위해 다음과 같은 항목이 알리 - nacos - 소비자 체하다 프로젝트를 조정합니다.
알리 - nacos - 설정 - 서버 : 알리 - nacos - 설정 - 서버 : 서비스 센터, 서비스 이름 구성 , 포트 : 8001
알리 nacos - 설정 - 클라이언트 : 클라이언트 구성 1 (소비자 측), 서비스 이름 : 알리 - nacos-을 설정 - 클라이언트 포트 : 8002
알리 nacos - 소비자 척하기 : 클라이언트 2 (소비자 측)를 구성, 서비스 이름 : 알리 - nacos은 소비자가-가장하다 , 포트 : 9101
테스트를 실행
서비스 레지스트리 nacos을 시작하는 첫 번째
알리 - nacos - 설정 - 서버 서비스를 시작, 구성 서비스 센터 시험
- 방문에 http : // localhost를 : 8001 / 알리 - nacos - 설정 - 클라이언트 / dev에 반환 :
{
name: "ali-nacos-config-client",
profiles: [
"dev"
],
label: null,
version: "5456d7ca31d46e91464b6efd3a0831a8208413d9",
state: null,
propertySources: [ ]
}
- 방문에 http : // localhost를 : 8001 / 알리 - nacos - 설정 - 클라이언트 / 테스트, 반환 :
{
name: "ali-nacos-config-client",
profiles: [
"test"
],
label: null,
version: "5456d7ca31d46e91464b6efd3a0831a8208413d9",
state: null,
propertySources: [ ]
}
이 올바르게 자식 이동에서로드하여 구성된 것을 의미한다.
알리 - nacos - 설정 - 클라이언트 서비스를 시작, 클라이언트 테스트를 실행 1
- 활성 디바이스에 bootstrap.yml 조정, 방문에 http : // localhost를 : 8002 / 안녕하세요, 반환
{
hello: "ali-nacos-config-client 项目的 dev config",
myconfig: "ali-nacos-config-client 项目的 myconfig config"
}
- 활성 시험, 방문에 bootstrap.yml 조정 : HTTP : // localhost를 : 8002 / 안녕하세요, 반환
{
hello: "ali-nacos-config-client 项目的 test config",
myconfig: "ali-nacos-config-client 项目的 myconfig config"
}
프로젝트 두 개의 구성 파일에 내 자식을 표현 성공적으로 읽습니다.
시작을 알리 nacos - 소비자 체하다 프로젝트, 테스트 클라이언트 테스트 2
방문에 http : // localhost를 : 9101 / 안녕하세요
결과로 돌아 가기
{
hello: "ali-nacos-consumer-feign 项目的 dev config"
}
이 프로젝트는 구성 파일은 성공을로드 나타냅니다