接上篇:

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的使用入门--服务端与客户端的更多相关文章

  1. 【Eureka】服务端和客户端

    [Eureka]服务端和客户端 转载:https://www.cnblogs.com/yangchongxing/p/10778357.html Eureka服务端 1.添加依赖 <?xml v ...

  2. eureka服务端和客户端的简单搭建

    本篇博客简单记录一下,eureka 服务端和 客户端的简单搭建. 目标: 1.完成单机 eureka server 和 eureka client 的搭建. 2.完成eureka server 的添加 ...

  3. Spring Boot 集成 WebSocket 实现服务端推送消息到客户端

    假设有这样一个场景:服务端的资源经常在更新,客户端需要尽量及时地了解到这些更新发生后展示给用户,如果是 HTTP 1.1,通常会开启 ajax 请求询问服务端是否有更新,通过定时器反复轮询服务端响应的 ...

  4. seata服务端和客户端配置(使用nacos进行注册发现,使用mysql进行数据持久化),以及过程中可能会出现的问题与解决方案

    seata服务端和客户端配置(使用nacos进行注册发现,使用mysql进行数据持久化),以及过程中可能会出现的问题与解决方案 说明: 之所以只用nacos进行了注册与发现,因为seata使用naco ...

  5. 使用Apache CXF开发WebServices服务端、客户端

    在前一篇的博客中,我使用Xfire1.x来开发了WebServies的服务端. 但是如果你访问Apache的官网,可以看到xfire已经被合并了. 最新的框架叫做CXF. Apache CXF = C ...

  6. 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表

    1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...

  7. 【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示

    前言: MQTT广泛应用于工业物联网.智能家居.各类智能制造或各类自动化场景等.MQTT是一个基于客户端-服务器的消息发布/订阅传输协议,在很多受限的环境下,比如说机器与机器通信.机器与物联网通信等. ...

  8. Java 断点下载(下载续传)服务端及客户端(Android)代码

    原文: Java 断点下载(下载续传)服务端及客户端(Android)代码 - Stars-One的杂货小窝 最近在研究断点下载(下载续传)的功能,此功能需要服务端和客户端进行对接编写,本篇也是记录一 ...

  9. asp.net获取服务端和客户端信息

    asp.net获取服务端和客户端信息 获取服务器名:Page.Server.ManchineName获取用户信息:Page.User 获取客户端电脑名:Page.Request.UserHostNam ...

随机推荐

  1. mysql主从延迟

    1. MySQL数据库主从同步延迟原理.要说延时原理,得从mysql的数据库主从复制原理说起,mysql的主从复制都是单线程的操作,主 库对所有DDL和DML产生binlog,binlog是顺序写,所 ...

  2. mysql外键约束总结

    总结三种MySQL外键约束方式 如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表.外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是 ...

  3. Base -快捷键|通配符|特殊符号|输出(正确与错误的保存)

    curl + a   移动光标到行首. curl +e    移动光标到行尾. curl +k    剪切光标所在位置到行末的字符. curl+u    剪切光标所在位置到行首的字符. curl +y ...

  4. 2014蓝桥杯B组初赛试题《李白打酒》

    题目描述: 话说大诗人李白,一生好饮.幸好他从不开车.     一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒.     逢店加一倍,遇花喝一斗.     这一路上 ...

  5. PCL点云库中的坐标系(CoordinateSystem)

    博客转载自:https://blog.csdn.net/qq_33624918/article/details/80488590 引言 世上本没有坐标系,用的人多了,便定义了坐标系统用来定位.地理坐标 ...

  6. CompositePattern(23种设计模式之一)

    设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒置原则 设计模式六大原则(4):接口隔离原则 设计模式六大原则(5):迪米特法则 设计模式六大 ...

  7. myeclipse自动化提示

    jsp自动提示:1.快捷键提示代码 window-->Preferences的General-->Keys下修改Content Assist的快捷键为Alt+/,这样就可以通过快捷键得到提 ...

  8. C/C++预处理指令常见的预处理指令

    C/C++预处理指令常见的预处理指令如下: #空指令,无任何效果 #include包含一个源代码文件 #define定义宏 #undef取消已定义的宏 #if如果给定条件为真,则编译下面代码 #ifd ...

  9. (转)使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享

    原文地址:http://www.cnblogs.com/huyong/archive/2013/09/24/3334848.html 使用Jquery+EasyUI 进行框架项目开发案例讲解之一 员工 ...

  10. 编写高质量代码改善C#程序的157个建议——建议5: 使用int?来确保值类型也可以为null

    建议5: 使用int?来确保值类型也可以为null 基元类型为什么需要为null?考虑两个场景: 1)数据库中一个int字段可以被设置为null.在C#中,值被取出来后,为了将它赋值给int类型,不得 ...