Home > Back-end >  Spring in the cloud service registration is successful, but cannot call by service name IP address
Spring in the cloud service registration is successful, but cannot call by service name IP address

Time:09-19

Recently in learning a spring cloud, according to the online tutorials, hello - service service has been set up but invoked by service consumers showed No instances available for hello - service

But the service registration has been successful

Why is this? I direct call service IP address can get return values

The service provider's controller classes and entry is as follows:
 @ RestController 
Public class HelloController {
Private final Logger Logger=Logger. GetLogger (getClass ());
The @autowired
Private DiscoveryClient client;

@ RequestMapping (value="https://bbs.csdn.net/hello", method=RequestMethod. GET)
Public String index () {
List The instances=client. GetInstances (" hello - service ");
for (int i=0; I & lt; The instances. The size (); I++) {
Logger. The info ("/hello, host: "+ instances. The get (I). The getHost () +", service_id: "+ instances. The get (I). GetServiceId ());
}
Return "Hello World".
}
}

 @ EnableDiscoveryClient 
@ SpringBootApplication
Public class ProviderApplication {

Public static void main (String [] args) {
SpringApplication. Run (ProviderApplication. Class, args);
}
}


The service consumer controller classes and entry:
 @ RestController 
Public class ConsumerController {
The @autowired
RestTemplate RestTemplate;
@ RequestMapping (value="https://bbs.csdn.net/ribbon-consumer", method=RequestMethod. GET)
Public String helloController () {
Return restTemplate. GetForEntity (" http://HELLO-SERVICE/hello ", String class). The getBody ();
}
}

 @ SpringBootApplication 
@ EnableDiscoveryClient
Public class RibbonConsumerApplication {

Public static void main (String [] args) {
SpringApplication. Run (RibbonConsumerApplication. Class, args);
}
@ LoadBalanced
@ Bean
RestTemplate RestTemplate () {
Return new RestTemplate ();
}
}

CodePudding user response:

The consumer end
Eureka configuration is correct?

CodePudding user response:

Because the value of your @ RequestMapping hello

CodePudding user response:

I also met before, is the same as mine? https://blog.csdn.net/a729913162/article/details/81123078#comments

CodePudding user response:

Eureka. The instance. The hostname configuration is there, any deleted try again

CodePudding user response:

I also met, just to solve in the consumer end write spring. When accessing a service application. The name of the name of external exposure

CodePudding user response:

 
Eureka:
Client:
Service - url:
DefaultZone: http://localhost:7001/eureka
# defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/, http://eureka7003.com:7003/eureka/
Instance:
The instance - id: jih - manage
-- - IP address: true
Server:
Enable - self - preservation: false
this is the publisher of eureka configuration, the source code at https://github.com/qpdyg/jih

CodePudding user response:

There is something wrong with the RestTemplate injection
The needs of the new injection:
@ Bean
@ LoadBalanced
RestOperations restTemplate (RestTemplateBuilder builder) {
Return builder. The build ();
}

The @autowired
RestOperations restTemplate;

CodePudding user response:

12th floor is a positive solution, for many days

CodePudding user response:

Excuse me, can you solve it, you and I appeared the same problem, 13 said 12 floor is a positive solution, can you try it, I don't know public RestOperations restTemplate (RestTemplateBuilder builder) {
Return builder. The build ();
}
What is RestTemplateBuilder this parameter

CodePudding user response:

Why not feign brother

CodePudding user response:

The
refer to 12 floor bin brother reply:
RestTemplate injection has a problem
The needs of the new injection:
@ Bean
@ LoadBalanced
RestOperations restTemplate (RestTemplateBuilder builder) {
Return builder. The build ();
}

The @autowired
RestOperations restTemplate;

Bosses, what is your version? I have used this way, you still have a problem, I wonder could it be version of the problem?

CodePudding user response:

https://blog.csdn.net/o_darling/article/details/82147350

CodePudding user response:

reference 16 floor changerzhuo response:
Quote: refer to 12 floor bin brother reply:

There is something wrong with the RestTemplate injection
The needs of the new injection:
@ Bean
@ LoadBalanced
RestOperations restTemplate (RestTemplateBuilder builder) {
Return builder. The build ();
}

The @autowired
RestOperations restTemplate;

Bosses, what is your version? I have used this way, you still have a problem, I wonder could it be version of the problem?



SpringBoot 2.0.4 version. RELEASE
SpringCloud version Finchley. SR1
The injection method can also be abbreviated:

@ Bean
@ LoadBalanced
Public RestTemplate RestTemplate (RestTemplateBuilder builder) {
Return builder. The build ();
}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related