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

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

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

pom.xml配置文件内容:

<packaging>jar</packaging>
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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);
    }
}

项目结构

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

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);
    }
}

项目结构

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

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

3、搭建Ribbon

pom.xml配置文件内容:

<packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
    </properties>
    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!--Eureka相关依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

    <!--Ribbon相关依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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();
    }
}

项目结构

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

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

4、搭建Fegin

pom.xml配置文件内容:

<packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/>
    </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-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <!--注意这个位置,必须是springboot支持的版本。否则会报出依赖会有错误,但是编译没有问题
                运行出 java.net.UnknownHostException: 的错-->
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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);
    }
}

项目结构

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值