注意:这个章节,请结合前几章节一起使用,因为其要调用上一章节的服务
1、pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.test eureka-client-movie 0.0.1-SNAPSHOT eureka-client-movie Demo project for Spring Boot 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
2、添加application.yml配置
server: port: 8664user: userServicePath: http://localhost:8663/user/spring: application: name: eureka-client-movieeureka: instance: hostname: localhost prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} client: serviceUrl: defaultZone: http://${eureka.instance.hostname}:8661/eureka
3、在启动类添加注解
package com.test.eurekaclientmovie;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableEurekaClientpublic class EurekaClientMovieApplication { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(EurekaClientMovieApplication.class, args); }}
4、MovieController.java
package com.test.eurekaclientmovie.controller;import com.test.eurekaclientmovie.entity.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class MovieController { @Autowired private RestTemplate restTemplate; @Value("${user.userServicePath}") private String userServicePath; /* @Autowired private LoadBalancerClient loadBalancerClient;*/ @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { //请用虚拟ip return restTemplate.getForObject(this.userServicePath+id, User.class); } /** * 配置单一的方法进行轮询 * @return *//* @GetMapping("/test") public String test() { //请用虚拟ip ServiceInstance serviceInstance =loadBalancerClient.choose("eureka-client-user"); System.out.println(serviceInstance.getHost()+":"+serviceInstance.getPort()); return "1"; }*/}
5、User.java
package com.test.eurekaclientmovie.entity;import java.io.Serializable;import java.math.BigDecimal;public class User implements Serializable { private Long id; private String name; private String username; private BigDecimal balance; private short age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public BigDecimal getBalance() { return balance; } public void setBalance(BigDecimal balance) { this.balance = balance; } public short getAge() { return age; } public void setAge(short age) { this.age = age; }}
6、实现服务之间的调用,如果访问下面URL有返回值,说明实现了服务之间的调用了,没有实现,请仔细检查配置,也可以留言
http://localhost:8664/movie/1
7、查看eureka-server的情况
http://localhost:8661/
如下图所示