Spring Cloud(2)——服务提供者

前言: 本文中的注册中心基于Spring Cloud(1)——服务注册中心,请先了解注册中心的相关知识后再阅读本文。

以用户服务为例,创建Maven工程microservice-provider-user

1、pom.xml加入以下依赖

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
      
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、创建应用启动类UserProviderApplication.java

package com.baibei.provider.user;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author: 会跳舞的机器人
 * @email:2268549298@qq.com
 * @date: 17/2/17 上午9:55
 * @description:用户服务提供者
 */
@SpringBootApplication
@EnableDiscoveryClient
public class UserProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserProviderApplication.class, args);
    }
}

开启服务注册功能很简单,只需要在启动类上加@EnableDiscoveryClient注解即可开启

3、在resource文件夹下创建application.properties,内容如下:

#应用名称
spring.application.name=microservice-provider-user
#服务端口
server.port=8003
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

#服务续租频率。默认是30秒,意思是服务提供者需要多久发送一次心跳检测至Eureka Server来确保Eureka Server知道提供者还存活着,
#如果超过指定时间没有发送,Eureka Server则会从服务提供者列表中将其剔除
eureka.instance.lease-renewal-interval-in-seconds=30

#服务失效时间。默认是90秒,也就是如果Eureka Server在90秒内没有接收到来自服务提供者的Renew操作,就会把服务提供者剔除
eureka.instance.leaseExpirationDurationInSeconds=90

4、编写一个查找用户的服务方法

package com.baibei.provider.user.controller;

import com.baibei.provider.user.entity.User;
import com.baibei.provider.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 会跳舞的机器人
 * @email:2268549298@qq.com
 * @date: 17/2/17 上午10:01
 * @description:用户相关rest API
 */
@RestController
@RequestMapping("/user")
public class UserProviderController {
    @Autowired
    private IUserService userService;

    /**
     * 查找用户
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public User findById(@PathVariable Integer id) {
        User user = userService.getUser(id);

        return user;
    }
}

5、运行验证

运行UserProviderApplication的main方法,启动成功后,访问http://localhost:8003/user/5 Paste_Image.png

打开 http://localhost:8761/ Eureka Server注册中心地址,可以看到microservice-provider-user服务已经注册至注册中心 Paste_Image.png



附项目的完整代码包结构 Paste_Image.png

转载于:https://my.oschina.net/dancingRobot/blog/871172

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值