说明:
Eureka分为Server端和Client端,Server端与Client端为一对多关系(Server端可配成集群),Server端(类似Zookeeper)提供注册中心功能,Client端用于向Server端注册/调用服务,Client端配置在各微服务Module工程中。Client端本身无法发布为Web微服务,需要和SpringMVC一起使用。
一、导入SpringCloud与SpringBoot依赖包(必须版本对应,此处为2.2.x <-> Hoxton):
1.导入SpringBoot依赖包:
<project>
<!-- 导入spring-boot的2.2.x版本,对应spring-cloud的Hoxton版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.8.RELEASE</version>
</parent>
...
</project>
2.定死SpringCloud依赖包:
<project>
...
<!-- 定死spring-cloud为Hoxton版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
二、Eureka的Server端服务器搭建(类似Zookeeper服务器):
1.导入Eureka的Server端依赖包:
<!-- 导入Eureka的Server端依赖包 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
2.application.yml配置Eureka的Server端信息:
server: #配置此Web微服务端口号
port: 10000
eureka:
server:
enable-self-preservation: false #关闭自我保护,默认为打开
eviction-interval-timer-in-ms: 300000 #失效时间,单位毫秒,微服务超时未发心跳包则被剔除
client:
register-with-eureka: false #是否将自已注册到Eureka服务中(默认,不用注册)
fetch-registry: false #配置是否从Eureka中获取注册信息
service-url: #配置Eureka Server端的URL,此处端口与server.port中一样
defaultZone: http://127.0.0.1:10000/eureka/
3.编写Eureka的Server端启动类:
@SpringBootApplication //配置启动类
@EnableEurekaServer //配置Eureka服务端启动类
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class); //加载启动类
}
}
三、Eureka的Client端搭建(每个微服务Module均配置为Client端):
1.导入Eureka的Client端依赖包和Web功能依赖包:
<dependencies>
<!-- 导入Eureka的Client端依赖包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 导入Web功能依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.application.yml配置Eureka的Client端信息和Web微服务端口号:
spring:
application:
name: mall-usercenter #配置此Web微服务名称
server: #配置此Web微服务端口号
port: 10001
eureka:
client:
fetch-registry: true #开启后会将server端信息缓存本地,30秒获取更新一次
registry-fetch-interval-seconds: 30 #30秒从server端获取一次,上面的为false时不起作用
service-url: #导入Eureka Server配置的地址
defaultZone: http://127.0.0.1:10000/eureka/
instance:
prefer-ip-adress: true #配置支持跨域访问
lease-renewal-interval-in-seconds: 5 #每隔5秒向注册中心发送续约指令
lease-expiration-duration-in-seconds: 30 #服务过期时间为30秒
3.编写Eureka的Client端启动类:
@SpringBootApplication //配置启动类
@EnableEurekaClient //配置Eureka客户端
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class); //加载启动类
}
}
4.提供Web功能的请求处理类:
//提供Web功能的请求拦截类
@Controller
public class UserController {
//请求路径为:http://127.0.0.1:10001/getUserDetail/1001 ,userNo是参数值
@RequestMapping(value = "getUserDetail/{userNo}", method = RequestMethod.GET)
@ResponseBody // @ResponseBody将返回对象转换为json字符串
public User getUserDetail(@PathVariable("userNo") String userNo) {// @PathVariable用于将{userNo}的值取出,赋值给userNo
System.out.println("mall-usercenter微服务内部getUserDetail方法触发 userNo: " + userNo);
User user = new User();
user.setUserNo(userNo);
user.setName("先生");
return user;
}
}
5.使用RestTemplate工具类调用微服务(在其他工程中):
(1)application.yml:
spring:
application:
name: eurekacaller
server: #配置此Web微服务端口号
port: 9898
eureka:
client:
service-url: #导入Eureka Server配置的地址,多个时每个地址用逗号隔开
defaultZone: http://127.0.0.1:10001/eureka/
(2)Application类:
@SpringBootApplication //配置启动类
@EnableDiscoveryClient //发现微服务
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class); //加载启动类
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
(3)Controller类:
@Controller
public class UserAPIController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate; //http请求工具类
@GetMapping("/getUserDetailAPI")
@ResponseBody
public String getUserDetailAPI(String userNo) {
ServiceInstance si = loadBalancerClient.choose("mall-usercenter"); //获取指定微服务
if (si == null) return "此微服务不存在";
String url = String.format("http://%s:%s/getUserDetail/%s", si.getHost(), si.getPort(), userNo); //组成url
return restTemplate.getForObject(url, String.class); //调用GET方法,请求微服务,获取数据
//return restTemplate.postForObject(url, new UserParam(userNo), String.class); //调用POST方法,请求微服务,获取数据
}
}