博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring-Cloud-eureka(Dalston)的配置
阅读量:7220 次
发布时间:2019-06-29

本文共 7379 字,大约阅读时间需要 24 分钟。

hot3.png

注册中心的配置:

需要的依赖的配置

<parent>

    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- SpringCloud eureka server-->
   
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </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>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 

 

2、注册中心配置文件application.properties的配置

#配置注册中心我的端口号

server.port=5001

#配置服务的名称

spring.application.name=spring-cloud-register

#服务所在的机器的名称

eureka.instance.hostname=com.lcg.test0

#配置eureka不注册自己

eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false

#配置注册中心的地址

eureka.client.serviceUrl.defaultZone=http\://${eureka.instance.hostname}\:${server.port}/eureka,http\://com.lcg.test1\:4001/eureka

 

3、配置中心启动类

 

package com.lcg;

 

 

 

import org.springframework.boot.SpringApplication;

 

import org.springframework.boot.autoconfigure.SpringBootApplication;

 

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

 

 

 

@SpringBootApplication

 

@EnableEurekaServer

 

public class DemoApplication {

 

 

 

    public static void main(String[] args) {

 

        SpringApplication.run ( DemoApplication.class, args );

 

    }

 

}

发布接口的服务端的配置

依赖配置:

<parent>

 

    <groupId>org.springframework.boot</groupId>

 

    <artifactId>spring-boot-starter-parent</artifactId>

 

    <version>1.5.4.RELEASE</version>

 

    <relativePath/> <!-- lookup parent from repository -->

 

</parent>

 

 

 

<properties>

 

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

 

    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

 

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

 

</properties>

 

 

 

<dependencies>

 

    <dependency>

 

        <groupId>org.springframework.cloud</groupId>

 

        <artifactId>spring-cloud-starter-eureka</artifactId>

 

    </dependency>

 

 

 

    <dependency>

 

        <groupId>org.springframework.boot</groupId>

 

        <artifactId>spring-boot-starter</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>Dalston.SR1</version>

 

            <type>pom</type>

 

            <scope>import</scope>

 

        </dependency>

 

    </dependencies>

 

</dependencyManagement>

 

 

2、发布接口的服务端的application.properties配置

#配置服务端的端口

 

server.port=8090

 

#服务的名称

 

spring.application.name=cloud-server-provider

 

#配置服务的注册中心

 

eureka.client.service-url.defaultZone==http\://com.lcg.test0\:5001/eureka,http\://com.lcg.test1\:4001/eureka

 

3、服务端启动类的配置

package com.lcg;

 

 

 

import org.springframework.boot.SpringApplication;

 

import org.springframework.boot.autoconfigure.SpringBootApplication;

 

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

 

 

 

@SpringBootApplication

 

@EnableEurekaClient

 

public class CloudServerProviderApplication {

 

 

 

    public static void main(String[] args) {

 

        SpringApplication.run ( CloudServerProviderApplication.class, args );

 

    }

 

}

 

 

 

 

 

 

 

4、服务接口的测试

package com.lcg.server;

 

 

 

 

 

import org.springframework.beans.factory.annotation.Autowired;

 

import org.springframework.cloud.client.discovery.DiscoveryClient;

 

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

 

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

 

 

 

import javax.servlet.http.HttpServletRequest;

 

 

 

@RestController

 

public class TestServerProviderController {

 

 

 

    @Autowired

 

    private DiscoveryClient discoveryClient;

 

 

 

    @RequestMapping("/test")

 

    public String test1(HttpServletRequest request){

 

 

 

        System.out.println ("=========ppp===========>"+request.getParameter ( "str" ));

 

 

 

        return "server-provider-ok";

 

    }

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

客户端的配置

客户端依赖:

<parent>

 

    <groupId>org.springframework.boot</groupId>

 

    <artifactId>spring-boot-starter-parent</artifactId>

 

