Spring Eureka的使用入门--服务端与客户端
接上篇:
Eureka作为注册中心,连接服务端与客户端;
服务端:
依赖包:
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management' ext {
springCloudVersion = 'Edgware.SR4'
} dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-actuator' compile 'org.springframework.cloud:spring-cloud-starter-eureka'
compile 'org.springframework.cloud:spring-cloud-config-client'
compile 'org.springframework.cloud:spring-cloud-starter-bus-amqp' compile 'org.springframework:springloaded'
compile 'org.springframework.boot:spring-boot-devtools'
} dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class ServerApplication { public static void main(String[] args) {
SpringApplication.run(ServerApplication .class, args);
}
}
配置文件app.yml
server:
port: 1800 eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: server
prefer-ip-address: true info:
app.name: a-server
company.name: www.*.com
核心代码:服务提供者
import java.util.List; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody; @FeignClient(name = "hello", fallbackFactory = HelloFallbackFactory.class)
@ResponseBody
public interface HelloApi { @PostMapping(path = "/api/hello")
public String sayHI(@RequestBody SayHiRequest request); }
服务已接想口形式提供,注册到Eurka注册中心里:
import org.springframework.stereotype.Component;
import feign.hystrix.FallbackFactory; @Component
public class HelloFallbackFactory implements FallbackFactory<HelloApi> { @Override
public HelloApicreate(Throwable cause) {
return new HelloApi() { @Override
public String sayHi(SayHiRequest request) {
// TODO Auto-generated method stub } };
} }
接口实现:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody; @Controller
public class HelloApiImpl implements HelloApi { @Override
public String sayHi(@RequestBody @Validated SayHiRequest request) {
//do somenting
return "";
} }
服务端搭建完成;
微服务架构里,接口一般抽象出来,将接口和接口实现抽离,放到不同的服务里面;
启动服务,当我注册中心htttp://127.0.0.1:8761/eureka 查看服务注册情况;
客户端:
通过注册中心查找服务,进行服务调用;
依赖包:重点是引入接口方提供jar包
apply plugin: 'io.spring.dependency-management'
dependencies {
compile 'org.springframework.cloud:spring-cloud-starter-eureka'
compile 'org.springframework.cloud:spring-cloud-starter-ribbon'
compile 'org.springframework.cloud:spring-cloud-starter-feign'
compile 'org.springframework.cloud:spring-cloud-starter-config'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile(project(':hello-api'))
}
ext {
springCloudVersion = 'Edgware.SR4'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringCloudApplication
@EnableFeignClients
public class ClientApplication { public static void main(String[] args) {
SpringApplication.run(ClientApplication .class, args);
} }
调用服务类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/api/test/")
public class TestRS { @Autowired
private TestService _testService; @RequestMapping(value = "/say", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public ResponseBase<String> test() {
String test = _testService.sayHi();
return new ResponseBase<String>("0", "success", "");
} } @service
public class TestService {
@Autowired
private HelloApi _helloApi; public String sayHi() {
String test = _helloApi.sayHi();
return test;
} }
配置文件app.yml
server:
port: 1668
contextPath: /hello eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka
instance:
prefer-ip-address: true info:
app.name: hello feign:
client:
config:
default:
connectTimeout: 60000
readTimeout: 60000
启动服务,访问注册中心查看是否注册成功;调用接口测试;
Spring Eureka的使用入门--服务端与客户端的更多相关文章
- 【Eureka】服务端和客户端
[Eureka]服务端和客户端 转载:https://www.cnblogs.com/yangchongxing/p/10778357.html Eureka服务端 1.添加依赖 <?xml v ...
- eureka服务端和客户端的简单搭建
本篇博客简单记录一下,eureka 服务端和 客户端的简单搭建. 目标: 1.完成单机 eureka server 和 eureka client 的搭建. 2.完成eureka server 的添加 ...
- Spring Boot 集成 WebSocket 实现服务端推送消息到客户端
假设有这样一个场景:服务端的资源经常在更新,客户端需要尽量及时地了解到这些更新发生后展示给用户,如果是 HTTP 1.1,通常会开启 ajax 请求询问服务端是否有更新,通过定时器反复轮询服务端响应的 ...
- seata服务端和客户端配置(使用nacos进行注册发现,使用mysql进行数据持久化),以及过程中可能会出现的问题与解决方案
seata服务端和客户端配置(使用nacos进行注册发现,使用mysql进行数据持久化),以及过程中可能会出现的问题与解决方案 说明: 之所以只用nacos进行了注册与发现,因为seata使用naco ...
- 使用Apache CXF开发WebServices服务端、客户端
在前一篇的博客中,我使用Xfire1.x来开发了WebServies的服务端. 但是如果你访问Apache的官网,可以看到xfire已经被合并了. 最新的框架叫做CXF. Apache CXF = C ...
- 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表
1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...
- 【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
前言: MQTT广泛应用于工业物联网.智能家居.各类智能制造或各类自动化场景等.MQTT是一个基于客户端-服务器的消息发布/订阅传输协议,在很多受限的环境下,比如说机器与机器通信.机器与物联网通信等. ...
- Java 断点下载(下载续传)服务端及客户端(Android)代码
原文: Java 断点下载(下载续传)服务端及客户端(Android)代码 - Stars-One的杂货小窝 最近在研究断点下载(下载续传)的功能,此功能需要服务端和客户端进行对接编写,本篇也是记录一 ...
- asp.net获取服务端和客户端信息
asp.net获取服务端和客户端信息 获取服务器名:Page.Server.ManchineName获取用户信息:Page.User 获取客户端电脑名:Page.Request.UserHostNam ...
随机推荐
- 值得一做》一道类似于货车运输的题目(BZOJ3732)(easy+)
这是一道模板套模板的题目,只要会LCA和最小生成树就可以做,水题 直接先甩题目 Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条 ...
- 开坑数位dp
[背景] 在10月3日的dp专练中,压轴的第6题是一道数位dp,于是各种懵逼. 为了填上这个留存已久的坑,蒟蒻chty只能开坑数位dp了. [例题一][HDU2089]不要62 题目大意:给你一个区间 ...
- 598. Range Addition II 矩阵的范围叠加
[抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...
- opennebula 发送序列化ID,构造json格式错误
- 面试题:各大公司Java后端开发面试题总结 已看1 背1 有用 链接有必要看看
ThreadLocal(线程变量副本) --整理 Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量. 采用空间换时间,它用于线程间的数据隔离,为每一个 ...
- CSS变量教程
今年三月,微软宣布 Edge 浏览器将支持 CSS 变量. 这个重要的 CSS 新功能,所有主要浏览器已经都支持了.本文全面介绍如何使用它,你会发现原生 CSS 从此变得异常强大. 一.变量的声明 声 ...
- hdu 2519 新生晚会 (求排列组合时容易溢出)
#include<stdio.h> #include<algorithm> using namespace std; void cal(int n,int m) { ; m=m ...
- 使用jQuery完成表单验证
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>注 ...
- Sql语句摘要
1.分批更新数据库 declare @x intset @x=1 while(@x<=51) begin begin tran update UserFavorite set UserFavor ...
- 使用Fiddler进行IOS APP的HTTP抓包
Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获各种智能手机发出的HTTP/HTTPS请求.Fiddler能捕获IOS设备发出的请求,比如IPhone, IPad, MacBook. ...