SpringCloud微服务创建与学习之Eureka
1. Ribbon 1.1 按照2.2.1创建新Model,创建spring_cloud_eureka_ribbon工程。 1.2 pom.xml1.3 application.ymlSpringCloudLearn org.ccit 1.0-SNAPSHOT 4.0.0 org.ccit.SPringCloudLearn spring_cloud_eureka_ribbon org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-ribbon org.springframework.cloud spring-cloud-starter-hystrix org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
spring:
application:
name: eureka-ribbon-client
server:
port: 8764
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
1.4 org.ccit.learn.config.RibbonConfig.class
package org.ccit.learn.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
1.5 org.ccit.learn.service.RibbonService.class
package org.ccit.learn.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RibbonService {
@Autowired
private RestTemplate restTemplate;
public String hi(String name){
return restTemplate.getForObject("http://eureka-client/hi?name="+name,String.class);
}
}
1.6 Org.ccit.learn.controller.RibbonController.class
ackage org.ccit.learn.controller;
import org.ccit.learn.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RibbonController {
@Autowired
private RibbonService ribbonService;
@GetMapping("/hi")
public String hi(@RequestParam("name") String name){
return ribbonService.hi(name);
}
}
1.7 SpringCloudEurekaRibbonClient
package org.ccit.learn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
//@EnableRi
@SpringBootApplication
public class SpringCloudEurekaRibbonClient {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaRibbonClient.class,args);
}
}
1.8 运行结果
2.3 创建EurekaClientFeign接口SpringCloudLearn org.ccit 1.0-SNAPSHOT 4.0.0 org.ccit.SpringCloudLearn spring_cloud_eureka_feign_client org.springframework.cloud spring-cloud-starter-feign com.netflix.feign feign-httpclient RELEASE org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
package org.ccit.learn.feign;
import org.ccit.learn.config.FeignConfig;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "eureka-client",configuration = FeignConfig.class)
public interface EurekaClientFeign {
@GetMapping(value = "/hi")
String sayHiFromClientEureka(@RequestParam(value = "name") String name);
}
2.4 创建FeignConfig配置类
package org.ccit.learn.config;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static java.util.concurrent.TimeUnit.SECONDS;
@Configuration
public class FeignConfig {
@Bean
public Retryer feignRetryer(){
return new Retryer.Default(100,SECONDS.toMicros(1),5);
}
}
2.5 创建HiService
package org.ccit.learn.service;
import org.ccit.learn.feign.EurekaClientFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class HiService {
@Autowired
private EurekaClientFeign eurekaClientFeign;
public String sayHi(String name){
return eurekaClientFeign.sayHiFromClientEureka(name);
}
}
2.6 创建HiController
package org.ccit.learn.controller;
import org.ccit.learn.service.HiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HiController {
@Autowired
private HiService hiService;
@GetMapping("/hi")
public String sayHi(@RequestParam("name") String name){
return hiService.sayHi(name);
}
}
2.7 启动类
package org.ccit.learn;
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;
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class SpringCloudFeignClient {
public static void main(String[] args) {
SpringApplication.run( SpringCloudFeignClient.class,args);
}
}
2.8 运行结果



