快速构建Spring Cloud 微服务架构项目(一) springcloud初探

1、微服务架构基于Spring boot项目

2、构建工具Idea

3、步骤:

a)服务中心启动

b)各个应用注册,可以使用基于http或rpc等形式调用

 

一、创建基于maven项目的spring boot项目

a)创建springboot项目

b)删除目录下的src等一系列文件,只留.idea,XXX.iml,pom.xml文件

二、创建Eureka服务中心

在项目内创建modules

快速构建Spring Cloud 微服务架构项目(一) springcloud初探_第1张图片

快速构建Spring Cloud 微服务架构项目(一) springcloud初探_第2张图片

快速构建Spring Cloud 微服务架构项目(一) springcloud初探_第3张图片

成功后在启动方法中添加注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class RegistServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(RegistServerApplication.class, args);
    }

}

在application.yml加入

#测试环境
server:
  port: 8989
eureka:
  instance:
    hostname: localhost
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
    #关闭安全检查
    healthcheck:
      enabled: true
  server:
    waitTimeInMsWhenSyncEmpty: 0
    #取消自我保护
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 4000

 

启动项目

访问 http://localhost:8989  即可看到

快速构建Spring Cloud 微服务架构项目(一) springcloud初探_第4张图片

三、利用先前方法构建ribbon 或者 openFeign

修改父目录 pom.xml文件


    regist-server
    ribbon
    ribbon-consumer
    feign

各个子目录下pom.xml对应需要修改


    
    cloud
    1.0-SNAPSHOT

 

注:所有子module 需要指向注册中心的端口,子module 中port端口号不能重名

快速构建Spring Cloud 微服务架构项目(一) springcloud初探_第5张图片

#测试环境
server:
  port: 8990
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8989/eureka/

 

ribbon-0项目

主方法需要加入

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(RibbonApplication.class, args);
    }

    @Bean
    @Autowired
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

service层

@Service
public class HelloService {
    public String sayHello() {
        return "hello World";
    }
}

controller层

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String say(String name) {
        return helloService.sayHello() + " " + name;
    }
}

 

ribbon-1项目

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {


    @Bean
    @Autowired
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumerApplication.class, args);
    }
}

controller层

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String say(String name) {
        return helloService.sayHello() + " " + name;
    }
}

service层

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String sayHello() {
        return restTemplate.getForObject("http://ribbon-provider/hello?name=zhang",String.class);
    }
}

ribbon-provider 是 ribbon-0中的 spring.application.name 在 ribbon-0中的application.yml中有写

 

基于rpc的OpenFeign

 

feign项目

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class, args);
    }

}

 

接口

@FeignClient(value="ribbon-consumer")
public interface GetHello {
    @RequestMapping(value = "/hello?name=1",method = RequestMethod.GET)
    public String sayHello();
}

service层

@Service
public class HelloService {

    @Autowired
    private GetHello getHello;

    public String sayHello() {
        return getHello.sayHello();
    }
}

controller层

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String say(String name) {
        return helloService.sayHello() + " " + name;
    }
}

 

报错:

FreeMarker template error (DEBUG mode; use RETHROW in production!) The following has evaluated to null or missing == replica.key

 

大部分原因是在defaultZone下少写东西。具体原因待细查(freemaker的错)快速构建Spring Cloud 微服务架构项目(一) springcloud初探_第6张图片

 

你可能感兴趣的:(SpringCloud)