前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud(2)——服务提供者

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

作者头像
会跳舞的机器人
发布2018-09-21 16:11:03
4090
发布2018-09-21 16:11:03
举报

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

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

1、pom.xml加入以下依赖

代码语言:javascript
复制
<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

代码语言:javascript
复制
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,内容如下:

代码语言:javascript
复制
#应用名称
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、编写一个查找用户的服务方法

代码语言:javascript
复制
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
Paste_Image.png

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

Paste_Image.png
Paste_Image.png

附项目的完整代码包结构

Paste_Image.png
Paste_Image.png
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-11-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、pom.xml加入以下依赖
  • 3、在resource文件夹下创建application.properties,内容如下:
  • 4、编写一个查找用户的服务方法
  • 5、运行验证
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档