spring cloud consul整合

本文基于spring cloud Finchley.SR1

consul如何搭建可以看文章consul docker方式搭建

本文章源码位置:https://github.com/wanghongqi/springcloudconsul_test/

目录

服务端

客户端

测试


服务端

pom.xml

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

 

application.yml

server:port: 9201spring:application:name: springtest-servicecloud:consul:host: 192.168.140.128port: 8500discovery:register: truehostname: 10.1.69.72serviceName: ${spring.application.name}port: ${server.port}healthCheckPath: /homehealthCheckInterval: 15stags: urlprefix-/${spring.application.name}instanceId: ${spring.application.name}

 application.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@EnableDiscoveryClient
@RestController
@SpringBootApplication
public class SpringtestServerApplication {@Autowiredprivate DiscoveryClient discoveryClient;public static void main(String[] args) {SpringApplication.run(SpringtestServerApplication.class, args);}/*** 获取所有服务*/@RequestMapping("/services")public Object services() {return discoveryClient.getServices();}@RequestMapping("/home")public String home() {return "Hello World";}@RequestMapping("/test")public String test(String id) {return "test"+id;}
}

客户端

pom.xml

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>

application.yml

server:port: 9202spring:application:name: springtest-clientcloud:consul:host: 192.168.140.128port: 8500discovery:register: false

application.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@EnableDiscoveryClient
@RestController
@SpringBootApplication
public class SpringtestClientApplication {public static void main(String[] args) {SpringApplication.run(SpringtestClientApplication.class, args);}@Autowiredprivate LoadBalancerClient loadBalancer;@Autowiredprivate DiscoveryClient discoveryClient;/*** 从所有服务中选择一个服务(轮询)*/@RequestMapping("/discover")public Object discover() {return loadBalancer.choose("springtest-service").getUri().toString();}/*** 获取所有服务*/@RequestMapping("/services")public Object services() {return discoveryClient.getInstances("springtest-service");}
}

测试

访问服务端的/services和/home

访问客户端的/services和/discover

Feign调用实现

pom.xml

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.yml


server:port: 9203
feign:hystrix:enabled: true
ribbon:ReadTimeout:  30000ConnectTimeout:  15000
hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 10000

bootstrap.yml


spring:application:name: springtest-feigncloud:consul:host: 192.168.140.128port: 8500discovery:instance-id: ${spring.application.name}:${server.port}service-name: ${spring.application.name}config:discovery:enabled: trueservice-id: springcloud-config-serverfail-fast: true

Application.java

@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class SpringtestFeignApplication {public static void main(String[] args) {SpringApplication.run(SpringtestFeignApplication.class, args);}
}

MyFeignClient

@FeignClient(name = "springtest-service",fallback = HystrixFeignFallback.class)
public interface MyFeignClient {//这里是使用feign请求的地址@RequestMapping("/home")String home();@RequestMapping("/test")String test(@RequestParam(value = "id")String id);
}

HystrixFeignFallback  断路器

@Component
public class HystrixFeignFallback implements MyFeignClient {@Overridepublic String home(){return "Hystrix home";}@Overridepublic String test(String id) {return "Hystrix test"+id;}
}

TestController.java

@RestController
public class TestController {@AutowiredMyFeignClient myFeignClient;@RequestMapping("/home")public String home(){return myFeignClient.home();}@RequestMapping("/test")public String test(String id){return myFeignClient.test(id);}
}

测试

/home

/test?id=123

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/417097.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

event android,androidEvent 对 android 事件分发机制的一点个人理解,将事件机制进行了简单化处理,刚方便理解! @codeKK Android开源站...

android 中的事件处理一直以来困扰不少刚刚从事 android 开发的同学&#xff0c;网上也有不少讲解 android 事件分发的文章&#xff0c;然而讲解的都不够简洁&#xff01;现在我将用另一种简洁的方式来讲解 android 事件的分发机制&#xff01;android 的事件分发可以简单的归位…

consul docker方式搭建

目录 获取镜像 运行 集群搭建 官网&#xff1a;https://www.consul.io/ 文档&#xff1a;https://www.consul.io/docs/index.html 获取镜像 docker pull consul:1.3.0 运行 如果已存在dev-consul先移除 docker rm -f dev-consul 创建容器&#xff0c;守护进程方式启动&…

锁屏壁纸开发 Android,Android开发自己的锁屏壁纸

SurfaceView SurfaceHolder MediaPlayer Service BroadcastReceiver KeyguardManager PowerManager思路&#xff1a;启动一个服务&#xff0c;监听灭屏广播&#xff0c;当收到广播的时候&#xff0c;点亮屏幕&#xff0c;禁用锁屏&#xff0c;调用使用SurfaceV…

