spring cloud client 搭建(2/3)

2018-03-01 11:25:19来源:oschina作者:Germmy人点击

分享
步骤:

1、新建maven项目


2、pom文件如下:


<?xml version="1.0" encoding="UTF-8"?>
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">
4.0.0
com.mzjf.eurekaclient
eurekaclient
1.0-SNAPSHOT

org.springframework.boot
spring-boot-starter-parent
1.5.4.RELEASE




org.springframework.cloud
spring-cloud-starter-eureka


org.springframework.boot
spring-boot-starter-web





org.springframework.cloud
spring-cloud-dependencies
Dalston.SR1
pom
import




3、实现/dc请求处理接口


package com.mzjf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by germmy on 18/2/27.
*/
@RestController
public class DcController {
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/dc")
public String dc() {
String services = "Services: " + discoveryClient.getServices();
System.out.println(services);
return services;
}
}

4、新建spring-boot启动程序


package com.mzjf;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Created by germmy on 18/2/27.
*/
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(
Application.class)
.web(true).run(args);
}
}

5、新建application.yml文件


server:
port: 2001
spring:
application:
name: eureka-client-germmy
eureka:
instance:
hostname: localhost
port: 8081
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/

注意:server.port是client自己绑定的端口,8081是eureka server的端口


6、右击,Run,可以在eureka server看到名为eureka-client-germmy的客户端,如下图所求



------


带参数运行

将端口传入给spring-boot的启动程序,这样可以启动多个客户端


7、点击Run-Configuration,或者右上角的Edit Configuration,如下图所示



7.1、Defaults和加号的区别


Defaults是默认设置,类似于父类


加号类似于子类,会默认继承父类的



7.2、设置Defaults



7.2.1、设置加号里面的2个儿子,一个客户端为8082,一个客户端为8083,如下图所示



8、分别启动这2个客户端,在eureka server中可以看到,如下图所示:


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台