接上篇:

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. Log4Net 在ASP.NET WebForm 和 MVC的全局配置

    使用log4net可以很方便地为应用添加日志功能.应用Log4net,开发者可以很精确地控制日志信息的输出,减少了多余信息,提高了日志记录性能.同时,通过外部配置文件,用户可以不用重新编译程序就能改变 ...

  2. mfs教程(二)

    mfs文件系统(二) 编译和安装 MooseFS部署的首选方法是从源代码安装 源代码包安装支持标准./configure && make && make install ...

  3. 由于挂载的nfs存储目录掉下线,导致创建VM时,无法创建

    具体错误,如下截图 重新挂载存储后,在创建VM,将成功

  4. 【转】浏览器中F5和CTRL F5的行为区别

    原文地址:http://www.cnblogs.com/jiji262/p/3410518.html 前言 在印象中,浏览器中的F5和刷新按钮是一样的效果,都是对当前页面进行刷新:Ctrl-F5的行为 ...

  5. 为docker设置国内镜像

    docker的默认镜像(https://hub.docker.com/)地址,拉取镜像时是比较慢的,经常会超时,有时拉取几个小时.为了加快拉取的时间和速度,需要添加中国的镜像地址: 国内的加速地址: ...

  6. C#向服务器上传文件问题

    最近在写服务器端web上传的接口.但一直报错,上传不上去,后来发现是在分隔符中出现的问题. 错误的写法: var boundary = "---------------" + Da ...

  7. MSSQL 静态值查询

    有些时候可能会需要查询一些静态值 ), (),()) AS tabName ( c1 ) 演变1.多列 ,), (,),(,)) AS tabName ( c1,c2 ) 演变2.聚合 ), (),( ...

  8. 数独·唯一性技巧(Uniqueness)-1

    唯一性技巧基于这样一个事实——各类出版物上发布的数独题目都只有唯一解.事实上,绝大多数数独玩家有这样的共识:即合格的数独题目解应该是唯一的.因此,为了保证题目合格.有效,出题者在制作题目时,会将一些虽 ...

  9. 13本热门书籍免费送!(Python、SpingBoot、Entity Framework、Ionic、MySQL、深度学习、小程序开发等)

    七月第一周,网易云社区联合清华大学出版社为大家送出13本数据分析以及移动开发的书籍(Python.SpingBoot.Entity Framework.Ionic.MySQL.深度学习.小程序开发等) ...

  10. 「BJOI2012」连连看

    题目链接 戳我 \(Solution\) 我们首先进行拆点操作,将每个点都拆成\(x\)和\(y\),将满足条件的两个点连起来就好了(记得要将\(x\)连\(y'\)的同时要将\(y\)联向\(x'\ ...