Spring-Cloud-eureka(Dalston)的配置

注册中心的配置:

需要的依赖的配置

<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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值