Spring Cloud Services to build micro-series (X): As a registered between service uses nacos call center openfeign

Previous obtain the currently logged on user we explain how to get the user currently logged on

We took off in terms of this one registration center, registration center in fact is a core component of the micro-services

This article Source:

Source Address

Common registry:

  • eureka maintains only version 1.X, 2.x version has been abandoned. CA belong to the principle of registry
  • ZooKeeper high consistency of the registry, the registry is dubbo better support
  • consul can understand that eureka sublimation, for large systems
  • Ali nacos new open source registry and configuration center integration services.

In fact, the above registry each have their own advantages, as to how to choose depends on the wishes of the individual.

Here to explain is based nacos for the registry configuration

Please refer to integration:

Registration center integration

After the integrated registry, calls between official to the service as follows:

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

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

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

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
        }
    }
}

But for openfeign accustomed to the way I am, it is a bit messed up, but each had to write connection address and in fact way before calling external API is the same, nothing more than now become internal calls. Then the following for use under the openfeign:

First introduced to increase POM file:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

Start class adds annotation:

@EnableFeignClients

New call to service:

@FeignClient(name = "user")
public interface UserService {
    /**
     * 通过用户名,或者email获取用户名
     * @param email
     * @return
     */
    @GetMapping("/user/email/{email}")
    ComResponse<User> getUserByEmail(@PathVariable("email") String email);

    /**
     * 通过用户名,或者email获取用户名
     * @param telephone
     * @return
     */
    @GetMapping("/user/tel/{telephone}")
    ComResponse<User> getUserByTel(@PathVariable("telephone") String telephone);

    /**
     * 根据用户查询角色
     * @param userId
     * @return
     */
    @GetMapping("/role/list/{userId}")
    ComResponse<List<Role>> listByUser(@PathVariable("userId") Long userId);
    /**
     * 通过用户查询权限
     * @param roleId
     * @return
     */
    @GetMapping("/permission/list/{roleId}")
    ComResponse<List<Permission>> listByRole(@PathVariable("roleId") Long roleId);
}

This is not yet a very familiar feeling it. This can be directly introduced into the service call.

He published 183 original articles · won praise 37 · views 160 000 +

Guess you like

Origin blog.csdn.net/zhuwei_clark/article/details/104790681