>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring:Java Web开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 20439 个阅读者 刷新本主题
 * 贴子主题:  Spring Cloud 服务发布与调用 回复文章 点赞(0)  收藏  
作者:sunshine    发表时间:2020-03-23 22:58:47     消息  查看  搜索  好友  邮件  复制  引用

  

1. 发布服务

  • 引入相关依赖pom.xml
<?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 http://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.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zws.cloud</groupId>
    <artifactId>Eurka-Producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Eurka-Producer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>

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

        <!-- Eureka 客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

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

</project>

  • 配置Eureka Server地址 application.yml
server:
  port: 8080

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

spring:
  application:
    name: Eureka-Producer

  • 发布一个服务,其实就是一个Http接口,如下:
package com.zws.cloud.producer;

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

/**
* @Author wensh.zhu
* @Date 2019/2/16 19:33
*/

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

启动
package com.zws.cloud.producer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EurekaProducerApplication {

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

}

2. 调用服务

  • 依赖配置pom.xml
<?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 http://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.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zws.cloud</groupId>
    <artifactId>Eurka-Producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Eurka-Producer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <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>

        <!-- Ribbon用于客户端负载均衡 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

        <!-- Feign用于服务发现与调用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

    </dependencies>

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

</project>

  • 启用Ribbon负载均衡以及Feign接口注入,application.yml
server:
  port: 8088

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

spring:
  application:
    name: Eureka-Consumer

  • 定义Feign客户端以及熔断回调
package com.zws.cloud.consumer;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* @Author wensh.zhu
* @Date 2019/2/16 15:14
*/

@FeignClient(value = "Eureka-Producer", fallbackFactory = HelloClientFallbackFactory.class)
public interface HelloClient {

    @GetMapping("/hello")
    String hello();
}

package com.zws.cloud.consumer;

import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

/**
* @Author wensh.zhu
* @Date 2019/2/16 16:12
*/

@Component
public class HelloClientFallbackFactory implements FallbackFactory<String>, HelloClient {
    @Override
    public String create(Throwable throwable) {
        return this.hello();
    }

    @Override
    public String hello() {
        return "";
    }
}

  @FeignClient(value = "Eureka-Producer", fallbackFactory = HelloClientFallbackFactory.class)其中Eurka-Producer为服务提供方的实例名称(spring.application.name)
  • 启用Feign接口以及调用
package com.zws.cloud.EurkaProducer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@EnableFeignClients
@SpringBootApplication
public class EurkaProducerApplication {

    @Resource
    private HelloFeign helloFeign;

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

    @GetMapping("/hello")
    public String hello() {
        return helloFeign.hello();
    }

}

3. 总结

服务发布主要涉及如下三个方面:
  • 配置Eureka Server地址:

    ## 多个以逗号隔开
    eureka:
    client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/

  • 引入Eureka Client依赖

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

  • 定义普通的Http接口
服务调用
  • 引入三个依赖:
<!-- Eureka客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!-- Ribbon用于负载均衡 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

<!-- Feign用于服务发布与调用 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

  • 配置Eureka Server:
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/

  • 定义FeignClient接口以及失败的回调
  • 启动类添加@EnableFeignClients注解
@EnableFeignClients会把第三步定义的接口注入Spring上下文中 ,然后使用的时候直接注入即可。

----------------------------
原文链接:https://blog.51cto.com/wenshengzhu/2350974

程序猿的技术大观园:www.javathinker.net




[这个贴子最后由 flybird 在 2020-03-23 22:58:47 重新编辑]
  Java面向对象编程-->Java常用类(上)
  JavaWeb开发-->自定义JSP标签(Ⅰ)
  JSP与Hibernate开发-->第一个helloapp应用
  Java网络编程-->基于UDP的数据报和套接字
  精通Spring-->组合(Composition)API
  Vue3开发-->Vue组件开发基础
  RocketMQ 常用消息类型
  Mybatis Plus 多租户架构(Multi-tenancy)实现
  Spring 5 webflux响应式编程 - 但时间也偷换概念
  重新理解响应式编程
  @Resource注解的用法
  解析配置文件 redis.conf、Redis持久化RDB、Redis的主从复制
  理解 RESTful 风格的 API
  spring cloud分布式微服务的概览
  Spring Security中使用的责任链模式
  写给新手的Spring Cloud的微服务入门教程
  一睹Web服务真面目,有商业价值的Web服务是这样的
  Spring配置日志
  Spring MVC:切面的应用
  Spring Boot整合WebSocket及Spring Security实例
  springboot —— 多数据源
  更多...
 IPIP: 已设置保密
楼主      
1页 2条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。