Skip to content

Cynaith/Spring-Cloud

Repository files navigation

Spring-Cloud

Eureka

    • Maven环境下:
    <!--https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
  • 配置文件

    • SpringBoot环境下:

    key: value: 含义: 注: |:-: | :-: | :-: | :-:| |eureka:instance:hostname|127.0.0.1|服务注册中心IP地址| |eureka:client:registerWithEureka|false|是否向服务注册中心注册自己| |eureka:client:fetchRegistry|false|是否检索服务| |eureka:client:serviceUrl|defaultZone:
    http://${eureka.instance.hostname}:
    ${server.port}/eureka/|服务注册中心的配置内容,指定服务注册中心的位置|

  • 主要注解


    注解: 含义: 注: |:-: | :-: | :-:| |@EnableEurekaServer|启用Eureka服务端| |@EnableEurekaClient|如注册中心为Eureka,则推荐| |@EnableDiscoveryClient|注解中含有@EnableEurekaClient功能|

Feign

    • Maven环境下:(消费者中添加依赖)
    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-feign</artifactId>
          <version>1.4.6.RELEASE</version>
      </dependency>
    
  • 主要步骤


    步骤 Demo 注 |-|-|-| |创建一个名为UserFeignClient接口|@FeignClient(name="user-api")
    public interface UserFeignClient{
    @RequestMapping(value="/getUser")
    public String getUser(@RequestParam("id")int id);
    }|注:@FeignClient("")注解中name值为微服务项目在EurekaServer中注册名称。
    或与spring.application.name一致 |创建一个类提供方法,通过接口调用Eureka提供者的方法|@RestController
    public class UserController{
    @Autowired
    private UserFeignClient userFeignClient;
    @RequestMapping("/getUser")
    public String getUser(){
    return userFeignClient.getUser(2);
    }
    }| |修改启动类|@RestController
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    public class UserserviceApplication{
    public static void main(String[] args){
    SpringApplication.run(UserserviceApplication.class,args);
    }
    }|


Hystrix

  • 主要步骤


    步骤 Demo 注 |-|-|-| |@FeignClient()加注解|@FeignClient(name="user-api",fallback=UserFeignClientFallBack.class)
    public interface UserFeignClient{
    @RequestMapping(value="/getUser")
    public String getUser(@RequestParam("id")int id);
    }| |创建容器处理类实现Feign接口|@Component
    public class UserFeignClientFallBack implements UserFeignClient{
    @Override
    public String getUser(int id){
    return "服务异常,请稍后重试";
    }
    }| |配置文件|feign:
    hystrix:
    enabled:true|


Releases

No releases published

Packages

No packages published

Languages