SpringCloud-Eureka-Ribbon项目搭建和问题总结

一、创建Eureka-Server项目

1.创建springboot项目

 2.完善项目信息,和勾选相应功能依赖

 3.选择相应的项目路径

 4.eureka-server的application.yml

server:
  port: 8700

#eureka客户端的注册地址,也就是eureka服务的提供方(注册中心)
eureka:
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
    #自己不再想eureka注册
    register-with-eureka: false
    #Erueka是为注册中心,不需要检索服务信息;(表示是否从Eureka Server获取注册信息,默认为true。 如果这是一个单点的 Eureka Server,不需要同步其他节点的数据,可以设为false)
    fetch-registry: false
  instance:
    hostname: 127.0.0.1
#服务名称
spring:
  application:
    name: eureka-server

 5.启动类代码

package com.yun.springcloud;

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

@SpringBootApplication
@EnableEurekaServer
public class SpringEurekaServerApplication {

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

}

 6.pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yun</groupId>
    <artifactId>spring-eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-eureka-server</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>

</project>

 二、创建服务提供方server-support项目

1.创建springboot模块

 2.完善模块信息

 3.选择模块路径和eureka服务端项目并列

 4.服务提供方的controller

package com.yun.springcloudeurekaservicesupport.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hanjl
 * @Description 服务提供方测试controller
 * @ClassName TestController
 * @createTime 2021/9/14 11:20
 */

@RestController
@RequestMapping(value = "/test")
public class TestController {


    @RequestMapping(value = "/hello")
    public Map<String,Object> helloWorld(String s){
        Map<String,Object> result = new HashMap<>(16);
        result.put("data",s);
        result.put("code",200);
        result.put("message","服务调用成功");
        System.out.println("请求参数:"+ s);
        return result;
    }
}

 5.服务提供方的启动类

package com.yun.springcloudeurekaservicesupport;

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;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableEurekaClient
@ComponentScan(basePackages = {"com.yun"})
public class SpringcloudEurekaServicesupportApplication {

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

}

 6.服务提供方的application.yml

server:
  port: 8701

eureka:
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8700/eureka
  instance:
    hostname: 127.0.0.1

spring:
  application:
    name: service-support

 7.服务提供方的依赖

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yun</groupId>
    <artifactId>springcloud-eureka-servicesupport</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-eureka-servicesupport</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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>

</project>

 三、创建服务消费方

1.创建服务消费方的module,过程和创建服务提供方一致,创建完成项目展示如图

 2.controller

package com.yun.springcloudserviceconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriTemplateHandler;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hanjl
 * @Description ribbon调用
 * @ClassName HelloController
 * @createTime 2021/9/14 13:59
 */
@RestController
@RequestMapping(value = "/ribbon")
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private DiscoveryClient discoveryClient;
  /*  @Resource
    private SpringClientFactory springClientFactory;*/

    @RequestMapping(value = "/hello")
    public Map<String,Object> helloWorld(String s){
        System.out.println("传入的值:" + s);
        List<String> services = discoveryClient.getServices();
        System.out.println("无敌:"+ services.toString());

        List<ServiceInstance> instances = discoveryClient.getInstances("service-support");
        System.out.println("服务列表:" + instances.toString());


        UriTemplateHandler uriTemplateHandler = restTemplate.getUriTemplateHandler();
        System.out.println("template:"+uriTemplateHandler);
        String forObject = restTemplate.getForObject("http://SERVICE-SUPPORT/test/hello?str=" + s, String.class);
        Map<String,Object> result = new HashMap<>(16);
        result.put("data",forObject);
        result.put("code",200);
        result.put("message","ribbon调用成功");
        return result;
    }
}

 3.启动类

package com.yun.springcloudserviceconsumer;

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;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableEurekaClient
public class SpringcloudServiceConsumerApplication {

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

}

 4.ribbon调用使用的restTemplate配置类

package com.yun.springcloudserviceconsumer.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author hanjl
 * @Description 对象配置
 * @ClassName BeansConfig
 * @createTime 2021/9/14 14:00
 */
@Configuration
public class BeansConfig {

    @Bean
    @LoadBalanced //ribbon客户端负载均衡
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

 5.application.yml

server:
  port: 8702

eureka:
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8700/eureka

  instance:
    hostname: 127.0.0.1


spring:
  application:
    name: eureka-consumer

 6.pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yun</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-service-consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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>

</project>

 四、然后顺序启动项目,访问eureka管理页面http://localhost:8700/

 五、完成上面内容我所遇到的问题

1.服务调用方老是获取不到服务列表,报错内容是java.lang.IllegalStateException: No instances available for PROVIDER之类的。

百度的可能原因有一下几个:

(1)缺失依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

(2)URL没有使用服务名称。而是用了IP地址加端口号

        String forObject = restTemplate.getForObject("http://SERVICE-SUPPORT/test/hello?str=" + s, String.class);

 (3)eureka服务端没有配置正确

eureka.client.fetch-registry:true

 其实还有一些我都尝试了,都不正确,最后发现我的eureka客户端使用的是3.0.3版本,而3版本以上的已经集成了ribbon了。所以在服务消费方不要再添加ribbon依赖。

六、备注

1.@EnableDiscoveryClient和@EnableEurekaClient的区别

简单区分下,他们在功能上是一致的:写在启动类的上,开启服务注册发现功能。

不同的是,当注册中心不一样时,像:eureka、consul、zookeeper,使用是也有了区别。

EnableDiscoveryClient注解在common包中,通过项目的classpath来决定使用哪种实现,而EnableEurekaClient注解在netflix包中,只会使用eureka这种实现方式;

所以,使用EnableDiscoverClient,对任何注册中心都适用。而EnableEurekaClient是为eureka服务的。

springcloud的Dalston或更早期的版本EnableEurekaClient是包含EnableDiscoverClient注解的,这种情况使用什么已经没区别了。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值