Bonjour à tous
je suis complètement nouveau sur Spring et j'ai un exemple basique que je n'arrive pas à faire fonctionner.
C'est d'ailleurs probablement un truc très simple
Pour le contexte, j'ai plusieurs services qui s'enregistrent correctement sur Eureka. L'exemple que je cherche à faire fonctionner doit appeler ces services.
Je cherche donc à demander à Eureka où se trouve mon service
J'ai un bout de code qui fonctionne parfaitement et fait exactement ce que je veux
mais DiscoveryManager.getInstance().getDiscoveryClient(); est deprecated ... ce qui commence assez mal
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 EurekaClient eurekaClient = DiscoveryManager.getInstance().getDiscoveryClient(); Application application = eurekaClient.getApplication(service); InstanceInfo instanceInfo = application.getInstances().get(0); String hostname = instanceInfo.getHostName(); int port = instanceInfo.getPort();
j'essaie de remplacer ça par
mais mon eurekaClient reste désespérément vide ... (je ne souhaite pas utiliser feign pour l'instant)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 @Autowired public EurekaClient eurekaClient;
je problème étant peut être un truc Spring de base
Mon code ressemble à ça (je met les pom car je nage un peu dans les dépendances spring)
un pom parent avec
Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 <dependency><groupId>org.test</groupId><artifactId>test</artifactId><version>${project.version}</version></dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.2.6.RELEASE</version><type>pom</type><scope>import</scope></dependency> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR3</version><type>pom</type><scope>import</scope></dependency> <!-- spring boot fail if this is not added --> <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.2</version></dependency>
un pom projet avec
Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 <dependencies> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency> <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId></dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> </dependencies>
une class Main avec
Toute indication serait la bien venue
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 @SpringBootApplication @EnableEurekaClient @ComponentScan public class Main { public static void main(String[] args) throws Exception { SpringApplication.run(Main.class, args); while(true) { try { // just in case of, wait for a few seconds for all to be initialised Thread.sleep(10000); Session s = new Session(); System.out.println(s.getBaseUrl("auth")); } catch(Exception e) { System.out.println("failed "+e.getMessage()); } } } } et une classe Session avec @EnableEurekaClient @Service public class Session { // this is never populated @Autowired public EurekaClient eurekaClient; public String getBaseUrl(String service) { // this WORKS perfectly .... but is deprecated EurekaClient eurekaClient = DiscoveryManager.getInstance().getDiscoveryClient(); Application application = eurekaClient.getApplication(service); InstanceInfo instanceInfo = application.getInstances().get(0); String hostname = instanceInfo.getHostName(); int port = instanceInfo.getPort(); // return service URL return "http://"+hostname+":"+port + "/"; } }
Merci d'avance !
Partager