    <version>1.5.4.RELEASE</version>

 

    <relativePath/> <!-- lookup parent from repository -->

 

</parent>

 

 

 

<dependencies>

 

    <dependency>

 

        <groupId>org.springframework.cloud</groupId>

 

        <artifactId>spring-cloud-starter-eureka</artifactId>

 

    </dependency>

 

    <dependency>

 

        <groupId>org.springframework.boot</groupId>

 

        <artifactId>spring-boot-starter-web</artifactId>

 

    </dependency>

 

 

 

    <dependency>

 

        <groupId>org.springframework.cloud</groupId>

 

        <artifactId>spring-cloud-starter-feign</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>Dalston.SR1</version>

 

            <type>pom</type>

 

            <scope>import</scope>

 

        </dependency>

 

    </dependencies>

 

</dependencyManagement>

2、客户端application.properties配置文件

server.port=8080

 

 

 

spring.application.name=cloud-server-client

 

 

 

eureka.client.service-url.defaultZone==http\://com.lcg.test0\:5001/eureka,http\://com.lcg.test1\:4001/eureka

 

 

3、客户端启动类

package com.lcg;

 

 

 

import org.springframework.boot.SpringApplication;

 

import org.springframework.boot.autoconfigure.SpringBootApplication;

 

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

 

import org.springframework.cloud.netflix.feign.EnableFeignClients;

 

@SpringBootApplication

 

@EnableEurekaClient

 

@EnableFeignClients

 

public class CloudServerClientApplication {

 

    public static void main(String[] args) {

 

        SpringApplication.run ( CloudServerClientApplication.class, args );

 

    }

 

}

4、测试类

package com.lcg.client;

 

import org.springframework.beans.factory.annotation.Autowired;

 

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

 

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

 

import java.sql.SQLOutput;

 

@RestController

 

public class TestClientController {

 

    @Autowired

 

    private TestClient testClient;

 

    @RequestMapping("/testClient")

 

    public String test(){

 

        String str= testClient.test000 ( "客户端的请求信息" );

 

        System.out.println ("----------服务端返回--------message------"+str);

 

        return "ok";

 

    }

 

 

 

}

接口配置类

package com.lcg.client;

 

 

 

import org.springframework.cloud.netflix.feign.FeignClient;

 

import org.springframework.web.bind.annotation.RequestBody;

 

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

 

import org.springframework.web.bind.annotation.RequestParam;

 

               //服务的名称

 

@FeignClient("cloud-server-provider")

 

public interface TestClient {

 

 

 

    /**

 

    * @RequestParam 配置请求参数的注解

 

    * @param str

 

    * @return

 

    */

 

    @RequestMapping("/test")

 

    public String test000(@RequestParam("str") String str);

转载于:https://my.oschina.net/demons99/blog/1936570

你可能感兴趣的文章
积累_前辈的推荐
查看>>
strcpy和memcpy的区别《转载》
查看>>
在windows平台下electron-builder实现前端程序的打包与自动更新
查看>>
DroidPilot V2.1 手写功能特别版
查看>>
COOKIE欺骗
查看>>
js 强转规范解读
查看>>
ACdream - 1735:输油管道
查看>>
golang 获取get参数
查看>>
服务器状态码
查看>>
非小型电子商务系统设计经验分享
查看>>
Video Target Tracking Based on Online Learning—深度学习在目标跟踪中的应用
查看>>
深度学习理论解释基础
查看>>
遗传算法
查看>>
将web网站移动化
查看>>
Application-Session-Cookie
查看>>
Perl的多进程框架(watcher-worker)
查看>>
phpMyAdmin 后台拿webshell
查看>>
Linux 关机 休眠, 关闭移动设备自动挂载 命令
查看>>
Html唤起手机APP,如果有就唤起,如果没有就跳到下载页。
查看>>
Java中File类如何扫描磁盘所有文件包括子目录及子目录文件
查看>>