搭建最简单的springcloud包括Eureka、Ribbon、Fegin

本篇博文只包含搭建springcloud最简单的配置,有这些东西就可以起springcloud了,没有包含理论讲解

1、搭建springboot的Eureka注册中心项目(如何搭建springboot项目就不赘述了)

pom.xml配置文件内容:

jar

        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
        
    
    
        UTF-8
        UTF-8
        1.8
        Edgware.RELEASE
    

    
    
        org.springframework.cloud
        spring-cloud-starter-eureka-server
    
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

application.properties内容:

server.port=8000
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

启动类内容:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaCenterStarter {

    public static void main(String[] args) {
        SpringApplication.run(EurekaCenterStarter.class,args);
    }
}

项目结构

搭建最简单的springcloud包括Eureka、Ribbon、Fegin_第1张图片

之后启动启动类,在浏览器输入http://localhost:8000/即可看到Eureka界面,会发现这里是空的,我此处是已经启动了服务的状态

搭建最简单的springcloud包括Eureka、Ribbon、Fegin_第2张图片

2、搭建一个服务,然后注册到Eureka注册中心,都是使用springboot搭建

pom.xml与上面相同

application.properties内容:

server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
spring.application.name=service-hi

Controller类内容:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Controller
public class HiController {

    @Value("${server.port}")
    private String port;

    @RequestMapping("/hi")
    public String sayHi(String name){
        return "hi " + name +",i am from " + port;
    }
}

启动类内容:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaServer01 {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServer01.class, args);
    }
}

项目结构

搭建最简单的springcloud包括Eureka、Ribbon、Fegin_第3张图片

启动启动类,在Eureka注册中心界面就会发现这个服务,个数是1个,图片是启动了3个效果

搭建最简单的springcloud包括Eureka、Ribbon、Fegin_第4张图片

2、再搭建2个一模一样的服务,注意端口号要记得更改,就可以出现上图所示的样子SERVICE-HI启动了三个服务,为Ribbon和Fegin做准备

3、搭建Ribbon

pom.xml配置文件内容:

jar
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
        Edgware.RELEASE
    
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

    
    
        org.springframework.cloud
        spring-cloud-starter-eureka
    

    
    
        org.springframework.cloud
        spring-cloud-starter-ribbon
    
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

application.properties内容:

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
server.port=9003
spring.application.name=service-ribbon

Controller类内容:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
public class HiController {

    @Autowired
    private RestTemplate template;
    @RequestMapping("hi")
    @ResponseBody
    public String hello(String name){
        //利用template对象访问服务
        //url是服务连接地址
        //http://service-hi对应了后台服务提供者的工程
        ///hi?name=传入的参数
        //相当于用服务名称代替了工程访问的域名+端口
        String hi = template.getForObject(
                "http://service-hi/hi?name="+name, String.class);
        return hi;
    }
}

启动类内容:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonClient {

    public static void main(String[] args) {
        SpringApplication.run(RibbonClient.class, args);
    }

    //ribbon组件配合一个对象RestTemplate
    //利用restTemplate调用服务
    @Bean //其他代码可以注入使用
    @LoadBalanced //利用这个对象访问服务,底层的负载均衡需要指定
    //这个注解,轮训
    public RestTemplate getResouce(){
        return new RestTemplate();
    }
}

项目结构

搭建最简单的springcloud包括Eureka、Ribbon、Fegin_第5张图片

好了,启动启动类,Eureka注册中心界面就可以看到这个:

搭建最简单的springcloud包括Eureka、Ribbon、Fegin_第6张图片

在浏览器输入http://localhost:9003/hi?name=Cathy,一直点重新加载,就可以看到端口号的位置一直是0、1、2的轮训变化

搭建最简单的springcloud包括Eureka、Ribbon、Fegin_第7张图片

4、搭建Fegin

pom.xml配置文件内容:

jar
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
    
    
    
        org.springframework.cloud
        spring-cloud-starter-eureka
    
    
        org.springframework.cloud
        spring-cloud-starter-feign
    
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                
                Dalston.SR4
                pom
                import
            
        
    

application.properties内容:

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
server.port=9004
spring.application.name=service-feign

Controller类内容:

import com.test.service.HiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HiController {
    @Autowired
    private HiService service;
    @RequestMapping("hi")
    @ResponseBody
    public String sayHi(String name){
        String hi=service.sayHi(name);
        return hi;
    }
}

Service类内容:

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("service-hi")
public interface HiService {
    //template.getObject(url,String.class)
    //http://service-hi/hi?name=Cathy
    @RequestMapping(value="/hi",method= RequestMethod.GET)
    public String sayHi(@RequestParam(value="name")String name);
}

启动类内容:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

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

项目结构

搭建最简单的springcloud包括Eureka、Ribbon、Fegin_第8张图片

启动启动类,在Eureka注册中心界面就可以看到这个:

搭建最简单的springcloud包括Eureka、Ribbon、Fegin_第9张图片

在浏览器输入http://localhost:9004/hi?name=Cathy,可以看到和Ribbon同样的效果

你可能感兴趣的:(springcloud,springcloud,Eureka,Ribbon,Feign,简单搭建)