1、创建服务中心 Eureka Server
1.1 新建spring cloud工程,springcloud-eureka-server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaServerApplication.class, args);
}
}
1.3 配置文件 application.properties ,配置内容:
server.port= 9525
eureka.instance.hostname=localhost
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.server.eviction-interval-timer-in-ms=5000
eureka.server.enable-self-preservation=true
eureka.server.renewal-percent-threshold=0.1
1.4 启动 服务中心 工程,打开浏览器,访问 服务中心 前台页面:http://localhost:9525
2、创建服务中心集群
2.1 创建工程配置文件 application-server1.properties ,配置内容:
server.port= 9527
spring.profiles=server1
eureka.instance.hostname=localhost
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
eureka.server.eviction-interval-timer-in-ms=5000
eureka.server.enable-self-preservation=false
eureka.server.renewal-percent-threshold=0.49
并创建工程配置文件 application-server2.properties,并修改 server.port 为 9528 以及 application-server3.properties,并修改 server.port 为 9529
3、创建服务提供者Eureka Client
3.1 新建spring cloud工程,springcloud-eureka-client
3.2 在工程启动类添加@EnableEurekaClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaClientApplication.class, args);
}
}
3.3 新建 Eureka Client 控制器类 EurekaClientController,提供一个 Restful 服务
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
public class EurekaClientController {
@Value("${server.port}")
private String port;
/**
* 提供的一个restful服务,返回内容格式:服务协议://服务器地址:服务端口/服务uri
*
* @param request
* @return
*/
@RequestMapping("/info")
public String info(HttpServletRequest request) {
String message = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getServletPath();
return message;
}
}
3.4 配置文件 application.properties ,配置内容:
server.port= 52601
spring.application.name=eureka-client
eureka.instance.hostname=localhost
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.client.service-url.defaultZone= http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
3.5 启动 Eureka Client 工程,打开浏览器,访问 Eureka Client 提供的 Restful 路径 http://localhost:52601/info
4、创建服务提供者集群
4.1 创建工程配置文件 application-client1.properties ,配置内容:
server.port= 52601
spring.profiles=client1
spring.application.name=eureka-client
eureka.instance.hostname=localhost
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.client.service-url.defaultZone= http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
并创建工程配置文件 application-client2.properties,并修改 server.port 为 52602 以及 application-client3.properties,并修改 server.port 为 52603
4.3 访问 服务中心 前台页面 http://localhost:9527,http://localhost:9528,http://localhost:9529
5、创建服务消费者
5.1 新建spring cloud工程,springcloud-eureka-feign
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableHystrix
@EnableDiscoveryClient // 启用 Eureka 服务发现
@EnableFeignClients // 启用 Feign
@SpringBootApplication
public class SpringcloudEurekaFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaFeignApplication.class, args);
}
}
5.3 创建服务类 EurekaFeignService
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
//@FeignClient(value = "eureka-client") // 调用的服务的名称
@FeignClient(value = "EUREKA-CLIENT", fallback = EurekaFeignServiceFailure.class) // 调用的服务的名称
public interface EurekaFeignService {
@RequestMapping(value = "/info")
String getInfo();
}
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class EurekaRibbonService {
@Autowired
private RestTemplate restTemplate;
public String getInfo() {
String message;
try {
System.out.println("调用 服务 EUREKA-CLIENT/info");
message = restTemplate.getForObject("http://EUREKA-CLIENT/info", String.class);
System.out.println("here");
System.out.println("服务 EUREKA-CLIENT/info 返回信息 : " + message);
System.out.println("调用 服务 EUREKA-CLIENT/info 成功!");
} catch (Exception ex) {
message = ex.getMessage();
}
return message;
}
5.4 创建控制器类 EurekaFeignController
import com.example.springcloudeurekafeign.service.EurekaFeignService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class EurekaFeignController {
@Resource
private EurekaFeignService eurekaFeignService;
@RequestMapping("/feignInfo")
public String feignInfo() {
String message = eurekaFeignService.getInfo();
return "获取到的信息:" + message;
}
}
5.5 配置文件 application.properties ,配置内容:
server.port= 52620
spring.application.name=eureka-discovery-feign
eureka.instance.hostname=localhost
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.client.service-url.defaultZone=http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
feign.hystrix.enabled=true
5.6 启动服务消费者工程,访问服务中心前台界面 http://localhost:9527,http://localhost:9528,http://localhost:9529
可以看到服务消费实例已经注册到了服务注册中心,服务名 EUREKA-DISCOVERY-FEIGN
可以看到服务消费者成功调用了服务提供者提供的服务,并实现了负载均衡,轮询请求不同的服务提供者。
以上一个简单的单点服务消费者搭建完成。