|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2020 "Neo4j," |
| 3 | + * Neo4j Sweden AB [https://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.doc.driver.springframework.boot.dedicated_routing_driver; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.HashSet; |
| 23 | + |
| 24 | +import org.neo4j.driver.AuthToken; |
| 25 | +import org.neo4j.driver.Config; |
| 26 | +import org.neo4j.driver.Driver; |
| 27 | +import org.neo4j.driver.GraphDatabase; |
| 28 | +import org.neo4j.driver.net.ServerAddress; |
| 29 | +import org.neo4j.driver.springframework.boot.autoconfigure.Neo4jDriverProperties; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.context.annotation.Profile; |
| 33 | + |
| 34 | +/** |
| 35 | + * This is a setup for the solution proposeed in the official driver docs: |
| 36 | + * <a href="https://neo4j.com/docs/driver-manual/current/client-applications/#driver-resolver-function">2.2.1.3. Resolver function</a> |
| 37 | + */ |
| 38 | +// This is just here to prevent both this configuration and RoutingDriverConfiguration to run in parallel. |
| 39 | +@Profile("custom-resolver-function") |
| 40 | + |
| 41 | +@Configuration |
| 42 | +public class DriverWithCustomResolverFunctionConfiguration { |
| 43 | + |
| 44 | + @Bean |
| 45 | + public Driver neo4jDriver(Neo4jDriverProperties neo4jDriverProperties) { |
| 46 | + |
| 47 | + Config config = Config.builder() |
| 48 | + .withResolver(address -> { |
| 49 | + if ("datacenter1".equals(address.host())) { |
| 50 | + return new HashSet<>( |
| 51 | + Arrays.asList(ServerAddress.of("dc1-core1", 7687), ServerAddress.of("dc1-core2", 7687))); |
| 52 | + } else { |
| 53 | + return new HashSet<>(Arrays.asList(ServerAddress.of("other-core", 7687))); |
| 54 | + } |
| 55 | + }).build(); |
| 56 | + AuthToken authToken = neo4jDriverProperties.getAuthToken(); |
| 57 | + |
| 58 | + return GraphDatabase.driver(neo4jDriverProperties.getUri(), authToken, config); |
| 59 | + } |
| 60 | +} |
0 commit comments