spring cloud服务提供与调用示例

本文建立方式采用intellij IDEA  建立项目java

1.建立基于Eureka的注册中心。web

  在打开项目中右键,选择new 选择moudlespring

而后下一步apache

输入要建立的项目的信息浏览器

 

 选择web下面的web,选择cloud  discovery下面的 Eureka server  下一步,建立项目app

而后同步pom.xml文件内容、maven

<?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.example</groupId>
    <artifactId>server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</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-server</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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

 

 在启动文件中选择spring-boot

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

配置文件测试

server:
  port: 8000
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/
spring:
  application:
    name: spring-cloud-eureka

  建立完,咱们去运行下,运行正常后,咱们去访问localhost:8000, 到下面的界面,这样咱们Eureka 注册中心就建立成功,fetch

 

下面咱们去建立server端,和client;

server端呢建立中与Eureka选择不一样的在于cloud  discovery中,这里须要选择cloud Discovery

而后建立完,去同步对应的pom.xml文件

在启动类编写以下

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


配置文件
server: port:
8001 eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8000/eureka/ spring: application: name: spring-serverserver

咱们须要编写一个提供服务的接口

@RestController public class HelloController { @RequestMapping("/hello") public String indesx(@RequestParam String name) { return "hello "+name+",this is first messge"; } }

这样咱们就能够实现咱们的server端

而后咱们去建立client端

client端多须要在server上建立多一个Feign

那么咱们在启动类,以下

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

配置文件

server: port: 8002 eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8000/eureka/
spring: application: name: spring-client

那么咱们去写调用server的服务

@FeignClient(name= "spring-serverserver") public interface HelloRemote { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name); }

实现接口

@RestController public class ConsumerController { @Autowired HelloRemote lloRemote; @RequestMapping("/hello/{name}") public String index(@PathVariable("name") String name) { return lloRemote.hello(name); } }

这样咱们就实现了服务的注册与调用。

 

那么咱们去启动服务进行测试,服务注册成功,咱们去启动服务调用端

服务调用端成功,

那么咱们去测试下,咱们先去测试看单独访问服务是否正常

输入http://localhost:8001/hello?name=liwanlei  

显示

那么咱们看下另一个调用这个服务的服务

http://localhost:8002/hello/name

那么咱们看下返回

这样咱们调试成功。

户端已经成功的经过feign调用了远程服务,而且将结果返回到了浏览器。

相关文章
相关标签/搜索