Spring Cloud Starter Feign Ribbon Bootstrap AngularJS 是一个基于 Spring Cloud 的微服务架构项目

在Spring Cloud Starter Feign Ribbon Bootstrap AngularJS项目中,配置Feign客户端可以通过以下步骤进行:

  1. 添加依赖:首先,确保你的项目中引入了必要的依赖。你需要在pom.xml中添加Spring Cloud OpenFeign和Ribbon的依赖。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
    
  2. 启用Feign客户端:在你的Spring Boot主类上使用@EnableFeignClients注解来启用Feign客户端。

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableFeignClients
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
  3. 创建Feign客户端接口:定义一个接口并使用@FeignClient注解来指定远程服务的名称和配置。

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @FeignClient(name = "remote-service", configuration = FeignConfig.class)
    public interface RemoteServiceClient {
        @GetMapping("/api/data")
        String getData();
    }
    
  4. 自定义Feign配置(可选):如果你需要对Feign进行一些自定义配置,可以创建一个配置类。例如,设置连接超时、读取超时等。

    import feign.Logger;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class FeignConfig {
        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    }
    
  5. 注入和使用Feign客户端:最后,你可以在你的服务或控制器中注入并使用这个Feign客户端。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class MyService {
        private final RemoteServiceClient remoteServiceClient;
    
        @Autowired
        public MyService(RemoteServiceClient remoteServiceClient) {
            this.remoteServiceClient = remoteServiceClient;
        }
    
        public String fetchData() {
            return remoteServiceClient.getData();
        }
    }
    

Spring Cloud Starter Feign Ribbon Bootstrap AngularJS 是一个基于 Spring Cloud 的微服务架构项目,它整合了 Feign、Ribbon 和 AngularJS。以下是该项目的一些主要特点和组成部分:

  1. Feign: Feign 是一个声明式的 Web 服务客户端,使得调用 HTTP 接口变得简单。通过定义一个接口并使用注解来描述请求,Feign 会自动生成代码来实现这些请求。

  2. Ribbon: Ribbon 是 Netflix 发布的负载均衡器,它可以与 Feign 结合使用,实现对多个服务实例的负载均衡。

  3. Bootstrap: 在 Spring Cloud 中,Bootstrap 通常用于加载配置信息。在这个项目中,Bootstrap 可能用于加载一些全局配置或初始化一些资源。

  4. AngularJS: AngularJS 是一个前端 JavaScript 框架,用于构建动态的单页面应用(SPA)。在这个项目中,AngularJS 可能用于开发前端界面,与后端的 Spring Cloud 服务进行交互。

  5. Spring Cloud Starter: Spring Cloud Starter 是一组自动化配置的集合,简化了 Spring Cloud 应用程序的开发。在这个项目中,Spring Cloud Starter 提供了 Feign 和 Ribbon 的自动配置。

  6. 项目结构: 这个项目的结构可能包括以下几个部分:

    • src/main/java: 存放 Java 源代码文件。
    • src/main/resources: 存放配置文件,如 application.yml 或 application.properties。
    • src/main/webapp: 存放静态资源文件,如 HTML、CSS 和 JavaScript 文件。
    • src/test/java: 存放单元测试代码。
    • pom.xml: Maven 项目的配置文件,用于管理项目依赖和构建过程。
  7. 依赖管理: 在 pom.xml 文件中,可能会包含以下依赖:

    • Spring Boot Starter Web
    • Spring Cloud Starter OpenFeign
    • Spring Cloud Starter Netflix Ribbon
    • AngularJS (如果使用 Maven 进行前端依赖管理)
  8. 示例代码: 以下是一个简化的示例代码,展示了如何使用 Feign 和 Ribbon 进行服务调用:

@FeignClient(name = "service-name", configuration = FeignConfig.class)
public interface ServiceClient {
    @RequestMapping(value = "/api/resource", method = RequestMethod.GET)
    String getResource();
}

@Configuration
public class FeignConfig {
    @Bean
    public IRule ribbonRule() {
        return new RoundRobinRule(); // 使用轮询策略进行负载均衡
    }
}

@RestController
public class MyController {
    @Autowired
    private ServiceClient serviceClient;

    @GetMapping("/my-endpoint")
    public String myEndpoint() {
        return serviceClient.getResource();
    }
}
  1. 注意事项: 在使用 Spring Cloud Starter Feign Ribbon Bootstrap AngularJS 时,需要注意以下几点:
    • 确保所有依赖的版本兼容。
    • 正确配置 Feign 和 Ribbon 的相关属性。
    • 如果使用 AngularJS,确保前端代码与后端 API 正确对接。
    • 处理可能出现的异常和错误情况。

