版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/myNameIssls/article/details/81193557
基于SpringCloud完成服务提供者示例
工程示例概述
这个工程是一个微服务实例工程,用来实现向Eureka Server
中注册一个微服务。
实现步骤分析
引入Eureka Server Client依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
创建application.yml文件
server:
port: 8000
spring:
application:
# 用于指定Eureka Server中application的名称
name: microservice-service-provider
eureka:
client:
service-url:
# defaultZone: http://localhost:8761/eureka/
# Eureka Server高可用配置,注意多个节点采用英文逗号连接,且不允许出现其它的特殊字符(例如:空格符)
defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
instance:
# 用于指定Eureka Server中的服务状态名称
instance-id: microservice-service-provider-8000
# 用于标记选中Eureka Server中的服务状态名称时,
# 可以从浏览器左下角看到IP地址
# 默认是false
prefer-ip-address: true
# info 模块用来显示app相关信息,如果不配置info模块,那么点击Eureka Server中的状态链接时,会出现访问异常
# 使用info模块需要在pom.xml中添加spring-boot-starter-actuator依赖
info:
app:
name: microservice-service-provider
GitHub: https://github.com/myNameIssls/springcloud-study
Blog: https://blog.csdn.net/column/details/24459.html
注意:
spring-boot-starter-actuator
的GAV
地址:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
spring-boot-starter-actuator
属于SpringBoot
模块中的依赖,所以也需要引入SpringBoot
依赖,GAV
如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
创建springcloud-service-provider启动类
@SpringBootApplication
@EnableEurekaClient // 服务发现
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
创建服务示例类
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 这个类用于说明
* @author shanglishuai
*
*/
@RestController // @RestController <==> @Controller + @ResponseBody
public class ServiceProviderController {
@Autowired private DiscoveryClient discoveryClient;
@RequestMapping("/provider/service1")
public String service1() {
String str = "这是服务生产者提供的一个服务";
return str;
}
// 查看服务相关信息
@RequestMapping("/service/discovery")
public Object discovery() {
List<ServiceInstance> instances = discoveryClient.getInstances("MICROSERVICE-SERVICE-PROVIDER");
// for (ServiceInstance serviceInstance : instances) {
// System.out.println(serviceInstance.getHost());
// }
List<String> services = discoveryClient.getServices();
return instances;
}
}
测试
启动本示例之前,需要提前启动Eureka Server
高可用工程,即springcloud-eureka-server-peer
。
项目启动以后,可以Eureka Server
中看到这个微服务:
Eureka Server
:http://peer1:8761/eureka/
Eureka Server
:http://peer2:8762/eureka/
源代码链接:
https://github.com/myNameIssls/springcloud-study/tree/master/springcloud-service-provider