600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > [Spring-cloud-eureka]使用 gradle spring boot Spring cloud Eureka 搭建服务提供者

[Spring-cloud-eureka]使用 gradle spring boot Spring cloud Eureka 搭建服务提供者

时间:2021-08-26 11:43:04

相关推荐

[Spring-cloud-eureka]使用 gradle  spring boot Spring cloud Eureka 搭建服务提供者

独角兽企业重金招聘Python工程师标准>>>

上篇博客,已经搭建好了一个可用的服务注册中心EurekaServer,下面我们开始搭建一个服务提供者EurekaAService.

1) 用 eclipse 新建一个 gradle 项目 EurekaAService.

2) 配置 build.gradle 文件,然后 gradle build 一次,确保所有的 jar都引用到位。

build.gradle配置内容如下:

buildscript {repositories {jcenter()}dependencies {classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE'}}apply plugin: 'java'apply plugin: 'spring-boot'repositories {jcenter()}dependencies {compile 'org.springframework.cloud:spring-cloud-starter-eureka:1.2.7.RELEASE'compile 'org.springframework.boot:spring-boot-starter-test'compile group: 'com.alibaba', name: 'fastjson', version: '1.2.32'}

3)服务提供者 EurekaAService 实现。

项目目录结构如下:

├── build.gradle├── gradle│ └── wrapper│├── gradle-wrapper.jar│└── gradle-wrapper.properties├── gradlew├── gradlew.bat├── settings.gradle└── src├── main│ ├── java│ │ └── com│ │└── simonton│ │ └── eureka│ │ ├── EurekaAApplication.java│ │ └── controller│ │ └── EurekaAController.java│ └── resources│└── application.yml└── test└── java

EurekaAController 代码如下:

/*** */package com.simonton.eureka.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import com.alibaba.fastjson.JSONObject;/*** @author simonton**/@RestControllerpublic class EurekaAController {@Autowiredprivate DiscoveryClient client;@RequestMapping(value="/serviceA", method=RequestMethod.GET)public String service() {System.out.println(JSONObject.toJSONString(client.getLocalServiceInstance()));return "service A";}@RequestMapping(value="/print", method=RequestMethod.GET)public void print() {System.out.println(" print info.");}@RequestMapping(value="/beforeService", method=RequestMethod.GET)public String beforeService() {return "Ready invoke service? ";}}

EurekaAApplication 代码如下:

/*** */package com.simonton.eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*** @author simonton**/@EnableDiscoveryClient@SpringBootApplicationpublic class EurekaAApplication {public static void main(String[] args) {SpringApplication.run(EurekaAApplication.class, args);}}

@enableDiscoveryClient 用于激活 Eureka 中的 DiscoveryClient,让该 application 拥有与 server 交互的能力。本质上 Eureka client 是通过向 Eureka server 发送 restful 请求来实现服务的注册与服务信息查询的。

application.yml配置如下:

server:port: 8888spring:application:name: a-serviceeureka:client:service-url:defaultZone: http://localhost:9999/eureka

最后 RunEurekaAApplication,启动后,服务 a-service就注册好了。

下面是在服务注册中心查询到的服务注册信息:

通过工具 postman 可以测试该接口,测试验证近限于验证接口,并没有从服务注册中心,下一篇博客将讲述如何实现一个服务消费者,服务消费者才是从注册中心获取查询服务,然后消费。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。