1、结构概述

本文包含4个springboot服务,A.服务注册中心,B.被发现和被调用服务(注册到A服务),C.接口服务(注册到A服务) ,D. client服务(外部服务);

C服务调用B 服务,若是C调用B服务失败,有断路功能,并且C 服务对外提供接口供D 服务使用

 注意:示例只提供基本的逻辑,不做业务处理。

各个服务结构图如下:

 服务A:

pom文件:

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency><!--服务注册server,服务注册中心 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application yml文件:

server:
  port: 20001

eureka:
  instance:
    hostname: 127.0.0.1
    appname: eureka-server      #服务注册中心名称
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}  #服务注册中心IP
    registerWithEureka: false   #不能注册自身
    fetchRegistry: false

 启动类 java 文件:

@SpringBootApplication
@EnableEurekaServer//服务注册server注解
public class EurekaServerApplication {

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

}

 

服务B:

 pom 文件:

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency><!-- 服务健康监测 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application yml文件:

eureka:
  instance:
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://127.0.0.1:20001/eureka/ #注册中心地址


spring:
  application:
    name: organizationservice
  profiles:
    active:
      default
  cloud:
    config:
      enabled: true

server:
  port: 8087

服务启动Java文件:

@SpringBootApplication
@EnableEurekaClient//服务发现注解
public class ServerApplication {

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

}

控制层 Java文件:

@RestController
public class Hello {
    @GetMapping("/hello")
    public User hello() {
        User user = new User();
        user.setAge("66");
        user.setName("abcd");
        return user;
    }
}

 

服务C:

pom 文件:

<dependencies>
        <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>
        <dependency><!-- 服务健康状态监测 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
        <!-- 支持断路器-->
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>1.5.12</version>
        </dependency>
    </dependencies>

application yml文件:

eureka:
  instance:
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:20001/eureka/   #注册中心地址
spring:
  application:
    name: licensingservice
  profiles:
    active:
      default
  cloud:
    config:
      enabled: true

server:
  port: 8088

#打开断路器
feign:
  hystrix:
    enabled:  true

启动类 java文件:

/**
 * 接口应用
 * 发现 server 服务,调用 server服务应用的api
 * 不处理逻辑业务
 * 对外提供接口
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class LicenseApplication {

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

}

接口类:

//TestCallBack--- 若是调用organizationservice服务接口失败,调用TestCallBack类中相同的接口方法
@FeignClient(name = "organizationservice", fallback = TestCallBack.class)
public interface TestApi {
    @GetMapping("/hello")
    User hello();

}

断路器类:

@Component
public class TestCallBack implements TestApi {
	@Override
	public User hello() {
	    return null;
	}

}

控制类:

@RestController
public class TestController {
    @Autowired
    TestService testService;
    @GetMapping("/hello")
    public User hello () {
        return testService.hello();
    }

}

业务类:

@Service
public class TestService {
    @Autowired
    TestApi testApi;
    public User hello() {
        return testApi.hello();
    }
}

 

服务D:

pom 文件:

<dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
    </dependencies>

application 文件:

server.port=8889
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

启动类: 不需要修改,默认

控制类:

@RestController
public class TestController {

    @Autowired
    private TestService service;


     @GetMapping("/hello")
    public User getHello () {
        return service.getHello();
    }
}

业务类:

@Service
public class TestService {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    @Autowired
    private RestTemplate template;
    
    public User getHello () {
        String url = "http://localhost:8088/hello";
        ResponseEntity<User> resultResponseEntity = this.template.exchange(
                String.format(url),
                HttpMethod.GET, null, User.class);
        if (resultResponseEntity.getStatusCode() == HttpStatus.OK) {
            return resultResponseEntity.getBody();
        }
        return null;
    }


}

服务 A、B、C、D 建立好了之后,按顺序依次启动这几个服务。

进入服务注册中心(A服务),出现如下标注处的服务(B和C服务),说明服务已经注册到了注册中心:

 

进入D 服务,外部调用C服务,能够返回数据,说明请求成功:

 进入C服务调用接口:

 

关闭服务B,再次调用服务C接口:

返回内容为空,说明断路器起了作用。

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