이번 포스팅에서는 OpenFeign 통합을 통해 Spring 환경과 다른 Spring 프로그래밍 모델에서의 자동구성과 바인딩을 해보도록 하겠습니다.
1. 선언적 REST Client: Feign
Feign은 선언적 웹 서비스 클라이언트로 web service client를 쉽게 해줍니다.
먼저, Feign을 사용하려면 인터페이스를 만들고 주석을 달아야 합니다. Feign annotation과 JAX-RS annotation을 포함한 플러그형 주석을 제공합니다. Feign은 플러그형 인코더와 디코더를 지원합니다.
Spring Cloud는 Spring MVC annotation과 Spring web에서 HttpMessageConverters와 같이 사용하기 위해 추가합니다. Spring Cloud는 Feign을 사용할 때 부하 분산 http 클라이언트를 제공하기 위해 Eureka, Spring Cloud CircuitBreaker, Spring Clould LoadBalancer를 통합합니다.
1.1 Feign 사용법
Feign을 프로젝트에 사용하기 위해 org.springframework.cloud와 artifact id spring-cloud-starter-openfeign을 포함합니다.
spring boot app 예제
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
StoreClient.java
@FeignClient("stores")
public interface StoreClient {
@RequestMapping(method = RequestMethod.GET, value = "/stores")
List<Store> getStores();
@RequestMapping(method = RequestMethod.GET, value = "/stores")
Page<Store> getStores(Pageable pageable);
@RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
Store update(@PathVariable("storeId") Long storeId, Store store);
@RequestMapping(method = RequestMethod.DELETE, value = "/stores/{storeId:\\d+}")
void delete(@PathVariable Long storeId);
}
@FeignClient annotation에서 String value("sotres" above)는 임의의 클라이언트 이름으로, Spring Cloud LoadBalnacer 클라이언트를 생성하는 데 사용됩니다. URL 속성(절대값 또는 호스트이름)을 사용하여 지정할 수도 있습니다. application context에서 bean의 이름은 인터페이스의 정규화된 이름입니다. 원하는 alias를 지정하기 위해서는 @FeignClient annotation의 qualifiers 값을 사용하면 됩니다.
load-balancer client는 "stores"서비스에 대한 물리적 주소를 알아내고자 할 것입니다. Eureka 클라이언트의 경우 Eureka 서비스 레지스트리에서 서비스를 확인합니다. Eureka를 사용하지 않으려면 SimpleDiscoveryClient를 사용하여 외구 구성에서 서버 목록을 구성할 수 있습니다.
Feign client bean을 생성하는 동안 @FeignClient 주석을 통해 전달된 값을 확인합니다. 4.x부터 즉시 확인가능하고 대부분의 사용 사례에 적합한 솔루션이며 AOT 지원도 가능합니다. 속성을 지연되는 경우 spring.cloud.openfeign.lazy-attributes-resolution property 값을 true로 변경해 보세요.
$ yarn global add knex-migrator ember-cli
yarn global v1.22.18
[1/4] Resolving packages...
warning knex-migrator > ghost-ignition > bunyan-loggly > node-loggly-bulk > request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
warning knex-migrator > ghost-ignition > bunyan-loggly > node-loggly-bulk > request > uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
warning knex-migrator > ghost-ignition > bunyan-loggly > node-loggly-bulk > request > har-validator@5.1.5: this library is no longer supported
warning ember-cli > broccoli > sane@4.1.0: some dependency vulnerabilities fixed, support for node < 10 dropped, and newer ECMAScript syntax/features added
warning ember-cli > broccoli-babel-transpiler > @babel/polyfill@7.12.1: 🚨 This package has been deprecated in favor of separate inclusion of a polyfill and regenerator-runtime (when needed). See the @babel/polyfill docs (https://babeljs.io/docs/en/babel-polyfill) for more information.
warning ember-cli > broccoli-babel-transpiler > @babel/polyfill > core-js@2.6.12: core-js@<3.4 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.
warning ember-cli > broccoli-concat > fast-sourcemap-concat > source-map-url@0.3.0: See https://github.com/lydell/source-map-url#deprecated
warning ember-cli > broccoli > sane > micromatch > snapdragon > source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated
warning ember-cli > broccoli > sane > micromatch > snapdragon > source-map-resolve > source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated
warning ember-cli > broccoli > sane > micromatch > snapdragon > source-map-resolve > resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
warning ember-cli > broccoli > sane > micromatch > snapdragon > source-map-resolve > urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
[2/4] Fetching packages...
error knex@2.0.0: The engine "node" is incompatible with this module. Expected version ">=12". Got "10.19.0"
error Found incompatible module.
info Visit https://yarnpkg.com/en/docs/cli/global for documentation about this command.
node 버전 문제로 인해 error가 발생합니다.
12 이상의 node버전이 필요하므로 node update를 먼저 실행하고 진행합니다. (위에서 언급한 node설치 포스팅을 참고하세요.)
global package를 다시 설치해보니 이번엔 잘 됩니다.
$ yarn global add knex-migrator ember-cli
yarn global v1.22.18
[1/4] Resolving packages...
warning knex-migrator > ghost-ignition > bunyan-loggly > node-loggly-bulk > request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
warning knex-migrator > ghost-ignition > bunyan-loggly > node-loggly-bulk > request > uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
warning knex-migrator > ghost-ignition > bunyan-loggly > node-loggly-bulk > request > har-validator@5.1.5: this library is no longer supported
warning ember-cli > broccoli > sane@4.1.0: some dependency vulnerabilities fixed, support for node < 10 dropped, and newer ECMAScript syntax/features added
warning ember-cli > broccoli-babel-transpiler > @babel/polyfill@7.12.1: 🚨 This package has been deprecated in favor of separate inclusion of a polyfill and regenerator-runtime (when needed). See the @babel/polyfill docs (https://babeljs.io/docs/en/babel-polyfill) for more information.
warning ember-cli > broccoli-babel-transpiler > @babel/polyfill > core-js@2.6.12: core-js@<3.4 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.
warning ember-cli > broccoli-concat > fast-sourcemap-concat > source-map-url@0.3.0: See https://github.com/lydell/source-map-url#deprecated
warning ember-cli > broccoli > sane > micromatch > snapdragon > source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated
warning ember-cli > broccoli > sane > micromatch > snapdragon > source-map-resolve > source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated
warning ember-cli > broccoli > sane > micromatch > snapdragon > source-map-resolve > resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
warning ember-cli > broccoli > sane > micromatch > snapdragon > source-map-resolve > urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "knex-migrator@4.2.10" with binaries:
- knex-migrator
- knex-migrator-init
- knex-migrator-health
- knex-migrator-migrate
- knex-migrator-reset
- knex-migrator-rollback
success Installed "ember-cli@4.3.0" with binaries:
- ember
Done in 37.60s.
~/Ghost$ yarn start
yarn run v1.22.18
warning ghost@4.46.0: The engine "cli" appears to be invalid.
$ node index
[2022-05-18 05:50:53] INFO Ghost is running in development...
[2022-05-18 05:50:53] INFO Listening on: 127.0.0.1:2368
[2022-05-18 05:50:53] INFO Url configured as: http://localhost:2368/
[2022-05-18 05:50:53] INFO Ctrl+C to shut down
[2022-05-18 05:50:53] INFO Ghost server started in 2.136s
[2022-05-18 05:50:54] INFO Database is in a ready state.
[2022-05-18 05:50:54] INFO Ghost database ready in 2.726s
[2022-05-18 05:50:59] INFO Stripe not configured - skipping migrations
[2022-05-18 05:50:59] INFO Ghost URL Service Ready in 8.238s
[2022-05-18 05:50:59] INFO Ghost booted in 8.417s
[2022-05-18 05:50:59] INFO Adding offloaded job to the queue
[2022-05-18 05:50:59] INFO Scheduling job update-check at 2 53 15 * * *. Next run on: Wed May 18 2022 15:53:02 GMT+0000 (Coordinated Universal Time)
node는 서버 프로그래밍을 위한 js 런타임으로 javaScript로 확장 가능한 백엔드 기능을 개발할 수 있습니다.
1. CURL 설치
node를 설치하기 전 커맨드 라인 툴을 설치해야 합니다.
CURL은 서버와 통신할 수 있는 커맨드 명령어 오픈소스 툴로 아래의 명령어로 설치합니다.
$ sudo apt-get install -y curl
Reading package lists... Done
Building dependency tree
Reading state information... Done
curl is already the newest version (7.68.0-1ubuntu2.7).
curl set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
2. apt 패키지 매니저 이용하여 설치
apt 패키지 매니저를 update 합니다.
$ sudo apt update
Hit:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu focal InRelease
.....
Get:42 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 c-n-f Metadata [512 B]
Fetched 22.8 MB in 4s (5470 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
48 packages can be upgraded. Run 'apt list --upgradable' to see them.
nodejs를 설치합니다.
$ sudo apt install nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
nodejs is already the newest version (10.19.0~dfsg-3ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 48 not upgraded.
버전 확인을 통해 설치 확인을 합니다.
$ nodejs -v
v10.19.0
노드 패키지 매니저인 npm을 다운로드합니다.
$ sudo apt install npm
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
...
$ sudo bash nodesource_14_setup.sh
## Installing the NodeSource Node.js 14.x repo...
## Populating apt-get cache...
+ apt-get update
Hit:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu focal-backports InRelease
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Fetched 114 kB in 2s (69.3 kB/s)
Reading package lists... Done
## Confirming "focal" is supported...
+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_14.x/dists/focal/Release'
## Adding the NodeSource signing key to your keyring...
+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee /usr/share/keyrings/nodesource.gpg >/dev/null
## Creating apt sources list file for the NodeSource Node.js 14.x repo...
+ echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_14.x focal main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_14.x focal main' >> /etc/apt/sources.list.d/nodesource.list
## Running `apt-get update` for you...
+ apt-get update
Hit:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu focal-backports InRelease
Get:4 https://deb.nodesource.com/node_14.x focal InRelease [4583 B]
Get:5 https://deb.nodesource.com/node_14.x focal/main amd64 Packages [774 B]
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease
Fetched 5357 B in 0s (11.9 kB/s)
Reading package lists... Done
## Run `sudo apt-get install -y nodejs` to install Node.js 14.x and npm
## You may also need development tools to build native addons:
sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
다운로드한 버전으로 node를 설치합니다.
$ sudo apt install nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
fontconfig-config fonts-dejavu-core gyp javascript-common libauthen-sasl-perl libc-ares2 libdata-dump-perl
libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libencode-locale-perl libfile-basedir-perl
libfile-desktopentry-perl libfile-listing-perl libfile-mimeinfo-perl libfont-afm-perl libfontconfig1 libfontenc1
libgl1 libgl1-mesa-dri libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libhtml-form-perl libhtml-format-perl
libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl libhttp-daemon-perl libhttp-date-perl
libhttp-message-perl libhttp-negotiate-perl libice6 libio-html-perl libio-socket-ssl-perl libio-stringy-perl
...
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
libnode-dev libnode64 node-gyp nodejs-doc npm
The following packages will be upgraded:
nodejs
1 upgraded, 0 newly installed, 5 to remove and 46 not upgraded.
Need to get 25.4 MB of archives.
After this operatio , 87.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://deb.nodesource.com/node_14.x focal/main amd64 nodejs amd64 14.19.3-deb-1nodesource1 [25.4 MB]
Fetched 25.4 MB in 0s (61.0 MB/s)
(Reading database ... 75731 files and directories currently installed.)
Removing npm (6.14.4+ds-1ubuntu2) ...
Removing node-gyp (6.1.0-3) ...
Removing libnode-dev:amd64 (10.19.0~dfsg-3ubuntu1) ...
Removing nodejs-doc (10.19.0~dfsg-3ubuntu1) ...
dpkg: libnode64:amd64: dependency problems, but removing anyway as you requested:
nodejs depends on libnode64 (= 10.19.0~dfsg-3ubuntu1).
...
Removing libnode64:amd64 (10.19.0~dfsg-3ubuntu1) ...
(Reading database ... 74289 files and directories currently installed.)
Preparing to unpack .../nodejs_14.19.3-deb-1nodesource1_amd64.deb ...
Unpacking nodejs (14.19.3-deb-1nodesource1) over (10.19.0~dfsg-3ubuntu1) ...
Setting up nodejs (14.19.3-deb-1nodesource1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.7) ...
Processing triggers for man-db (2.9.1-1) ...
$ sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '비밀번호';
Query OK, 0 rows affected (0.01 sec)
Ghost-CLI는 Ghost를 빠르고 쉽게 사용하기 위한 commandline 툴입니다. npm 또는 yarn을 이용하여 설치할 수 있습니다.
$ sudo npm install ghost-cli@latest -g
5. Ghost 설치
생성할 sitename으로 디렉터리를 생성합니다.
sudo mkdir -p /var/www/'sitename'
ghost-admin 사용자를 추가합니다.
$ sudo adduser ghost-admin
Adding user `ghost-admin' ...
Adding new group `ghost-admin' (1001) ...
Adding new user `ghost-admin' (1001) with group `ghost-admin' ...
Creating home directory `/home/ghost-admin' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for ghost-admin
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]
추가한 사용자에 superuser gorup을 부여합니다.
$ sudo usermod -aG sudo ghost-admin
위에서 생성한 폴더에 소유자를 변경합니다.
$ sudo chown ghost-admin:ghost-admin /var/www/'sitename'
[sudo] password for ghost-admin:
ll 명령어로 소유자가 변경됐음을 확인합니다.
$ ll
total 16
drwxr-xr-x 4 root root 4096 May 16 06:17 ./
drwxr-xr-x 14 root root 4096 May 16 05:54 ../
drwxr-xr-x 2 ghost-admin ghost-admin 4096 May 16 06:17 sitename/
drwxr-xr-x 2 root root 4096 May 16 05:54 html/
755로 권한을 변경합니다.
$ sudo chmod 775 /var/www/sitename
생성한 폴더로 이동합니다.
cd /var/www/sitename
설치 프로세스를 시작합니다.
ghost install
다운로드 및 설치에 약간의 시간이 소요됩니다.
ghost 서비스가 올라오고 http://localhost:2368/ghost로 접속하니 잘 되네요.