接上篇:

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. solrcloud上传collection配置

    建议:上传多个collection的配置文件,建议修改配置文件名字,在solr.xml中按照文件名来引用collection对应的schema和solrconfig.xml文件,这样无论你多个coll ...

  2. 2-3 JAVA内存模型

  3. 【转】LVS负载均衡之session解决方案 持久连接

    原文地址:http://minux.blog.51cto.com/8994862/1744761 1. 持久连接是什么? 1.1 在LVS中,持久连接是为了用来保证当来自同一个用户的请求时能够定位到同 ...

  4. httpd和apache的区别

    今天要配置集成服务器环境 apache + tomcat + php + jsp + mysql  + sqlserver 去下载apache 发现有: apache_2.2.14-win32-x86 ...

  5. WCF项目问题1-找不到类型“WCFService.Service1”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。

    找不到类型“WCFService.Service1”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHosti ...

  6. numpy.loadtxt() 出现codecError_____ Excel 做矩阵乘法

    1) 用 numpy读入csv文件是报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal m ...

  7. The Difference Between @Helpers and @Functions In WebMatrix

    from: http://www.mikesdotnetting.com/article/173/the-difference-between-helpers-and-functions-in-web ...

  8. css总结13:CSS 伪类(Pseudo-classes)

    1 伪类作用:CSS伪类是用来添加一些选择器的特殊效果. 2 常用示例: 2.1anchor伪类:代码:   正常语法: a{color:#FF0000;}/* 文字颜色 */   伪类语法: a:l ...

  9. Asp.net MVC 基于Area的路由映射

    对于一个较大规模的Web应用,我们可以从功能上通过Area将其划分为较小的单元.每个Area相当于一个独立的子系统,具有一套包含Models.Views和Controller在内的目录结构和配置文件. ...

  10. Jquery hover 事件

    hover(over,out)一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法.这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态. 当鼠标移动到一个匹配的元素上面时 ...