springCloud Eureka集群整合Zuul、Feign

简介

Eureka是Spring Cloud Netflix微服务套件中的一部分,提供了服务治理的功能。Eureka集群中的节点基于REST服务进行通信,如使用HttpClient与RestTemplate,Spring Cloud 套件中的Feign模块提供了更为简洁的服务访问。
Zuul是一个网关,能够将集群的服务隐藏到网关后面,统一对外提供服务,功能上类似Nginx(部分类似),提升了集群的安全性。

集群结构

在这里插入图片描述
本示例共4个项目,分别为eureka-server、service-provider、service-invoker和zuul-server。其中,eureka-server为服务注册中心,端口号为8761;service-provider为服务提供者,端口号为9100;service-invoker为服务调用者,端口号为9000;zuul-server为网关,端口号为8080。

1、构建Eureka服务器
创建名为eureka-server的maven项目,或者使用eclipse(idea)的Springboot生成器创建Springboot项目。pom.xml文件如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.16.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.SR4</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>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

接下来,编写一个Springboot的启动类,并加上@EnableEurekaServer即可将此项目声明为Eureka服务器。启动类如下所示:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {

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

}

最后修改application.properties文件。

server.port: 8761

是否向Eureka服务器注册

eureka.client.register-with-eureka: false

是否抓取注册信息

eureka.client.fetch-registry: false

因为本例只有一台eureka服务器(可以是多台),所以register-with-eureka和fetch-registry这两项为false

2、编写服务提供者
创建名为service-provider的maven项目。pom.xml文件如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.16.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.SR4</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</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>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

编写一个Controller,提供REST服务。代码如下:

@RestController
public class ProviderController {
    @RequestMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name) {
        return "Hello," + name;
    }
}

编写Springboot启动器,需要加上@EnableEurekaClient注解声明该项目为eureka的客户端。代码如下:

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

最后修改application.properties文件。

server.port=9100
spring.application.name=servcie-provider
#主机名
eureka.instance.hostname= localhost
#eureka服务器地址

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka

3、编写服务调用者(使用Feign)
创建名为service-invoker的maven项目。pom.xml文件如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.16.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.SR4</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</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-ribbon</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>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

首先编写一个HelloService接口,Feign使用该接口完成请求。@FeignClient注解中的值表示调用服务名为service-provider的节点,@RequestMapping注解声明了调用该节点的哪个路由。

@FeignClient("service-provider")
public interface HelloService {
    @RequestMapping("/hello/{name}")
    String hello(@PathVariable("name") String name);
}

编写一个Controller,对外提供服务,使用HelloService接口(代理对象)完成服务调用。

@RestController
@Configuration
public class InvokerController {

    @Autowired
    private HelloService helloService;
    
    @RequestMapping("/invoker-hello/{name}")
    public String hello(@PathVariable String name) {
        return helloService.hello(name);
    }
}

编写Springboot启动器,需要加上@EnableDiscoveryClient注解声明有能力发现eureka中的服务,加上@EnableFeignClients打开Feign开关。启动器代码如下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class InvokerApplication {

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

最后修改application.properties文件。

server.port=9000
spring.application.name=server-invoker

#主机名

eureka.instance.hostname= localhost

#eureka服务器地址

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka

4、构建Zuul网关服务器
创建名为zuul-server的maven项目。pom.xml文件如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.16.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.SR4</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</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-zuul</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </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>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

编写一个Springboot的启动类,并加上@EnableZuulProxy即可将此项目声明为zuul服务器。启动类如下所示:

@SpringBootApplication
@EnableZuulProxy
public class ZuulGatewayApplication {

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

接下来,最关键的一步是配置zuul服务的application.properties文件,zuul的请求转发在此配置。

server.port=8080
spring.application.name=zuul-server

eureka.instance.hostname: localhost
eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka

zuul.routes.sale.path=/test/**
zuul.routes.sale.service-id=server-invoker

这个配置的含义为将所有/test/**的请求转发到服务名为service-invoker的节点进行处理。

测试:

启动服务器eureka-server项目
启动服务提供者service-provider项目
启动服务调用者service-invoker项目
启动服务调用者zuul-server项目

发布了31 篇原创文章 · 获赞 14 · 访问量 9557

猜你喜欢

转载自blog.csdn.net/weixin_42555514/article/details/99586250
今日推荐