Spring Cloud (一)Eureka(服务注册与消费详解)

Spring Cloud Eureka(服务注册与消费详解)

1.Spring Cloud Eureka 服务注册中心 Eureka原理 参考原文

2.项目工具及环境

  • JDK1.8
  • Spring boot 2.0
  • Intellij Idea
  • maven 3以上
  • maven下载资源包的时候会遇到无法下载的问题;更改maven镜像为国外的地址

    ui
    central
    Human Readable Name for this Mirror.
    http://uk.maven.org/maven2/


    sprintio
    central
    Human Readable Name for this Mirror.
    https://repo.spring.io/libs-snapshot/

    3.开始创建项目

    • 开始搭建
      1.先打建一个最基础的maven项目,然后就自己next,再依次填写即可
      maven项目
      2.删除最该maven项目下的src文件夹,新建moudel
      新module
      新建spring boot项目
      新建spring boot项目
      先不选择依赖
      先不选择依赖

    • 然后搭建项目基本目录
      项目目录基本结构图

4.进行配置

1.. 配置项目的公共pom.xml

 
<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">
    <modelVersion>4.0.0modelVersion>

    <groupId>cn.qxgroupId>
    <artifactId>eurekaartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>demo-server1module>
        <module>demo-clent1module>
    modules>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.0.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Finchley.M8spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-cacheartifactId>
        dependency>
        
            
            
        

        <dependency>
            <groupId>org.springframework.sessiongroupId>
            <artifactId>spring-session-coreartifactId>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

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

    <repositories>
        <repository>
            <id>spring-milestonesid>
            <name>Spring Milestonesname>
            <url>https://repo.spring.io/milestoneurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>


project>

2.. eureka-server的pom.xml文件的配置


<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>eurekaartifactId>
        <groupId>cn.qxgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>demo-sever1artifactId>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    dependency>
dependencies>
project>

3.. eureka-server的application.yml

server:
  port: 8001
spring:
  application:
    name: demo-server1

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      #defaultZone: http://peer2:8002/eureka,http://peer3:8003/eureka
      defaultZone: http://localhost:8001/eureka/

4.. eureka-server的启动注解

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

@SpringBootApplication
@EnableEurekaServer
public class DemoServer1Application {

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

配置好后启动即可。
eureka-server注册中心启动截图

5.eureka-client客户端,可以业务工程可以理解为服务提供者

  • eureka-client的pom.xml

<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>eurekaartifactId>
        <groupId>cn.qxgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>demo-client1artifactId>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
dependencies>
project>
  • eureka-client的application.yml
spring:
  application:
    name: demo-clent
server:
  port: 9001

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
  • eureka-client启动注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class DemoClent1Application {

    public static void main(String[] args) {
        SpringApplication.run(DemoClent1Application.class, args);
    }
}
  • eureka-client创建一个HelloController提供外界访问
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/info")
    public String Hello(){
        return "hello xxx,this is demo-client1 messge";
    }
    /*
        服务提供者
     */
    @RequestMapping("/producerHello")
    public String Hello(@RequestParam("name") String name){
        return "hello " + name + ",this is demo-client1 messge";
    }
}
  • 可以再创建一个服务提供者,仅仅application.yml端口不一样,其它的无区别
spring:
  application:
    name: demo-clent
server:
  port: 9002

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
  • eureka-consumer配置及代码
ss
  • eureka-consumer的pom.xml

<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>eurekaartifactId>
        <groupId>cn.qxgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>demo-consumer1artifactId>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-openfeignartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
dependencies>
project>
  • eureka-consumer的application.yml
spring:
  application:
    name: demo-consumer1

server:
  port: 7001

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/


  • eureka-consumer的代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class DemoConsumer1Application {

    public static void main(String[] args) {
        SpringApplication.run(DemoConsumer1Application.class, args);
    }
}
//@EnableDiscoveryClient :启用服务注册与发现
//@EnableFeignClients:启用feign进行远程调用

eureka-consumer的feign调用实现

//name:远程服务名,即spring.application.name配置的名称
@Service
@FeignClient(name= "demo-clent")
public interface HelloRemote {
    //需要匹配服务提供者接口名称
    @RequestMapping(value = "/producerHello")
    public String sayHello(@RequestParam(value="name") String name);
}

eureka-consumer的web层调用实现

@RestController
public class ConsumerController {

    @Autowired
    HelloRemote helloRemote;

    /*
        消费者的接口,去调用服务提供者
        问题:只能使用@RequestMapping("/consumerHello/{name}")  @PathVariable("name")方法 不知道有没有其它方式代替?
     */
    @RequestMapping("/consumerHello/{name}")
    public String index(@PathVariable("name") String name){
        return helloRemote.sayHello(name);
    }
    //有问题的
//    @RequestMapping("/consumerHello2")
//    public String index2(@RequestParam("name") String name){
//        return helloRemote.sayHello(name);
//    }
    @RequestMapping("/info")
    public String info(){
        return " Hi,I am a consumer!";
    }
}

测试
分别启动DemoServer1,DemoClient1,DemoClient2,DemoConsumer工程

  • 在浏览器中输入http://localhost:8001/ 会出现2个 DEMO-CLENT项目,一个DEMO-CONSUMER1
  • 在浏览器中输入http://localhost:9001/info会出现 hello xxx,this is demo-client1 messge
  • 在浏览器中输入http://localhost:9002/info会出现 hello xxx,this is demo-client2 messge
  • 在浏览器中输入http://localhost:7001/consumerHello/name会出现 hello name,this is demo-client1 messge ,多次访问会出现hello name,this is demo-client2 messge;因为服务中心自动提供了服务均衡负载的功能

Eureka-Server集群的配置参考Eureka-Server集群

问题集合

  • 问题: eureka-client注册不上
  • 解决: 添加 spring-boot-starter-web依赖包
<dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
dependency>
  • 问题:spring-cloud-starter-feign依赖包引入不进来
  • 解决:feign的依赖包名字有点改变,需要引入spring-cloud-starter-openfeign
ss

你可能感兴趣的:(SpringCloud)