SQL 데이터베이스(및 HTTP/HTTPS가 아닌 다른 서비스)에 대한 연결은 AEM 외부로 프록시되어야 합니다.
전용 이그레스 ip 주소를 사용 중이고 서비스가 Adobe 또는 Azure에 있는 경우는 이 규칙에서 예외입니다.
고급 네트워킹 지원
다음 코드 예는 다음 고급 네트워킹 옵션에서 지원됩니다.
이 자습서를 수행하기 전에 적절함 고급 네트워킹 구성이 설정되었는지 확인하십시오.
OSGi 구성
암호는 코드에 저장할 수 없으므로 SQL 연결의 사용자 이름과 암호는 암호 OSGi 구성 변수을 통해 제공되는 것이 가장 좋습니다. AIO CLI 또는 Cloud Manager API를 사용하여 설정합니다.
다음 aio CLI
명령을 사용하여 환경별로 OSGi 비밀을 설정할 수 있습니다.
코드 예
이 Java™ 코드 예제는 enableEnvironmentAdvancedNetworkingConfiguration 작업의 다음 Cloud Manager portForwards
규칙을 통해 외부 SQL Server 웹 서버에 연결하는 OSGi 서비스입니다.
package com.adobe.aem.wknd.examples.core.connections.impl;
import com.adobe.aem.wknd.examples.core.connections.ExternalService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
@Component
@Designate(ocd = MySqlExternalServiceImpl.Config.class)
public class MySqlExternalServiceImpl implements ExternalService {
private static final Logger log = LoggerFactory.getLogger(MySqlExternalServiceImpl.class);
// Get the proxy host using the AEM_PROXY_HOST Java environment variable provided by AEM as a Cloud Service
private static final String PROXY_HOST = System.getenv().getOrDefault("AEM_PROXY_HOST", "proxy.tunnel");
// Use the port mapped to the external MySQL service in the Cloud Manager API call
private static final int PORT_FORWARDS_PORT_ORIG = 30001;
private Config config;
@Override
public boolean isAccessible() {
log.debug("MySQL connection URL: [ {} ]", getConnectionUrl(config));
// Establish a connection with the external MySQL service
// This MySQL connection URL is created in getConnection(..) which will use the AEM_PROXY_HOST is it exists, and the proxied port.
try (Connection connection = DriverManager.getConnection(getConnectionUrl(config), config.username(), config.password())) {
// Validate the connection
connection.isValid(1000);
// Close the connection, since this is just a simple connectivity check
connection.close();
// Return true if AEM could reach the external SQL service
return true;
} catch (SQLException e) {
log.error("Unable to establish an connection with MySQL service using connection URL [ {} ]", getConnectionUrl(config), e);
}
return false;
}
/**
* Create a connection string to the MySQL using the AEM-provided AEM_PROXY_HOST and portForwards.portOrg port
* defined in the Cloud Manager API mapping.
*
* @param config OSGi configuration object
* @return the MySQL connection URI
*/
private String getConnectionUrl(Config config) {
return String.format("jdbc:mysql://%s:%d/wknd-examples", PROXY_HOST, PORT_FORWARDS_PORT_ORIG);
}
@Activate
protected void activate(Config config) throws ClassNotFoundException, SQLException {
this.config = config;
// Load the required MySQL Driver class required for Java to make the connection
// The OSGi bundle that contains this driver is deployed via the project's all project
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
}
@ObjectClassDefinition
@interface Config {
@AttributeDefinition(type = AttributeType.STRING)
String username();
@AttributeDefinition(type = AttributeType.STRING)
String password();
}
}