SpringBoot36-分布式系统开发-spring cloud2

来源:互联网 发布:哑铃品牌知乎 编辑:程序博客网 时间:2024/06/14 05:22

     接着上一篇博客代码继续

一,界面模块-ui

本模块是一个网关模块

1,依赖

    本模块会使用ribbon,feign,zuul,以及CircuitBreaker,所以需要添加如下的依赖,如下:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jack</groupId><artifactId>ui</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>ui</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.8.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter</artifactId><version>1.2.4.RELEASE</version></dependency><!--<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId><version>1.3.3.RELEASE</version></dependency>--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.3.5.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId><version>1.3.5.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zuul --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId><version>1.3.5.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId><version>1.3.5.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId><version>1.3.5.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>


2,关键代码

1)入口

package com.jack.ui;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;import org.springframework.cloud.netflix.zuul.EnableZuulServer;import org.springframework.context.annotation.Bean;import org.springframework.http.client.ClientHttpRequestFactory;import org.springframework.http.client.SimpleClientHttpRequestFactory;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableEurekaClient//通过@EnableFeignClients开启feign客户端支持@EnableFeignClients//通过@EnableCircuitBreaker开启CircuitBreaker的支持@EnableCircuitBreaker//通过@EnableZuulServer开启网关代理的支持@EnableZuulProxypublic class UiApplication {public static void main(String[] args) {SpringApplication.run(UiApplication.class, args);}@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory factory){return new RestTemplate(factory);}@Beanpublic ClientHttpRequestFactory simpleClientHttpRequestFactory(){SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();factory.setReadTimeout(5000);//msfactory.setConnectTimeout(15000);//msreturn factory;}}

2)使用feign调用person service

package com.jack.ui.service;import com.jack.ui.entity.Person;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.http.MediaType;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 java.util.List;/** * create by jack 2017/11/11 */@FeignClient(name = "person")public interface PersonService {    @RequestMapping(value = "/save",method = RequestMethod.POST,    produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)    @ResponseBody    List<Person> save(@RequestBody String name);}

3)调用person service的断路器

package com.jack.ui.service;import com.jack.ui.entity.Person;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.ArrayList;import java.util.List;/** * create by jack 2017/11/11 */@Servicepublic class PersonHystrixService {    @Autowired    PersonService personService;    /**     * 使用@HystrixCommand的fallbackMethod参数指定,当本地方法调用失败时,调用后备方法fallbackMethod     * @param name     * @return     */    @HystrixCommand(fallbackMethod = "fallbackSave")    public List<Person> save(String name){        return personService.save(name);    }    /**     * 下面的fallbackSave方法需要参数,参数的个数和类型需要和save方法一样     * @param name     * @return     */    public List<Person> fallbackSave(String name){        List<Person> list = new ArrayList<>();        Person p = new Person("Person Service故障");        list.add(p);        return list;    }}


4)使用ribbon调用some service,并使用断路器

package com.jack.ui.service;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;/** * create by jack 2017/11/11 */@Servicepublic class SomeHystrixService {    @Autowired    RestTemplate restTemplate;    @HystrixCommand(fallbackMethod = "fallbackSome")    public String getSome(){        return restTemplate.getForObject("http://localhost:some/getsome", String.class);    }    public String fallbackSome() {        return "some service 故障";    }}

5)person类

package com.jack.ui.entity;/** * create by jack 2017/10/3 *///@Entity注解指明这是一个和数据库表映射的实体类public class Person {    private Integer id;    /**     * 姓名     */    private String name;    /**     * 年龄     */    private Integer age;    /**     * 地址     */    private String address;    public Person() {        super();    }    public Person(String name) {        this.name = name;    }    public Person(Integer id, String name, Integer age, String address) {        super();        this.id = id;        this.name = name;        this.age = age;        this.address = address;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }}


6)测试调用服务的controller

package com.jack.ui.controller;import com.jack.ui.entity.Person;import com.jack.ui.service.PersonHystrixService;import com.jack.ui.service.SomeHystrixService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;/** * create by jack 2017/11/12 */@RestControllerpublic class PersonController {    @Autowired    PersonHystrixService personHystrixService;    @Autowired    SomeHystrixService someHystrixService;    @RequestMapping(value = "/save")    public List<Person> save(String name){        return personHystrixService.save(name);    }    @RequestMapping(value = "/getsome")    public String getSome(){        return someHystrixService.getSome();    }}




3,配置

bootstrap.yml


spring:  application:    name: uieureka:  instance:    non-secure-port: ${server.port:80}  client:    service-url:      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/


application.yml

server:  port: 80

以后通过网关访问其他的服务接口可以是这样:http:网关ip:网关端口/服务名/rest的url


二,断路监控-monitor(dashboard)

创建一个monitor名的spring boot项目

1,依赖

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jack</groupId><artifactId>monitor</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>monitor</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.8.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter</artifactId><version>1.2.4.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix-dashboard</artifactId><version>1.3.5.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-turbine --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-turbine</artifactId><version>1.3.5.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2,主要代码

package com.jack.monitor;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.cloud.netflix.turbine.EnableTurbine;@SpringBootApplication@EnableEurekaClient@EnableHystrixDashboard@EnableTurbinepublic class MonitorApplication {public static void main(String[] args) {SpringApplication.run(MonitorApplication.class, args);}}

3,配置

bootstrap.yml

spring:  application:    name: monitoreureka:  instance:    non-secure-port: ${server.port:8989}  client:    service-url:      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/

application.yml

server:  port: 8989

三,运行

      我们依次启动DiscoveryApplication,ConfigApplication,后面的所有的微服务启动顺序任意,最后期待MonitorApplication。此时访问http://localhost:8761/,查看Eureka server如下:




1,访问ui服务测试

     通过网关的测试controller,调用person的服务http://localhost/save?name=jack,结果如下:


  


    调用some服务,http://localhost/getsome,结果如下:





通过网关访问some的接口:http://localhost/some/getsome,注意这里和上面的接口区别是加了一个some,返回值结果和上面一样。


2,断路器

     此时停止person service和some servie,观察断路器的效果,分别如下:






三,断路器监控

     访问http://localhost:8989/hystrix.stream,如下:



输入http://localhost/hystrix.stream,效果如下:




     在http://localhost:8989/hystrix.stream的输入红色框的内容:



   点击monitor stream,效果如下:




源码地址:https://github.com/wj903829182/springcloud3

上一篇地址:http://blog.csdn.net/j903829182/article/details/78494437



阅读全文
0 0
原创粉丝点击