[转载来之雨松:NGUI研究院之为什么打开界面太慢(十三)]

本文固定链接: http://www.xuanyusong.com/archives/2799转载于:https://www.cnblogs.com/yexiaopeng/p/6591482.html

前端学习(2335):angular之内置结构指令ngif

child.html <p>child4 works!</p><div class"us">我是歌谣</div> <div [ngClass]"currentClass" class"col">我是歌谣</div><div *ngIf"true">aaaa</div> <div *ngIf"false…

centos7 postgresql安装

安装 Install the repository RPM: yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-centos11-11-2.noarch.rpm Install the client packages: yum install postgresql11 Optionally install the server packages: yum install …

android密码可见不可见的光标控制,Android EditText 在设置为输入密码的时候 密码是否可见 光标在最后显示...

释放双眼&#xff0c;带上耳机&#xff0c;听听看~&#xff01;今天在开发登录注册模块的时候&#xff0c;用EditText实现密码的输入&#xff0c;项目需要密码是可以选择可见或者默认的隐藏模式&#xff0c;很简单&#xff0c;但是第一次遇到就记录下来分享给大家。布局文件and…

centos6 postgresql安装

安装 Install the repository RPM: yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-6-x86_64/pgdg-centos11-11-2.noarch.rpm Install the client packages: yum install postgresql11 Optionally install the server packages: yum install…

l2-006 树的遍历

L2-006. 树的遍历 时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越给定一棵二叉树的后序遍历和中序遍历&#xff0c;请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。 输入格式&#xff1a; 输入第一行给出一个正整数N&#xff08;&l…

Ant Design Pro 2.0/umijs站点配置到非站点根目录下处理

1.config/config.js中 export default {中加上如下&#xff1a; history: hash, //采用hash路由&#xff1a;#/xxx的形式 base:./, publicPath:./, &#xff08;会自动将/static/xxx.jpg之类的和umi.js中的a.p"/"及index.html中window.routerBase中该值进行替换…

android添加商品到购物车,Android使用动画动态添加商品进购物车

本文实例为大家分享了Android添加商品进购物车的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下1、首先展示下效果图2、讲一下思路&#xff0c;小球由加号位置运动到购物车位置&#xff0c;首先得获得这两个点在整个屏幕中的坐标&#xff0c;然后分别计算这两个点的横…

字体小于12px解决办法

width: 100px; height: 30px;-webkit-transform: scale(0.5);margin: -7.5px -25px; 转载于:https://www.cnblogs.com/yghgo/p/6593449.html

前端学习(2338):记录解决问题的一次

<!DOCTYPE html> <html><head><meta charset"utf-8" /><title></title><style type"text/css">#f {font-size: 50px;font-family: 幼圆;}#btn {width: 50px;height: 30px;opacity: 0.5;background-color: paleg…

URAL1553 Caves and Tunnels 树链剖分 动态树

URAL1553 维护一棵树&#xff0c;随时修改某个节点的权值&#xff0c;询问&#xff08;x,y&#xff09;路径上权值最大的点。 树是静态的&#xff0c;不过套动态树也能过&#xff0c;时限卡的严就得上树链剖分了。 还是那句话 splay的核心是splay(x) LCT的核心是access(x) 把SP…

zb——中国人发起的编程语言之序章

20181110 大背景 贸易战&#xff0c;java11收费&#xff0c;python go等百家争鸣&#xff0c;但是没有一个是中国人发起创立的语言。 思考 夜来奇想&#xff0c;java不行了还有go&#xff0c;go如果要是不行了呢&#xff1f;python等都不行了呢&#xff1f; 中国人自己发起…

华为5g鸿蒙折叠,华为再次亮剑!5G新旗舰已经确认,折叠屏+升级到鸿蒙2.0,价格过万...

每年华为有两个新机上市高峰期&#xff0c;一个是年初的P系列发布之后&#xff0c;华为Nova系列、荣耀数字系列会跟着发布&#xff1b;另一个则是年底的华为Mate系列发布之后&#xff0c;华为Nova新系列和荣耀V系列也会一起发布。现在已经是9月底&#xff0c;再等不到一个月的时…

docker服务无法启动 神坑

背景环境 centos6 docker1.7 service docker status输出&#xff1a; docker dead but subsys locked 各种找资料 尝试&#xff1a; rm -rf /var/run/docker.* rm -rf /var/lock/subsys/docker &#xff08;备注&#xff1a;如果有需要备份的docker此处千万不要用rm -r…