spring-cloud-starter-feign-ribbon-bootstrap-angularjs

  • Bootstrap中包含了丰富的Web组件,根据这些组件,可以快速的搭建一个漂亮、功能完备的网站。其中包括以下组件:
    下拉菜单、按钮组、按钮下拉菜单、导航、导航条、路径导航、分页、排版、缩略图、警告对话框、进度条、媒体对象等 Bootstrap,来自
    Twitter,是目前很受欢迎的前端框架。Bootstrap 是基于 HTML、CSS、JAVASCRIPT 的,它简洁灵活,使得 Web
    开发更加快捷。 它由Twitter的设计师Mark Otto和Jacob
    Thornton合作开发,是一个CSS/HTML框架。Bootstrap提供了优雅的HTML和CSS规范,它即是由动态CSS语言Less写成。Bootstrap一经推出后颇受欢迎,一直是GitHub上的热门开源项目,包括NASA的MSNBC(微软全国广播公司)的Breaking
    News都使用了该项目。[2]
    国内一些移动开发者较为熟悉的框架,如WeX5前端开源框架等,也是基于Bootstrap源码进行性能优化而来。Bootstrap是基于HTML5和CSS3开发的,它在jQuery的基础上进行了更为个性化和人性化的完善,形成一套自己独有的网站风格,并兼容大部分jQuery插件。
package com.programb.abel;

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;


@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients //开启fegin 客户端支持
@EnableCircuitBreaker //开启CircuitBreaker的支持
@EnableZuulProxy //开启网关代理支持
public class UiApplication {
    public static void main(String[] args) {
        SpringApplication.run(UiApplication.class, args);
    }
}

package com.programb.abel.bean;


public class Person {
    private Long id;

    private String name;


    public Person() {
        super();
    }

    public Person(String name) {
        super();
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



}
package com.programb.abel.controller;

import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import com.programb.abel.bean.Person;
import com.programb.abel.service.PersonHystrixService;
import com.programb.abel.service.SomeHystrixService;

import java.util.List;

@RestController
public class UiController {
    @Autowired
    private SomeHystrixService someHystrixService;

    @Autowired
    private PersonHystrixService personHystrixService;

    @RequestMapping("/dispatch")
    public List<Person> sendMessage(@RequestBody String personName) {
        return personHystrixService.save(personName);
    }

    @RequestMapping(value = "/getsome",produces={MediaType.TEXT_PLAIN_VALUE})
    public String getSome(){
        return someHystrixService.getSome();
    }
}

package com.programb.abel.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.programb.abel.bean.Person;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PersonHystrixService {

    @Autowired
    PersonService personService;

    @HystrixCommand(fallbackMethod = "fallbackSave") //使用HystrixCommand的fallbackMethod参数指定,调用失败的时候调用后备方法 fallbackMethod
    public List<Person> save(String name) {
        return personService.save(name);
    }

    public List<Person> fallbackSave(String name){
        List<Person> list = new ArrayList<Person>();
        Person p = new Person(name+"没有保存成功,Person Service 故障");
        list.add(p);
        return list;
    }
}

package com.programb.abel.service;

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 com.programb.abel.bean.Person;

import java.util.List;

@FeignClient("programb")
public interface PersonService {
    @RequestMapping(method = RequestMethod.POST, value = "/save",
            produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    List<Person> save(@RequestBody String  name);
}

package com.programb.abel.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;


@Service
public class SomeHystrixService {

    @Autowired
    RestTemplate restTemplate; // springboot 下使用ribbon ,只需注入一个 RestTemplate,springboot 已经为我们做好了配置

    @HystrixCommand(fallbackMethod = "fallbackSome") //HystrixCommand 的参数指定,当调用失败时,使用备用方法fallbackMethod
    public String getSome() {
        return restTemplate.getForObject("http://programb/getsome", String.class);
    }

    public String fallbackSome(){
        return "some service模块故障";
    }
}

server:
  port: 80
  

  


spring:
  application:
    name: ui-programb

eureka:
  instance:
    non-secure-port: ${server.port:80}
  client:
    service-url:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
<?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">
    <parent>
        <artifactId>springCloud</artifactId>
        <groupId>com.programb.abel</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ui.programb</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>angularjs</artifactId>
            <version>1.3.15</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>angular-ui-router</artifactId>
            <version>0.2.13</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
        </dependency>
    </dependencies>
</project>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bol5261

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值