微服务中Eureka的搭建与使用(二)

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)启动服务,调用接口验证是否能够查询到用户信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值