如何配置Eureka注册中心替代RestTemplate直接调用微服务?
Got it, let's fix your UI-service to use Eureka for service discovery instead of that hardcoded localhost:8093 URL. Here's a step-by-step guide to make this work smoothly:
1. Add Eureka Client Dependencies
First, you need to include the Spring Cloud Eureka Client dependency in your UI-service's build file.
For Maven (add to pom.xml):
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
For Gradle (add to build.gradle):
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
2. Configure Eureka Client Settings
Next, tell your UI-service where the Eureka server is located, and set a service name for itself so it can register with Eureka. Add these properties to your application.properties or application.yml:
application.properties
# Eureka server URL eureka.client.service-url.defaultZone=http://your-eureka-server-host:port/eureka/ # UI-service's own name (will show up in Eureka dashboard) spring.application.name=UI-SERVICE
application.yml
eureka: client: service-url: defaultZone: http://your-eureka-server-host:port/eureka/ spring: application: name: UI-SERVICE
Make sure your Login-service is also registered with the same Eureka server, and its spring.application.name is set to something like LOGIN-SERVICE (we’ll use this name later).
3. Create a Load-Balanced RestTemplate
To let RestTemplate resolve service names from Eureka, configure it with the @LoadBalanced annotation. Create a @Bean for RestTemplate in your configuration class or main application class:
@Configuration public class RestTemplateConfig { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
The @LoadBalanced annotation integrates RestTemplate with Ribbon (Spring’s load balancer), which lets it look up service instances from Eureka’s registry automatically.
4. Update Your Controller Code
Now replace the hardcoded URL with the service name of your Login-service. Instead of http://localhost:8093/accounts/login, use http://LOGIN-SERVICE/accounts/login (replace LOGIN-SERVICE with whatever name you set for Login-service in its spring.application.name).
Here’s your updated controller method (note we’re injecting the RestTemplate instead of creating a new one each time):
@RestController public class YourController { @Autowired private RestTemplate restTemplate; @RequestMapping("/log") public String abc(HttpServletRequest request) { // Use Eureka-registered service name instead of hardcoded URL final String uri = "http://LOGIN-SERVICE/accounts/login"; String result = restTemplate.getForObject(uri, String.class); return result; } }
5. Enable Eureka Client on Your Application
Add the @EnableEurekaClient (or @EnableDiscoveryClient, which works for multiple service discovery tools) annotation to your UI-service’s main application class:
@SpringBootApplication @EnableEurekaClient public class UiServiceApplication { public static void main(String[] args) { SpringApplication.run(UiServiceApplication.class, args); } }
Final Checks
- Confirm your Eureka server is running and accessible.
- Verify both UI-service and Login-service appear in the Eureka dashboard (usually at
http://eureka-host:port/). - Test the
/logendpoint—your UI-service will now automatically find Login-service instances from Eureka, no hardcoded URLs required!
内容的提问来源于stack exchange,提问作者Manish Goyal




