Eureka服务器搭建
(1)创建cloud-eureka模块,在pom.xml中引入依赖:
<dependencies>
<!--注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
(2)application.yml配置如下:
server:
port: 6868 #服务端口
eureka:
client:
register-with-eureka: false #是否将自己注册到Eureka服务中
fetch-registry: false #是否从Eureka中获取注册信息
service-url: #Eureka客户端与Eureka服务端进行交互的地址
defaultZone: http://127.0.0.1:${server.port}/eureka/
(3)在启动类中加入@EnableEurekaServer注解:
@SpringBootApplication
@EnableEurekaServer
public class CloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(CloudEurekaApplication.class, args);
}
}
启动cloud-eureka模块,打开浏览器访问localhost:6868
Eureka服务器搭建完成。
Eureka客户端注册
将巡查管理模块cloud-patrol注册到Eureka服务中。
(1)在pom.xml中引入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
(2)application.yml中eureka的配置如下:
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka/
instance:
prefer-ip-address: true #模块之间跨域访问
(3)在启动类中加入@EnableEurekaClient注解:
@SpringBootApplication
@EnableEurekaClient
@ComponentScan(basePackages={"com.config"})//引入swagger2
public class CloudPatrolApplication {
public static void main(String[] args) {
SpringApplication.run(CloudPatrolApplication.class, args);
}
}
根据以上步骤将用户管理模块cloud-user也注册到Eureka服务中,然后启动cloud-patrol和cloud-user模块,打开浏览器访问localhost:6868
Eureka服务之间的调用
(1)现巡查管理模块cloud-patrol需要调用用户管理模块cloud-user中的接口,则cloud-patrol模块需要在pom.xml中引入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
即调用方需要引入以上依赖,被调用方不需要引入任何依赖。
(2)在cloud-patrol模块的启动类中加入@EnableDiscoveryClient和@EnableFeignClients注解,采用Feign去发现服务:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@ComponentScan(basePackages={"com.config"})//引入swagger2
public class CloudPatrolApplication {
public static void main(String[] args) {
SpringApplication.run(CloudPatrolApplication.class, args);
}
}
(3)cloud-user模块中根据id查询用户的接口如下:
@RestController
@CrossOrigin
@RequestMapping("/user")
@Api(tags = "用户管理系统", description = "用户管理系统模块相关接口")
public class UserController {
@Autowired
private UserService userService;
/**
* 根据ID查询
* @return
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Result findById(@PathVariable("id") Integer id){
return Result.QuerySuccess(userService.findById(id));
}
}
在cloud-patrol模块中新建接口类UserClient.java,在该类中编写findById接口,将实现在cloud-patrol模块中根据id去查询用户。
代码如下:
@FeignClient("cloud-user")
public interface UserClient {
@RequestMapping(value = "user/{id}", method = RequestMethod.GET)
public Result findById(@PathVariable("id") Integer id);
}
其中,@FeignClient注解用于指定从哪个服务中调用功能,注意里面的名称与被调用的服务名称保持一致,并且不能包含下划线。
@RequestMapping注解用于对被调用的微服务进行地址映射,注意@PathVariable注解一定要指定参数名称,否则出错。
(4)修改cloud-patrol模块中的PatrolController.java
@RestController
@CrossOrigin
@RequestMapping("/patrol")
@Api(tags = "巡查管理系统", description = "巡查管理系统模块相关接口")
public class PatrolController {
@Autowired
private UserClient userClient;
@RequestMapping(value = "user/{id}", method = RequestMethod.GET)
public Result findById(@PathVariable Integer id){
Result result = userClient.findById(id);
return Result.QuerySuccess(result);
}
}
(5)启动服务,调用接口验证是否能够查询到用户信息。