SpringCloud从入门到精通教程(二)- 服务提供者
需求背景
服务提供者
Tips技术点
1. @EnableEurekaClient注解
- 表示这是一个Eureka客户端程序
配置application.yml中eureka属性值
- 比如填写Eureka注册中心地址等
代码演示
1. 项目目录结构
2. pom.xml依赖组件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-provider-user</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.minbo.cloud</groupId>
<artifactId>spring-cloud-microservice-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- 添加Eureka的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
</project>
3. application.yml配置文件
server:
port: 8000
spring:
application:
name: microservice-provider-user # 项目名称尽量用小写
eureka:
# 配置开启健康检查
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # 指定注册中心的地址
instance:
# IP地址优先
preferIpAddress: true
# 按需配置续约更新时间和到期时间
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10
注:spring.application.name是注册服务名,非常重要。服务消费者是通过此名称调用该服务提供者
4. 接口类(测试提供的方法)
package com.minbo.cloud.study.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HiController {
@Value("${server.port}")
String port;
// 提供的方法
@RequestMapping("/hi")
public String hi(@RequestParam(value = "name", defaultValue = "minbo") String name) {
String result = "hi " + name + " , i am from port:" + port;
System.out.println(result);
return result;
}
}
5. 启动类
添加@EnableEurekaClient注解
package com.minbo.cloud.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
6. 是否注册成功
查看Eureka界面,可以看到该服务提供者已经成功注册进来了
完整源码下载
我的Github源码地址:
https://github.com/hemin1003/spring-cloud-study/tree/master/spring-cloud-greenwich/microservice-provider-user
上一章教程
SpringCloud从入门到精通教程(一)- 服务的注册与发现(Eureka)
下一章教程
SpringCloud从入门到精通教程(三)- 服务消费者,实现方式一(ribbon)
该系列教程
SpringCloud从入门到精通教程
我的专栏
- SpringBoot系列专栏
- SpringCloud系列专栏
- 高可用高并发实战专栏
- 微服务架构实战
- DevOps实战专栏
- 程序化广告实战专栏
至此,全部介绍就结束了
-———————————————
-———————————————
我的CSDN主页
关于我(个人域名)
我的开源项目集Github
期望和大家一起学习,一起成长,共勉,O(∩_∩)O谢谢
欢迎交流问题,可加个人QQ 469580884,
或者,加我的群号 751925591,一起探讨交流问题
不讲虚的,只做实干家
Talk is cheap,show me the code
还没有评论,来说两句吧...