一、创建Maven主工程Cloud(与一般Maven工程无异)
二、创建服务注册中心子模块 registrycenter,选择Spring Initializr,Next
Next,选择依赖的类库Eureka Server,Spring Boot版本选择最新,Next,Finish
模块创建完成,在主类上新增注解@EnableEurekaServer
在application.properties文件中增加配置信息
#注册中心服务ID
spring.application.name=eureka-server
#端口号
server.port=1111
eureka.instance.hostname=127.0.0.1
#是否将自己注册到Eureka服务中心,默认为true
eureka.client.register-with-eureka=false
#是否从Eureka服务中心获取注册信息
eureka.client.fetch-registry=false
#设置与Eureka服务中心交互地址,查询和注册服务都需要依赖这个地址
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
运行application主类,访问http://l27.0.0.1:1111/
Eureka页面显示正常,注册中心创建成功。
三、创建服务模块MyServiceServer,在注册中心进行注册,选择Spring Initializr,Next
Next,选择类库Eureka Discovery和SpringBoot版本,Next,Finish
模块创建完成,在启动类上添加注解@EnableDiscoveryClient,声明这是一个eureka client,否则不会进行服务注册
在application.properties配置文件增加配置信息
#服务名称
spring.application.name=eureka-my-service
#端口号
server.port=2222
#在注册中心进行注册
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/
运行项目失败,在控制台发现以下信息:
Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaInstanceConfigBean': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
因为Eureka client不包含tomcat依赖,spring容器无法创建一些实例,导致项目无法启动,需要在pom文件里面加上依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
启动项目成功,再次访问 http://127.0.0.1:1111/
页面上可以看到eureka-my-service服务,表示服务注册成功
在MyServiceServer模块中,新增业务类DispatcherController:
package com.tansky.cloud.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DispatcherController {
@RequestMapping("/test")
public String test(){
return "my service";
}
}
重启项目后访问http://127.0.0.1:2222/test,页面显示my service表示成功
四、创建客户端模块MyServiceClient,选择Spring Initializr,Next
Next,选择依赖的类库Eureka Discovery和Feign,Next,Finish
项目创建完成,在启动类上增加注解@EnableDiscoveryClient和@EnableFeignClients,声明可以注册和进行远程服务调用
在application.properties配置文件中增加配置:
spring.application.name=eureka-my-client
server.port=3333
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/
同样不要忘记在pom文件里面加上依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在MyServiceClient模块中,添加服务访问类MyClient
package com.tansky.cloud.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient("eureka-my-service")
public interface MyClient {
@GetMapping("/test")
String test();
}
@FeignClient(“eureka-my-service”)括号中的字符串要与在注册中心注册的名称一致,都是eureka-my-service
在MyServiceClient项目中,添加业务类DispatcherController调用服务访问类MyClient:
package com.tansky.cloud.controller;
import com.tansky.cloud.client.MyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DispatcherController {
@Autowired
private MyClient myClient;
@RequestMapping("/test")
public String test(){
return myClient.test();
}
}
运行项目,访问http://127.0.0.1:3333/test,页面显示my service,表示客户端访问服务成功,实现远程服务调用