SpringCloud 简单案例

一、服务注册与发现

这里会用到Spring Cloud Netflix,该项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。

这里的核心内容是服务发现模块:Eureka

创建“服务注册中心”

1.创建基于web的Maven项目(springcloud)

2.创建服务注册中心。在springcloud项目中创建SpringBoot项目(springboot):

image.png

image.png

勾选Cloud Discovery–>Eureka server。以方便导包

image.png

3编写springboot项目

3.1查看pom.xml文件

<?xml version="1.0"encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.1.3.RELEASE<!-- lookup parent from repository -->com.handspringclouddemo0.0.1-SNAPSHOTspringclouddemoDemo project for Spring Boot1.8Greenwich.RELEASEorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-pluginspring-milestonesSpring Milestoneshttps://repo.spring.io/milestone

4在启动类上加上注解 如下

通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。这一步非常的简单,只需要在一个普通的Spring Boot应用中添加这个注解就能开启此功能,比如下面的例子:

packagecom.hand;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublicclassSpringclouddemoApplication{publicstaticvoidmain(String[] args){        SpringApplication.run(SpringclouddemoApplication.class, args);    }}

5 修改application.yml文件

yml文件的好处,天然的树状结构,一目了然

---#端口号server:  port: 8760eureka:  instance:    hostname: localhost  client:# eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。# 由于当前这个应用就是Eureka Server,故而设为falseregister-with-eureka:false# eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,# 不需要同步其他的Eureka Server节点的数据,故而设为false。fetch-registry:falseservice-url:# eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,#查询服务和注册服务都需要依赖这个地址。默认是defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

6 启动项目后访问

http://localhost:8760

可以看到下面的页面,其中还没有发现任何服务:

image.png

7.搭建服务端(生产者)

创建springBoot项目同上

查看pom.xml文件

<?xml version="1.0"encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.1.3.RELEASE<!-- lookup parent from repository -->com.handproducer0.0.1-SNAPSHOTproducerDemo project for Spring Boot1.8Greenwich.RELEASEorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-pluginspring-milestonesSpring Milestoneshttps://repo.spring.io/milestone

8修改application.yml文件

注:端口不能与上面的相同。这里的服务name:service-hi 可以根据自己情况定义。

---server:  port:8762eureka:  client:    service-url:      defaultZone: http://localhost:8760/eureka/spring:  application:    name: service-producer

9 编写启动类

packagecom.hand.producer;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;@SpringBootApplication@EnableEurekaClient@RestControllerpublicclassProducerApplication{publicstaticvoidmain(String[] args){        SpringApplication.run(ProducerApplication.class, args);    }@Value("${server.port}")    String port;@RequestMapping("/hi")publicStringhome(@RequestParam String name){return"hi "+ name +",i am from port:"+ port;    }}

运行服务

http://localhost:8761/hi?name=xiong.zhang@hand-china.com

image.png

然后查看http://localhost:8760

可以看到,我们定义的服务被注册了。如下图所示:

image.png

9.创建消费者

9.1 创建消费者modul,流程如上述工程创建流程。

9.2查看pom.xml文件

<?xml version="1.0"encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.1.3.RELEASE<!-- lookup parent from repository -->com.handcustomer0.0.1-SNAPSHOTcustomerDemo project for Spring Boot1.8Greenwich.RELEASEorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-pluginspring-milestonesSpring Milestoneshttps://repo.spring.io/milestone

10 yml配置

---server:  port:8763eureka:  client:    service-url:      defaultZone: http://localhost:8760/eureka/spring:  application:    name: service-customerfeign:  hystrix:    enabled :true

11 编写启动类

@EnableDiscoveryClient表明标注类是消费者,加入restTemplate以消费相关的服务

packagecom.hand.customer;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;importorg.springframework.cloud.client.loadbalancer.LoadBalanced;importorg.springframework.context.annotation.Bean;importorg.springframework.web.client.RestTemplate;@SpringBootApplication@EnableDiscoveryClientpublicclassCustomerApplication{publicstaticvoidmain(String[] args){        SpringApplication.run(CustomerApplication.class, args);    }@Bean@LoadBalancedRestTemplaterestTemplate(){returnnewRestTemplate();    }}

12 .创建service和controller

12.1 service层

packagecom.hand.customer.controller;importcom.hand.customer.service.HelloService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;packagecom.hand.customer.service;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.web.client.RestTemplate;/**

* Created with IntelliJ IDEA.

* Author: Patrick

* Date: 2019/2/25

* Time: 23:12

* Description:

*/@ServicepublicclassHelloService{@AutowiredRestTemplate restTemplate;publicStringhiService(String name){returnrestTemplate.getForObject("http://SERVICE-PRODUCER/hi?name="+ name, String.class);    }}

12.2 controller层

packagecom.hand.customer.controller;importcom.hand.customer.service.HelloService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;/**

* Created with IntelliJ IDEA.

* Author: Patrick

* Date: 2019/2/25

* Time: 23:38

* Description:

*/@RestControllerpublic class HelloControler {    @AutowiredHelloService helloService;    @RequestMapping(value="/hi")    public String hi(@RequestParam String name) {returnhelloService.hiService(name);    }}

再次查看服务

image.png

在浏览器中输入http://localhost:8763/hi?name=admin

作者:佛祖0

链接:

简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355